github.com/pinpoint-apm/pinpoint-go-agent@v1.4.1-0.20240110120318-a50c2eb18c8c/span_event.go (about) 1 package pinpoint 2 3 import ( 4 "time" 5 ) 6 7 type spanEvent struct { 8 parentSpan *span 9 serviceType int32 10 sequence int32 11 depth int32 12 startTime time.Time 13 startElapsed int64 14 endElapsed int64 15 operationName string 16 nextSpanId int64 17 annotations annotation 18 endPoint string 19 destinationId string 20 errorFuncId int32 21 errorString string 22 asyncId int32 23 asyncSeqGen int32 24 apiId int32 25 isTimeFixed bool 26 exceptionId int64 27 } 28 29 var ( 30 asyncApiId int32 = 0 31 exceptionIdGen int64 = 0 32 ) 33 34 func defaultSpanEvent(span *span, operationName string) *spanEvent { 35 se := spanEvent{} 36 37 se.parentSpan = span 38 se.startTime = time.Now() 39 se.startElapsed = se.startTime.Sub(span.startTime).Milliseconds() 40 se.sequence = span.eventSequence 41 se.depth = span.eventDepth 42 se.operationName = operationName 43 se.endPoint = "" 44 se.asyncId = noneAsyncId 45 se.asyncSeqGen = 0 46 se.serviceType = ServiceTypeGoFunction 47 se.isTimeFixed = false 48 49 Log("span").Tracef("newSpanEvent: %s, %d, %d, %s", se.operationName, se.sequence, se.depth, se.startTime) 50 51 return &se 52 } 53 54 func newSpanEvent(span *span, operationName string) *spanEvent { 55 se := defaultSpanEvent(span, operationName) 56 se.apiId = span.agent.cacheSpanApi(operationName, apiTypeDefault) 57 58 return se 59 } 60 61 func newSpanEventGoroutine(span *span) *spanEvent { 62 se := defaultSpanEvent(span, "") 63 64 //Asynchronous Invocation 65 if asyncApiId == 0 { 66 asyncApiId = span.agent.cacheSpanApi("Goroutine Invocation", apiTypeInvocation) 67 } 68 se.apiId = asyncApiId 69 se.serviceType = ServiceTypeAsync 70 71 return se 72 } 73 74 func (se *spanEvent) end() { 75 se.parentSpan.eventDepth-- 76 if !se.isTimeFixed { 77 se.endElapsed = time.Now().Sub(se.startTime).Milliseconds() 78 } 79 Log("span").Tracef("endSpanEvent: %s", se.operationName) 80 } 81 82 func (se *spanEvent) generateNextSpanId() int64 { 83 se.nextSpanId = generateSpanId() 84 return se.nextSpanId 85 } 86 87 func (se *spanEvent) SetError(e error, errorName ...string) { 88 if e == nil { 89 return 90 } 91 92 var errName string 93 if len(errorName) > 0 { 94 errName = errorName[0] 95 } else { 96 errName = "error" 97 } 98 99 id := se.agent().cacheError(errName) 100 se.errorFuncId = id 101 se.errorString = e.Error() 102 103 cfg := se.config() 104 if cfg.errorTraceCallStack { 105 se.exceptionId = se.parentSpan.traceCallStack(e, cfg.errorCallStackDepth) 106 se.Annotations().AppendLong(AnnotationExceptionChainId, se.exceptionId) 107 } 108 } 109 110 func (se *spanEvent) SetServiceType(typ int32) { 111 se.serviceType = typ 112 } 113 114 func (se *spanEvent) SetDestination(id string) { 115 se.destinationId = id 116 } 117 118 func (se *spanEvent) SetEndPoint(endPoint string) { 119 se.endPoint = endPoint 120 } 121 122 func (se *spanEvent) SetSQL(sql string, args string) { 123 if sql == "" { 124 return 125 } 126 127 normalizer := newSqlNormalizer(sql) 128 nsql, param := normalizer.run() 129 130 agent := se.agent() 131 if se.config().sqlTraceQueryStat { 132 if id := agent.cacheSqlUid(nsql); id != nil { 133 se.annotations.AppendBytesStringString(AnnotationSqlUid, id, param, args) 134 } 135 } else { 136 if id := agent.cacheSql(nsql); id != 0 { 137 se.annotations.AppendIntStringString(AnnotationSqlId, id, param, args) 138 } 139 } 140 } 141 142 func (se *spanEvent) Annotations() Annotation { 143 return &se.annotations 144 } 145 146 func (se *spanEvent) FixDuration(start time.Time, end time.Time) { 147 se.startTime = start 148 se.startElapsed = start.Sub(se.parentSpan.startTime).Milliseconds() 149 se.endElapsed = end.Sub(start).Milliseconds() 150 se.isTimeFixed = true 151 } 152 153 func (se *spanEvent) agent() *agent { 154 return se.parentSpan.agent 155 } 156 157 func (se *spanEvent) config() *Config { 158 return se.agent().config 159 }