forked from pinpoint-apm/pinpoint-go-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
span_event.go
132 lines (108 loc) · 2.86 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
package pinpoint
import (
"time"
)
type spanEvent struct {
parentSpan *span
serviceType int32
sequence int32
depth int32
startTime time.Time
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
}
var asyncApiId = int32(0)
func defaultSpanEvent(span *span, operationName string) *spanEvent {
se := spanEvent{}
se.parentSpan = span
se.startTime = time.Now()
se.startElapsed = se.startTime.Sub(span.startTime).Milliseconds()
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, se.startTime)
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().Sub(se.startTime).Milliseconds()
}
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.parentSpan.agent.cacheError(errName)
se.errorFuncId = id
se.errorString = e.Error()
}
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()
id := se.parentSpan.agent.cacheSql(nsql)
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
se.startElapsed = start.Sub(se.parentSpan.startTime).Milliseconds()
se.endElapsed = end.Sub(start).Milliseconds()
se.isTimeFixed = true
}