-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoption.go
189 lines (168 loc) · 5.63 KB
/
option.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
186
187
188
189
package connect
import (
"connectrpc.com/connect"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
)
const (
defaultServerServiceName = "connect.server"
)
// Option specifies a configuration option for the grpc package. Not all options apply
// to all instrumented structures.
type Option func(*config)
type config struct {
serviceName func() string
spanName string
nonErrorCodes map[connect.Code]bool
traceStreamCalls bool
traceStreamMessages bool
noDebugStack bool
ignoredMethods map[string]struct{}
untracedMethods map[string]struct{}
withMetadataTags bool
ignoredMetadata map[string]struct{}
withRequestTags bool
spanOpts []ddtrace.StartSpanOption
tags map[string]interface{}
}
// InterceptorOption represents an option that can be passed to the grpc unary
// client and server interceptors.
// InterceptorOption is deprecated in favor of Option.
type InterceptorOption = Option
func defaults(cfg *config) {
cfg.traceStreamCalls = true
cfg.traceStreamMessages = true
cfg.nonErrorCodes = map[connect.Code]bool{connect.CodeCanceled: true}
// cfg.spanOpts = append(cfg.spanOpts, tracer.AnalyticsRate(globalconfig.AnalyticsRate()))
//if internal.BoolEnv("DD_TRACE_GRPC_ANALYTICS_ENABLED", false) {
// cfg.spanOpts = append(cfg.spanOpts, tracer.AnalyticsRate(1.0))
//}
cfg.ignoredMetadata = map[string]struct{}{
"x-datadog-trace-id": {},
"x-datadog-parent-id": {},
"x-datadog-sampling-priority": {},
}
}
func serverDefaults(cfg *config) {
// We check for a configured service name, so we don't break users who are incorrectly creating their server
// before the call `tracer.Start()`
cfg.serviceName = func() string { return defaultServerServiceName }
cfg.spanName = "connect.server.request"
defaults(cfg)
}
// WithServiceName sets the given service name for the intercepted client.
func WithServiceName(name string) Option {
return func(cfg *config) {
cfg.serviceName = func() string { return name }
}
}
// WithStreamCalls enables or disables tracing of streaming calls. This option does not apply to the
// stats handler.
func WithStreamCalls(enabled bool) Option {
return func(cfg *config) {
cfg.traceStreamCalls = enabled
}
}
// WithStreamMessages enables or disables tracing of streaming messages. This option does not apply
// to the stats handler.
func WithStreamMessages(enabled bool) Option {
return func(cfg *config) {
cfg.traceStreamMessages = enabled
}
}
// NoDebugStack disables debug stacks for traces with errors. This is useful in situations
// where errors are frequent and the overhead of calling debug.Stack may affect performance.
func NoDebugStack() Option {
return func(cfg *config) {
cfg.noDebugStack = true
}
}
// NonErrorCodes determines the list of codes which will not be considered errors in instrumentation.
// This call overrides the default handling of codes.Canceled as a non-error.
func NonErrorCodes(cs ...connect.Code) InterceptorOption {
return func(cfg *config) {
cfg.nonErrorCodes = make(map[connect.Code]bool, len(cs))
for _, c := range cs {
cfg.nonErrorCodes[c] = true
}
}
}
// WithAnalytics enables Trace Analytics for all started spans.
func WithAnalytics(on bool) Option {
return func(cfg *config) {
if on {
WithSpanOptions(tracer.AnalyticsRate(1.0))(cfg)
}
}
}
// WithAnalyticsRate sets the sampling rate for Trace Analytics events
// correlated to started spans.
func WithAnalyticsRate(rate float64) Option {
return func(cfg *config) {
if rate >= 0.0 && rate <= 1.0 {
WithSpanOptions(tracer.AnalyticsRate(rate))(cfg)
}
}
}
// WithIgnoredMethods specifies full methods to be ignored by the server side interceptor.
// When an incoming request's full method is in ms, no spans will be created.
//
// Deprecated: This is deprecated in favor of WithUntracedMethods which applies to both
// the server side and client side interceptors.
func WithIgnoredMethods(ms ...string) Option {
ims := make(map[string]struct{}, len(ms))
for _, e := range ms {
ims[e] = struct{}{}
}
return func(cfg *config) {
cfg.ignoredMethods = ims
}
}
// WithUntracedMethods specifies full methods to be ignored by the server side and client
// side interceptors. When a request's full method is in ms, no spans will be created.
func WithUntracedMethods(ms ...string) Option {
ums := make(map[string]struct{}, len(ms))
for _, e := range ms {
ums[e] = struct{}{}
}
return func(cfg *config) {
cfg.untracedMethods = ums
}
}
// WithMetadataTags specifies whether gRPC metadata should be added to spans as tags.
func WithMetadataTags() Option {
return func(cfg *config) {
cfg.withMetadataTags = true
}
}
// WithIgnoredMetadata specifies keys to be ignored while tracing the metadata. Must be used
// in conjunction with WithMetadataTags.
func WithIgnoredMetadata(ms ...string) Option {
return func(cfg *config) {
for _, e := range ms {
cfg.ignoredMetadata[e] = struct{}{}
}
}
}
// WithRequestTags specifies whether gRPC requests should be added to spans as tags.
func WithRequestTags() Option {
return func(cfg *config) {
cfg.withRequestTags = true
}
}
// WithCustomTag will attach the value to the span tagged by the key.
func WithCustomTag(key string, value interface{}) Option {
return func(cfg *config) {
if cfg.tags == nil {
cfg.tags = make(map[string]interface{})
}
cfg.tags[key] = value
}
}
// WithSpanOptions defines a set of additional ddtrace.StartSpanOption to be added
// to spans started by the integration.
func WithSpanOptions(opts ...ddtrace.StartSpanOption) Option {
return func(cfg *config) {
cfg.spanOpts = append(cfg.spanOpts, opts...)
}
}