-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathspan_event.go
158 lines (130 loc) · 3.38 KB
/
span_event.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
package pinpoint
import (
"time"
)
type spanEvent struct {
parentSpan *span
serviceType int32
sequence int32
depth int32
startTime int64
startElapsed int64
endElapsed int64
operationName string
nextSpanId int64
annotations annotation
endPoint string
destinationId string
errorFuncId int32
errorString string
asyncId int32
asyncSeqGen int32
apiId int32
isTimeFixed bool
exceptionId int64
}
var (
asyncApiId int32 = 0
exceptionIdGen int64 = 0
)
func defaultSpanEvent(span *span, operationName string) *spanEvent {
se := spanEvent{}
se.parentSpan = span
se.startTime = time.Now().UnixMilli()
se.startElapsed = 0
se.sequence = span.eventSequence
se.depth = span.eventDepth
se.operationName = operationName
se.endPoint = ""
se.asyncId = noneAsyncId
se.asyncSeqGen = 0
se.serviceType = ServiceTypeGoFunction
se.isTimeFixed = false
Log("span").Tracef("newSpanEvent: %s, %d, %d, %s", se.operationName, se.sequence, se.depth, time.Now())
return &se
}
func newSpanEvent(span *span, operationName string) *spanEvent {
se := defaultSpanEvent(span, operationName)
se.apiId = span.agent.cacheSpanApi(operationName, apiTypeDefault)
return se
}
func newSpanEventGoroutine(span *span) *spanEvent {
se := defaultSpanEvent(span, "")
//Asynchronous Invocation
if asyncApiId == 0 {
asyncApiId = span.agent.cacheSpanApi("Goroutine Invocation", apiTypeInvocation)
}
se.apiId = asyncApiId
se.serviceType = ServiceTypeAsync
return se
}
func (se *spanEvent) end() {
se.parentSpan.eventDepth--
if !se.isTimeFixed {
se.endElapsed = time.Now().UnixMilli() - se.startTime
}
Log("span").Tracef("endSpanEvent: %s", se.operationName)
}
func (se *spanEvent) generateNextSpanId() int64 {
se.nextSpanId = generateSpanId()
return se.nextSpanId
}
func (se *spanEvent) SetError(e error, errorName ...string) {
if e == nil {
return
}
var errName string
if len(errorName) > 0 {
errName = errorName[0]
} else {
errName = "error"
}
id := se.agent().cacheError(errName)
se.errorFuncId = id
se.errorString = e.Error()
cfg := se.config()
if cfg.errorTraceCallStack && se.parentSpan.canAddErrorChain() {
se.exceptionId = se.parentSpan.traceCallStack(e, cfg.errorCallStackDepth)
se.Annotations().AppendLong(AnnotationExceptionChainId, se.exceptionId)
}
}
func (se *spanEvent) SetServiceType(typ int32) {
se.serviceType = typ
}
func (se *spanEvent) SetDestination(id string) {
se.destinationId = id
}
func (se *spanEvent) SetEndPoint(endPoint string) {
se.endPoint = endPoint
}
func (se *spanEvent) SetSQL(sql string, args string) {
if sql == "" {
return
}
normalizer := newSqlNormalizer(sql)
nsql, param := normalizer.run()
agent := se.agent()
if se.config().sqlTraceQueryStat {
if id := agent.cacheSqlUid(nsql); id != nil {
se.annotations.AppendBytesStringString(AnnotationSqlUid, id, param, args)
}
} else {
if id := agent.cacheSql(nsql); id != 0 {
se.annotations.AppendIntStringString(AnnotationSqlId, id, param, args)
}
}
}
func (se *spanEvent) Annotations() Annotation {
return &se.annotations
}
func (se *spanEvent) FixDuration(start time.Time, end time.Time) {
se.startTime = start.UnixMilli()
se.endElapsed = end.UnixMilli() - se.startTime
se.isTimeFixed = true
}
func (se *spanEvent) agent() *agent {
return se.parentSpan.agent
}
func (se *spanEvent) config() *Config {
return se.agent().config
}