apm_tracing.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. package tracing
  2. import (
  3. "context"
  4. "fmt"
  5. "sort"
  6. "time"
  7. "strconv"
  8. "github.com/coroot/coroot-node-agent/ebpftracer"
  9. "github.com/coroot/coroot-node-agent/ebpftracer/l7"
  10. "go.opentelemetry.io/otel/attribute"
  11. "go.opentelemetry.io/otel/codes"
  12. semconv "go.opentelemetry.io/otel/semconv/v1.18.0"
  13. "go.opentelemetry.io/otel/trace"
  14. "inet.af/netaddr"
  15. )
  16. /**
  17. * Trace
  18. */
  19. func (t *Trace) setContext(ctx context.Context) {
  20. t.lock.Lock()
  21. defer t.lock.Unlock()
  22. t.ctx = ctx
  23. }
  24. func (t *Trace) setSpan(span trace.Span) {
  25. t.lock.Lock()
  26. defer t.lock.Unlock()
  27. t.span = span
  28. }
  29. type TimeMap struct {
  30. Time uint64
  31. Type int
  32. Map *ebpftracer.StackFunEvent
  33. }
  34. func (t *Trace) buildFun() {
  35. mapSlice := []TimeMap{}
  36. for i, v := range t.stack {
  37. timeStartMap := TimeMap{}
  38. if v.StackEvent.Location == 0 {
  39. timeStartMap = TimeMap{
  40. Time: v.StackEvent.TimeNsStart,
  41. Type: 0,
  42. Map: &t.stack[i],
  43. }
  44. } else {
  45. timeStartMap = TimeMap{
  46. Time: v.StackEvent.TimeNsEnd,
  47. Type: 1,
  48. Map: &t.stack[i],
  49. }
  50. }
  51. mapSlice = append(mapSlice, timeStartMap)
  52. }
  53. sort.Slice(mapSlice, func(i, j int) bool {
  54. return mapSlice[i].Time < mapSlice[j].Time
  55. })
  56. funStack := []TimeMap{}
  57. currentfunNum := 1
  58. // for k, v := range mapSlice {
  59. // fmt.Println("---SliceSliceindex", k, "value", v.Time, v.Type, v.Map.Uprobe.Funcname, v.Map.StackEvent.Nid)
  60. // }
  61. mapSliceLen := len(mapSlice)
  62. for k, v := range mapSlice {
  63. // fmt.Println("SliceSliceindex", k, "value", v.Time, v.Type, v.Map.Uprobe.Funcname, v.Map.StackEvent.Nid)
  64. if v.Type == 0 {
  65. // 函数入口
  66. funStack = append(funStack, v)
  67. } else if v.Type == 1 {
  68. // 函数出口
  69. len := len(funStack)
  70. if len < 1 {
  71. fmt.Printf("buildFun ErrorError return before enter: %v\n", v)
  72. continue
  73. }
  74. currnt := funStack[len-1]
  75. if currnt.Map.StackEvent.Location != 0 {
  76. fmt.Printf("currnt StackEvent ErrorError is not enter: %v\n", v)
  77. continue
  78. }
  79. if k < mapSliceLen-1 && len >= 2 {
  80. nextfun := mapSlice[k+1]
  81. preCurrnt := funStack[len-2]
  82. // // 处理 .netcore 多次 returun
  83. // 下一个事件是 return 并且函数名跟当前事件是一样的,且上一个函数不是当前函数
  84. if nextfun.Map.StackEvent.Location == 1 && nextfun.Map.Uprobe.Funcname == currnt.Map.Uprobe.Funcname && preCurrnt.Map.Uprobe.Funcname != currnt.Map.Uprobe.Funcname {
  85. currentfunNum++
  86. continue
  87. }
  88. }
  89. funStack = funStack[:len-1]
  90. duration := v.Map.StackEvent.TimeNsEnd - currnt.Map.StackEvent.TimeNsStart
  91. t.FuncTraceQuery(currnt.Map.Uprobe.Funcname, time.Duration(duration), currnt.Map.StackEvent.TimeNsStart, v.Map.StackEvent.TimeNsEnd, currentfunNum)
  92. currentfunNum = 1
  93. }
  94. }
  95. }
  96. func (t *Trace) TraceStart(method, path string, status l7.Status, duration time.Duration) {
  97. if t == nil || method == "" {
  98. return
  99. }
  100. t.createParentSpan("APPLICATION", duration, status >= 400,
  101. semconv.HTTPURL(fmt.Sprintf("http://%s%s", t.destination.String(), path)),
  102. semconv.HTTPMethod(method),
  103. //semconv.HTTPStatusCode(int(status)),
  104. attribute.String("http.uri", path),
  105. )
  106. }
  107. func (t *Trace) TraceEnd(r *l7.RequestData) {
  108. if t == nil {
  109. return
  110. }
  111. t.span.SetAttributes(
  112. semconv.HTTPStatusCode(int(r.Status)),
  113. attribute.String("server.trace_id_from", r.ParentSpanContext.TraceIdFrom),
  114. )
  115. CalledId, err := strconv.ParseInt(r.ParentSpanContext.CalledId, 10, 64)
  116. if err == nil && CalledId != 0 {
  117. t.span.SetAttributes(attribute.Int64("server.called_id", CalledId))
  118. }
  119. InstanceIdFrom, err := strconv.ParseInt(r.ParentSpanContext.InstanceIdFrom, 10, 64)
  120. if err == nil && InstanceIdFrom != 0 {
  121. t.span.SetAttributes(attribute.Int64("server.instance_id_from", InstanceIdFrom))
  122. }
  123. AppIdFrom, err := strconv.ParseInt(r.ParentSpanContext.AppIdFrom, 10, 64)
  124. if err == nil && AppIdFrom != 0 {
  125. t.span.SetAttributes(attribute.Int64("server.app_id_from", AppIdFrom))
  126. }
  127. if r.ParentSpanContext.SpanIdFrom != "0000000000000000" {
  128. t.span.SetAttributes(attribute.String("server.span_id_from", r.ParentSpanContext.SpanIdFrom))
  129. }
  130. // for _, v := range t.stack {
  131. // fmt.Printf("TraceEndTraceEndTraceEnd%s\n", v)
  132. // }
  133. t.buildFun()
  134. t.span.End(trace.WithTimestamp(time.Now()))
  135. }
  136. func (t *Trace) createParentSpan(name string, duration time.Duration, error bool, attrs ...attribute.KeyValue) {
  137. end := time.Now()
  138. start := end.Add(-duration)
  139. ctx, span := tracer(t.containerId).Start(context.Background(), name, trace.WithTimestamp(start), trace.WithSpanKind(trace.SpanKindClient))
  140. span.SetAttributes(attrs...)
  141. span.SetAttributes(t.commonAttrs...)
  142. if error {
  143. span.SetStatus(codes.Error, "")
  144. }
  145. t.setContext(ctx)
  146. t.setSpan(span)
  147. }
  148. func (t *Trace) createTraceSpan(name string, duration time.Duration, error bool, attrs ...attribute.KeyValue) {
  149. end := time.Now()
  150. start := end.Add(-duration)
  151. //fmt.Println("createTraceSpan:", t.ctx)
  152. _, span := tracer(t.containerId).Start(t.ctx, name, trace.WithTimestamp(start), trace.WithSpanKind(trace.SpanKindClient))
  153. span.SetAttributes(t.commonAttrs...)
  154. span.SetAttributes(attrs...)
  155. if error {
  156. span.SetStatus(codes.Error, "")
  157. }
  158. span.End(trace.WithTimestamp(end))
  159. }
  160. func (t *Trace) MysqlTraceQuery(query string, error bool, duration time.Duration, destination netaddr.IPPort) {
  161. if t == nil || query == "" {
  162. return
  163. }
  164. t.createTraceSpan(l7.ProtocolMysql.String(), duration, error,
  165. semconv.DBSystemMySQL,
  166. semconv.DBStatement(query),
  167. semconv.NetPeerName(destination.IP().String()),
  168. semconv.NetPeerPort(int(destination.Port())),
  169. )
  170. }
  171. func (t *Trace) RedisTraceQuery(cmd, args string, error bool, duration time.Duration) {
  172. if t == nil || cmd == "" {
  173. return
  174. }
  175. statement := cmd
  176. if args != "" {
  177. statement += " " + args
  178. }
  179. t.createTraceSpan(l7.ProtocolRedis.String(), duration, error,
  180. semconv.DBSystemRedis,
  181. semconv.DBOperation(cmd),
  182. semconv.DBStatement(statement),
  183. )
  184. }
  185. func (t *Trace) HttpTraceRequest(method, path, ip string, port uint16, r *l7.RequestData) {
  186. if t == nil || method == "" {
  187. return
  188. }
  189. assumedAppID, err := strconv.ParseInt(r.AssumedAppId, 10, 64)
  190. if err != nil {
  191. assumedAppID = 0
  192. }
  193. status := r.Status
  194. duration := r.Duration
  195. t.createTraceSpan(l7.ProtocolHTTP.String(), duration, status >= 400,
  196. semconv.HTTPURL(fmt.Sprintf("http://%s%s", t.destination.String(), path)),
  197. semconv.HTTPMethod(method),
  198. semconv.HTTPStatusCode(int(status)),
  199. attribute.String("http.uri", path),
  200. attribute.String("http.ip", ip),
  201. attribute.Int64("http.assumed_app_id", assumedAppID),
  202. attribute.String("http.span_id", r.SpanId),
  203. attribute.Int("http.port", int(port)),
  204. )
  205. }
  206. func (t *Trace) FuncTraceQuery(funcname string, duration time.Duration, start uint64, end uint64, num int) {
  207. if t == nil || funcname == "" {
  208. return
  209. }
  210. t.createTraceSpanNoTime(funcname, duration, false, start, end, attribute.Int("num", num))
  211. }
  212. func (t *Trace) createTraceSpanNoTime(name string, duration time.Duration, error bool, start uint64, end uint64, attrs ...attribute.KeyValue) {
  213. // end := time.Now()
  214. // start := end.Add(-duration)
  215. startTime := time.Unix(0, int64(start))
  216. endTime := time.Unix(0, int64(end))
  217. //fmt.Println("createTraceSpan:", t.ctx)
  218. _, span := tracer(t.containerId).Start(t.ctx, name, trace.WithTimestamp(startTime), trace.WithSpanKind(trace.SpanKindClient))
  219. span.SetAttributes(t.commonAttrs...)
  220. span.SetAttributes(attrs...)
  221. if error {
  222. span.SetStatus(codes.Error, "")
  223. }
  224. span.End(trace.WithTimestamp(endTime))
  225. }