-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbackend.go
38 lines (32 loc) · 881 Bytes
/
backend.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package metrics
import (
"context"
"github.com/devopsfaith/krakend/config"
"github.com/devopsfaith/krakend/proxy"
"github.com/newrelic/go-agent"
)
// BackendFactory creates an instrumented backend factory
func BackendFactory(segmentName string, next proxy.BackendFactory) proxy.BackendFactory {
if app == nil {
return next
}
return func(cfg *config.Backend) proxy.Proxy {
return NewBackend(segmentName, next(cfg))
}
}
// NewBackend includes NewRelic segmentation
func NewBackend(segmentName string, next proxy.Proxy) proxy.Proxy {
if app == nil {
return next
}
return func(ctx context.Context, req *proxy.Request) (*proxy.Response, error) {
tx, ok := ctx.Value(nrCtxKey).(newrelic.Transaction)
if !ok {
return next(ctx, req)
}
segment := newrelic.StartSegment(tx, segmentName)
resp, err := next(ctx, req)
segment.End()
return resp, err
}
}