-
Notifications
You must be signed in to change notification settings - Fork 2
/
newrelic.go
68 lines (54 loc) · 1.68 KB
/
newrelic.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package newrelic
import (
"context"
newrelic "github.com/americanas-go/ignite/newrelic/go-agent.v3"
"github.com/americanas-go/log"
"github.com/newrelic/go-agent/v3/integrations/nrgrpc"
"google.golang.org/grpc"
)
// Register registers a new compressor plugin for grpc server.
// Compressor represents compressor plugin for grpc server.
// NewCompressorWithOptions returns a new compressor plugin with options.
// NewCompressorWithConfigPath returns a new compressor plugin with options from config path.
// NewCompressor returns a new compressor plugin with default options.
// Register registers this compressor plugin for grpc server.
func Register(ctx context.Context) []grpc.ServerOption {
o, err := NewOptions()
if err != nil {
return nil
}
h := NewNewrelicWithOptions(o)
return h.Register(ctx)
}
type Newrelic struct {
options *Options
}
func NewNewrelicWithOptions(options *Options) *Newrelic {
return &Newrelic{options: options}
}
func NewNewrelicWithConfigPath(path string) (*Newrelic, error) {
o, err := NewOptionsWithPath(path)
if err != nil {
return nil, err
}
return NewNewrelicWithOptions(o), nil
}
func NewNewrelic() *Newrelic {
o, err := NewOptions()
if err != nil {
log.Fatalf(err.Error())
}
return NewNewrelicWithOptions(o)
}
func (i *Newrelic) Register(ctx context.Context) []grpc.ServerOption {
if !i.options.Enabled || !newrelic.IsEnabled() {
return nil
}
app := newrelic.Application()
logger := log.FromContext(ctx)
logger.Debug("newrelic interceptor successfully enabled in grpc server")
return []grpc.ServerOption{
grpc.ChainUnaryInterceptor(nrgrpc.UnaryServerInterceptor(app)),
grpc.ChainStreamInterceptor(nrgrpc.StreamServerInterceptor(app)),
}
}