/
nrgrpc_server.go
136 lines (119 loc) · 4.25 KB
/
nrgrpc_server.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright 2020 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
package nrgrpc
import (
"context"
"net/http"
"strings"
newrelic "github.com/newrelic/go-agent"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
)
func startTransaction(ctx context.Context, app newrelic.Application, fullMethod string) newrelic.Transaction {
method := strings.TrimPrefix(fullMethod, "/")
var hdrs http.Header
if md, ok := metadata.FromIncomingContext(ctx); ok {
hdrs = make(http.Header, len(md))
for k, vs := range md {
for _, v := range vs {
hdrs.Add(k, v)
}
}
}
target := hdrs.Get(":authority")
url := getURL(method, target)
webReq := newrelic.NewStaticWebRequest(hdrs, url, method, newrelic.TransportHTTP)
txn := app.StartTransaction(method, nil, nil)
txn.SetWebRequest(webReq)
return txn
}
// UnaryServerInterceptor instruments server unary RPCs.
//
// Use this function with grpc.UnaryInterceptor and a newrelic.Application to
// create a grpc.ServerOption to pass to grpc.NewServer. This interceptor
// records each unary call with a transaction. You must use both
// UnaryServerInterceptor and StreamServerInterceptor to instrument unary and
// streaming calls.
//
// Example:
//
// cfg := newrelic.NewConfig("gRPC Server", os.Getenv("NEW_RELIC_LICENSE_KEY"))
// app, _ := newrelic.NewApplication(cfg)
// server := grpc.NewServer(
// grpc.UnaryInterceptor(nrgrpc.UnaryServerInterceptor(app)),
// grpc.StreamInterceptor(nrgrpc.StreamServerInterceptor(app)),
// )
//
// These interceptors add the transaction to the call context so it may be
// accessed in your method handlers using newrelic.FromContext.
//
// Full example:
// https://github.com/newrelic/go-agent/blob/master/_integrations/nrgrpc/example/server/server.go
//
func UnaryServerInterceptor(app newrelic.Application) grpc.UnaryServerInterceptor {
if nil == app {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
return handler(ctx, req)
}
}
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
txn := startTransaction(ctx, app, info.FullMethod)
defer txn.End()
ctx = newrelic.NewContext(ctx, txn)
resp, err = handler(ctx, req)
txn.WriteHeader(int(status.Code(err)))
return
}
}
type wrappedServerStream struct {
grpc.ServerStream
txn newrelic.Transaction
}
func (s wrappedServerStream) Context() context.Context {
ctx := s.ServerStream.Context()
return newrelic.NewContext(ctx, s.txn)
}
func newWrappedServerStream(stream grpc.ServerStream, txn newrelic.Transaction) grpc.ServerStream {
return wrappedServerStream{
ServerStream: stream,
txn: txn,
}
}
// StreamServerInterceptor instruments server streaming RPCs.
//
// Use this function with grpc.StreamInterceptor and a newrelic.Application to
// create a grpc.ServerOption to pass to grpc.NewServer. This interceptor
// records each streaming call with a transaction. You must use both
// UnaryServerInterceptor and StreamServerInterceptor to instrument unary and
// streaming calls.
//
// Example:
//
// cfg := newrelic.NewConfig("gRPC Server", os.Getenv("NEW_RELIC_LICENSE_KEY"))
// app, _ := newrelic.NewApplication(cfg)
// server := grpc.NewServer(
// grpc.UnaryInterceptor(nrgrpc.UnaryServerInterceptor(app)),
// grpc.StreamInterceptor(nrgrpc.StreamServerInterceptor(app)),
// )
//
// These interceptors add the transaction to the call context so it may be
// accessed in your method handlers using newrelic.FromContext.
//
// Full example:
// https://github.com/newrelic/go-agent/blob/master/_integrations/nrgrpc/example/server/server.go
//
func StreamServerInterceptor(app newrelic.Application) grpc.StreamServerInterceptor {
if nil == app {
return func(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
return handler(srv, ss)
}
}
return func(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
txn := startTransaction(ss.Context(), app, info.FullMethod)
defer txn.End()
err := handler(srv, newWrappedServerStream(ss, txn))
txn.WriteHeader(int(status.Code(err)))
return err
}
}