-
Notifications
You must be signed in to change notification settings - Fork 15
/
noop.go
232 lines (168 loc) · 5.08 KB
/
noop.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package pinpoint
import (
"context"
"encoding/json"
"fmt"
"time"
)
type noopAgent struct {
config *Config
}
var defaultNoopAgent = &noopAgent{
config: defaultConfig(),
}
// NoopAgent returns a Agent that doesn't collect tracing data.
func NoopAgent() Agent {
return defaultNoopAgent
}
func (agent *noopAgent) NewSpanTracer(operation string, rpcName string) Tracer {
return NoopTracer()
}
func (agent *noopAgent) NewSpanTracerWithReader(operation string, rpcName string, reader DistributedTracingContextReader) Tracer {
return NoopTracer()
}
func (agent *noopAgent) Enable() bool {
return false
}
func (agent *noopAgent) Config() *Config {
return agent.config
}
func (agent *noopAgent) Shutdown() {
}
type noopSpan struct {
agent *agent
spanId int64
startTime time.Time
rpcName string
goroutineId int64
withStats bool
urlStat *UrlStatEntry
noopSe noopSpanEvent
annotations noopAnnotation
}
var defaultNoopSpan = noopSpan{}
// NoopTracer returns a Tracer that doesn't collect tracing data.
func NoopTracer() Tracer {
return &defaultNoopSpan
}
func newUnSampledSpan(agent *agent, rpcName string) *noopSpan {
span := noopSpan{}
span.agent = agent
span.spanId = generateSpanId()
span.startTime = time.Now()
span.rpcName = rpcName
span.withStats = true
addUnSampledActiveSpan(&span)
return &span
}
func (span *noopSpan) EndSpan() {
if span.withStats {
dropUnSampledActiveSpan(span)
endTime := time.Now()
elapsed := endTime.UnixMilli() - span.startTime.UnixMilli()
collectResponseTime(elapsed)
if span.urlStat != nil {
span.agent.enqueueUrlStat(&urlStat{entry: span.urlStat, endTime: endTime, elapsed: elapsed})
}
}
}
func (span *noopSpan) NewSpanEvent(operationName string) Tracer {
return span
}
func (span *noopSpan) NewAsyncSpan() Tracer {
return &noopSpan{}
}
func (span *noopSpan) NewGoroutineTracer() Tracer {
return &noopSpan{}
}
func (span *noopSpan) WrapGoroutine(goroutineName string, goroutine func(context.Context), ctx context.Context) func() {
asyncSpan := span.NewGoroutineTracer()
var newCtx context.Context
if ctx == nil {
newCtx = NewContext(context.Background(), asyncSpan)
} else {
newCtx = NewContext(ctx, asyncSpan)
}
return func() {
goroutine(newCtx)
}
}
func (span *noopSpan) EndSpanEvent() {}
func (span *noopSpan) TransactionId() TransactionId {
return TransactionId{"Noop", 0, 0}
}
func (span *noopSpan) SpanId() int64 {
return span.spanId
}
func (span *noopSpan) AsyncSpanId() string {
return fmt.Sprintf("%d^Noop", span.spanId)
}
func (span *noopSpan) Span() SpanRecorder {
return span
}
func (span *noopSpan) SpanEvent() SpanEventRecorder {
return &span.noopSe
}
func (span *noopSpan) SetError(e error) {}
func (span *noopSpan) SetFailure() {}
func (span *noopSpan) SetServiceType(typ int32) {}
func (span *noopSpan) SetRpcName(rpc string) {}
func (span *noopSpan) SetRemoteAddress(remoteAddress string) {}
func (span *noopSpan) SetEndPoint(endPoint string) {}
func (span *noopSpan) SetAcceptorHost(host string) {}
func (span *noopSpan) Inject(writer DistributedTracingContextWriter) {
if writer != nil {
writer.Set(HeaderSampled, "s0")
}
}
func (span *noopSpan) Extract(reader DistributedTracingContextReader) {}
func (span *noopSpan) Annotations() Annotation {
return &span.annotations
}
func (span *noopSpan) SetLogging(logInfo int32) {}
func (span *noopSpan) IsSampled() bool {
return false
}
func (span *noopSpan) collectUrlStat(stat *UrlStatEntry) {
if span.withStats && span.agent.config.collectUrlStat {
if stat.Url == "" {
stat.Url = "UNKNOWN_URL"
}
span.urlStat = stat
}
}
func (span *noopSpan) AddMetric(metric string, value interface{}) {
if metric == MetricURLStat {
span.collectUrlStat(value.(*UrlStatEntry))
}
}
func (span *noopSpan) JsonString() []byte {
b, _ := json.Marshal(span)
return b
}
type noopSpanEvent struct {
annotations noopAnnotation
}
var defaultNoopSpanEvent = noopSpanEvent{}
func (se *noopSpanEvent) SetError(e error, errorName ...string) {}
func (se *noopSpanEvent) SetServiceType(typ int32) {}
func (se *noopSpanEvent) SetDestination(id string) {}
func (se *noopSpanEvent) SetEndPoint(endPoint string) {}
func (se *noopSpanEvent) SetSQL(sql string, args string) {}
func (se *noopSpanEvent) Annotations() Annotation {
return &se.annotations
}
func (se *noopSpanEvent) FixDuration(start time.Time, end time.Time) {}
type noopAnnotation struct{}
func (a *noopAnnotation) AppendInt(key int32, i int32) {}
func (a *noopAnnotation) AppendLong(key int32, l int64) {}
func (a *noopAnnotation) AppendString(key int32, s string) {}
func (a *noopAnnotation) AppendStringString(key int32, s1 string, s2 string) {}
func (a *noopAnnotation) AppendIntStringString(key int32, i int32, s1 string, s2 string) {}
func (a *noopAnnotation) AppendBytesStringString(key int32, b []byte, s1 string, s2 string) {}
func (a *noopAnnotation) AppendLongIntIntByteByteString(key int32, l int64, i1 int32, i2 int32, b1 int32, b2 int32, s string) {
}
type noopDistributedTracingContextReader struct{}
func (r *noopDistributedTracingContextReader) Get(key string) string {
return ""
}