forked from pinpoint-apm/pinpoint-go-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
span.go
276 lines (217 loc) · 5.9 KB
/
span.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package pinpoint
import (
"container/list"
"math/rand"
"strconv"
"strings"
"sync/atomic"
"time"
)
var asyncIdGen int32 = 0
type span struct {
agent *Agent
txId TransactionId
spanId int64
parentSpanId int64
parentAppName string
parentAppType int
parentAppNamespace string
serviceType int32
rpcName string
endPoint string
remoteAddr string
acceptorHost string
spanEvents []*spanEvent
annotations annotation
loggingInfo int32
apiId int32
eventSequence int32
eventDepth int32
startTime time.Time
duration time.Duration
operationName string
sampled bool
flags int
err int
asyncId int32
asyncSequence int32
stack *list.List
}
func toMicroseconds(d time.Duration) int64 { return int64(d) / 1e3 }
func toMilliseconds(d time.Duration) int64 { return int64(d) / 1e6 }
func generateSpanId() int64 {
return rand.Int63()
}
func defaultSpan() *span {
span := span{}
span.parentSpanId = -1
span.parentAppType = -1
span.eventDepth = 1
span.sampled = true
span.serviceType = ServiceTypeGoApp
span.startTime = time.Now()
span.stack = list.New()
return &span
}
func newSampledSpan(agent *Agent, operation string) Tracer {
span := defaultSpan()
span.agent = agent
span.operationName = operation
return span
}
func (span *span) EndSpan() {
for e := span.stack.Front(); e != nil; e = e.Next() {
se := e.Value.(*spanEvent)
se.end()
}
dropActiveSpan(span.spanId)
span.duration = time.Now().Sub(span.startTime)
collectResponseTime(toMilliseconds(span.duration))
if !span.agent.tryEnqueueSpan(span) {
log("span").Debug("span channel - max capacity reached or closed")
}
}
func (span *span) Inject(writer DistributedTracingContextWriter) {
writer.Set(HttpTraceId, span.txId.String())
se := span.stack.Front().Value.(*spanEvent)
nextSpanId := se.generateNextSpanId()
writer.Set(HttpSpanId, strconv.FormatInt(nextSpanId, 10))
writer.Set(HttpParentSpanId, strconv.FormatInt(span.spanId, 10))
writer.Set(HttpFlags, strconv.Itoa(span.flags))
writer.Set(HttpParentApplicationName, span.agent.config.ApplicationName)
writer.Set(HttpParentApplicationType, strconv.Itoa(int(span.agent.config.ApplicationType)))
writer.Set(HttpParentApplicationNamespace, "")
se.endPoint = se.destinationId
writer.Set(HttpHost, se.destinationId)
log("span").Debug("span inject: ", span.txId, nextSpanId, span.spanId, se.destinationId)
}
func (span *span) Extract(reader DistributedTracingContextReader) {
tid := reader.Get(HttpTraceId)
if tid != "" {
s := strings.Split(tid, "^")
span.txId.AgentId = s[0]
span.txId.StartTime, _ = strconv.ParseInt(s[1], 10, 0)
span.txId.Sequence, _ = strconv.ParseInt(s[2], 10, 0)
} else {
span.txId = span.agent.generateTransactionId()
}
spanid := reader.Get(HttpSpanId)
if spanid != "" {
span.spanId, _ = strconv.ParseInt(spanid, 10, 0)
} else {
span.spanId = generateSpanId()
}
pspanid := reader.Get(HttpParentSpanId)
if pspanid != "" {
span.parentSpanId, _ = strconv.ParseInt(pspanid, 10, 0)
}
flag := reader.Get(HttpFlags)
if flag != "" {
span.flags, _ = strconv.Atoi(flag)
}
pappname := reader.Get(HttpParentApplicationName)
if pappname != "" {
span.parentAppName = pappname
}
papptype := reader.Get(HttpParentApplicationType)
if papptype != "" {
span.parentAppType, _ = strconv.Atoi(papptype)
}
host := reader.Get(HttpHost)
if host != "" {
span.acceptorHost = host
span.remoteAddr = host
span.endPoint = host
}
sampled := reader.Get(HttpSampled)
if sampled == "s0" {
span.sampled = false
} else {
span.sampled = true
}
addActiveSpan(span.spanId, span.startTime)
log("span").Debug("span extract: ", tid, spanid, pappname, pspanid, papptype, host, sampled)
}
func (span *span) NewSpanEvent(operationName string) Tracer {
se := newSpanEvent(span, operationName)
span.eventSequence++
span.eventDepth++
span.spanEvents = append(span.spanEvents, se)
span.stack.PushFront(se)
return span
}
func (span *span) EndSpanEvent() {
if span.stack.Len() > 0 {
e := span.stack.Front()
span.stack.Remove(e).(*spanEvent).end()
}
}
func (span *span) NewAsyncSpan() Tracer {
se := span.stack.Front().Value.(*spanEvent)
asyncSpan := newSpanForAsync(span)
if se.asyncId == 0 {
se.asyncId = atomic.AddInt32(&asyncIdGen, 1)
}
asyncSpan.asyncId = se.asyncId
se.asyncSeqGen++
asyncSpan.asyncSequence = se.asyncSeqGen
asyncSpan.newSpanEventForAsync()
return asyncSpan
}
func newSpanForAsync(parentSpan *span) *span {
span := defaultSpan()
span.agent = parentSpan.agent
span.txId = parentSpan.txId
span.spanId = parentSpan.spanId
return span
}
func (span *span) newSpanEventForAsync() {
se := newSpanEvent(span, "")
se.serviceType = 100 // ASYNC
se.apiId = asyncApiId
span.eventSequence++
span.eventDepth++
span.spanEvents = append(span.spanEvents, se)
span.stack.PushFront(se)
}
func (span *span) TransactionId() TransactionId {
return span.txId
}
func (span *span) SpanId() int64 {
return span.spanId
}
func (span *span) Span() SpanRecorder {
return span
}
func (span *span) SpanEvent() SpanEventRecorder {
return span.stack.Front().Value.(*spanEvent)
}
func (span *span) SetError(e error) {
//se := span.stack.Front().Value.(*spanEvent)
//se.SetError(e)
span.err = 1
}
func (span *span) SetApiId(id int32) {
span.apiId = id
}
func (span *span) SetServiceType(typ int32) {
span.serviceType = typ
}
func (span *span) SetRpcName(rpc string) {
span.rpcName = rpc
}
func (span *span) SetRemoteAddress(remoteAddress string) {
span.remoteAddr = remoteAddress
}
func (span *span) SetEndPoint(endPoint string) {
span.endPoint = endPoint
}
func (span *span) SetAcceptorHost(host string) {
span.acceptorHost = host
}
func (span *span) Annotations() Annotation {
return &span.annotations
}
func (span *span) SetLogging(logInfo int32) {
span.loggingInfo = logInfo
}