forked from DataDog/dd-trace-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
185 lines (173 loc) · 5.59 KB
/
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016 Datadog, Inc.
package grpc
import (
"github.com/lsgndln/dd-trace-go/ddtrace"
"github.com/lsgndln/dd-trace-go/ddtrace/ext"
"github.com/lsgndln/dd-trace-go/ddtrace/tracer"
"github.com/lsgndln/dd-trace-go/internal/appsec"
"github.com/lsgndln/dd-trace-go/internal/log"
"github.com/golang/protobuf/jsonpb"
"github.com/golang/protobuf/proto"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
)
type serverStream struct {
grpc.ServerStream
cfg *config
method string
ctx context.Context
}
// Context returns the ServerStream Context.
//
// One subtle difference between the server stream and the client stream is the
// order the contexts are created. In the client stream we pass the context to
// the streamer function, which means the ClientStream.Context() derives from
// the span context, so we want to return that. However with the ServerStream
// the span context derives from the ServerStream.Context, so we want to return
// the span context instead.
func (ss *serverStream) Context() context.Context {
return ss.ctx
}
func (ss *serverStream) RecvMsg(m interface{}) (err error) {
_, im := ss.cfg.ignoredMethods[ss.method]
_, um := ss.cfg.untracedMethods[ss.method]
if ss.cfg.traceStreamMessages && !im && !um {
span, _ := startSpanFromContext(
ss.ctx,
ss.method,
"grpc.message",
ss.cfg.serviceName,
ss.cfg.startSpanOptions(tracer.Measured())...,
)
span.SetTag(ext.Component, componentName)
defer func() {
withMetadataTags(ss.ctx, ss.cfg, span)
withRequestTags(ss.cfg, m, span)
finishWithError(span, err, ss.cfg)
}()
}
err = ss.ServerStream.RecvMsg(m)
return err
}
func (ss *serverStream) SendMsg(m interface{}) (err error) {
_, im := ss.cfg.ignoredMethods[ss.method]
_, um := ss.cfg.untracedMethods[ss.method]
if ss.cfg.traceStreamMessages && !im && !um {
span, _ := startSpanFromContext(
ss.ctx,
ss.method,
"grpc.message",
ss.cfg.serviceName,
ss.cfg.startSpanOptions(tracer.Measured())...,
)
span.SetTag(ext.Component, componentName)
defer func() { finishWithError(span, err, ss.cfg) }()
}
err = ss.ServerStream.SendMsg(m)
return err
}
// StreamServerInterceptor will trace streaming requests to the given gRPC server.
func StreamServerInterceptor(opts ...Option) grpc.StreamServerInterceptor {
cfg := new(config)
serverDefaults(cfg)
for _, fn := range opts {
fn(cfg)
}
log.Debug("contrib/google.golang.org/grpc: Configuring StreamServerInterceptor: %#v", cfg)
return func(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) (err error) {
ctx := ss.Context()
// if we've enabled call tracing, create a span
_, im := cfg.ignoredMethods[info.FullMethod]
_, um := cfg.untracedMethods[info.FullMethod]
if cfg.traceStreamCalls && !im && !um {
var span ddtrace.Span
span, ctx = startSpanFromContext(
ctx,
info.FullMethod,
cfg.spanName,
cfg.serviceName,
cfg.startSpanOptions(tracer.Measured(),
tracer.Tag(ext.Component, componentName),
tracer.Tag(ext.SpanKind, ext.SpanKindServer))...,
)
switch {
case info.IsServerStream && info.IsClientStream:
span.SetTag(tagMethodKind, methodKindBidiStream)
case info.IsServerStream:
span.SetTag(tagMethodKind, methodKindServerStream)
case info.IsClientStream:
span.SetTag(tagMethodKind, methodKindClientStream)
}
defer func() { finishWithError(span, err, cfg) }()
if appsec.Enabled() {
handler = appsecStreamHandlerMiddleware(span, handler)
}
}
// call the original handler with a new stream, which traces each send
// and recv if message tracing is enabled
return handler(srv, &serverStream{
ServerStream: ss,
cfg: cfg,
method: info.FullMethod,
ctx: ctx,
})
}
}
// UnaryServerInterceptor will trace requests to the given grpc server.
func UnaryServerInterceptor(opts ...Option) grpc.UnaryServerInterceptor {
cfg := new(config)
serverDefaults(cfg)
for _, fn := range opts {
fn(cfg)
}
log.Debug("contrib/google.golang.org/grpc: Configuring UnaryServerInterceptor: %#v", cfg)
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
_, im := cfg.ignoredMethods[info.FullMethod]
_, um := cfg.untracedMethods[info.FullMethod]
if im || um {
return handler(ctx, req)
}
span, ctx := startSpanFromContext(
ctx,
info.FullMethod,
cfg.spanName,
cfg.serviceName,
cfg.startSpanOptions(tracer.Measured(),
tracer.Tag(ext.Component, componentName),
tracer.Tag(ext.SpanKind, ext.SpanKindServer))...,
)
span.SetTag(tagMethodKind, methodKindUnary)
withMetadataTags(ctx, cfg, span)
withRequestTags(cfg, req, span)
if appsec.Enabled() {
handler = appsecUnaryHandlerMiddleware(span, handler)
}
resp, err := handler(ctx, req)
finishWithError(span, err, cfg)
return resp, err
}
}
func withMetadataTags(ctx context.Context, cfg *config, span ddtrace.Span) {
if cfg.withMetadataTags {
md, _ := metadata.FromIncomingContext(ctx) // nil is ok
for k, v := range md {
if _, ok := cfg.ignoredMetadata[k]; !ok {
span.SetTag(tagMetadataPrefix+k, v)
}
}
}
}
func withRequestTags(cfg *config, req interface{}, span ddtrace.Span) {
if cfg.withRequestTags {
var m jsonpb.Marshaler
if p, ok := req.(proto.Message); ok {
if s, err := m.MarshalToString(p); err == nil {
span.SetTag(tagRequest, s)
}
}
}
}