forked from DataDog/dd-trace-go
/
option.go
136 lines (118 loc) · 3.71 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
// 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-2020 Datadog, Inc.
package grpc
import (
"math"
"gopkg.in/DataDog/dd-trace-go.v1/internal/globalconfig"
"google.golang.org/grpc/codes"
)
// 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 string
nonErrorCodes map[codes.Code]bool
analyticsRate float64
traceStreamCalls bool
traceStreamMessages bool
noDebugStack bool
ignoredMethods map[string]struct{}
}
func (cfg *config) serverServiceName() string {
if cfg.serviceName == "" {
cfg.serviceName = "grpc.server"
if svc := globalconfig.ServiceName(); svc != "" {
cfg.serviceName = svc
}
}
return cfg.serviceName
}
func (cfg *config) clientServiceName() string {
if cfg.serviceName == "" {
return "grpc.client"
}
return cfg.serviceName
}
// 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.serviceName defaults are set in interceptors
cfg.traceStreamCalls = true
cfg.traceStreamMessages = true
cfg.nonErrorCodes = map[codes.Code]bool{codes.Canceled: true}
// cfg.analyticsRate = globalconfig.AnalyticsRate()
cfg.analyticsRate = math.NaN()
}
// WithServiceName sets the given service name for the intercepted client.
func WithServiceName(name string) Option {
return func(cfg *config) {
cfg.serviceName = 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 ...codes.Code) InterceptorOption {
return func(cfg *config) {
cfg.nonErrorCodes = make(map[codes.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 {
cfg.analyticsRate = 1.0
} else {
cfg.analyticsRate = math.NaN()
}
}
}
// 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 {
cfg.analyticsRate = rate
} else {
cfg.analyticsRate = math.NaN()
}
}
}
// 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.
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
}
}