apm_tracing.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package tracing
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "github.com/coroot/coroot-node-agent/ebpftracer/l7"
  7. "go.opentelemetry.io/otel/attribute"
  8. "go.opentelemetry.io/otel/codes"
  9. semconv "go.opentelemetry.io/otel/semconv/v1.18.0"
  10. "go.opentelemetry.io/otel/trace"
  11. "inet.af/netaddr"
  12. )
  13. /**
  14. * Trace
  15. */
  16. func (t *Trace) setContext(ctx context.Context) {
  17. t.lock.Lock()
  18. defer t.lock.Unlock()
  19. t.ctx = ctx
  20. }
  21. func (t *Trace) setSpan(span trace.Span) {
  22. t.lock.Lock()
  23. defer t.lock.Unlock()
  24. t.span = span
  25. }
  26. func (t *Trace) TraceStart(method, path string, status l7.Status, duration time.Duration) {
  27. if t == nil || method == "" {
  28. return
  29. }
  30. t.createParentSpan("APPLICATION", duration, status >= 400,
  31. semconv.HTTPURL(fmt.Sprintf("http://%s%s", t.destination.String(), path)),
  32. semconv.HTTPMethod(method),
  33. //semconv.HTTPStatusCode(int(status)),
  34. attribute.String("http.uri", path),
  35. )
  36. }
  37. func (t *Trace) TraceEnd(r *l7.RequestData) {
  38. if t == nil {
  39. return
  40. }
  41. t.span.SetAttributes(semconv.HTTPStatusCode(int(r.Status)))
  42. t.span.End(trace.WithTimestamp(time.Now()))
  43. }
  44. func (t *Trace) createParentSpan(name string, duration time.Duration, error bool, attrs ...attribute.KeyValue) {
  45. end := time.Now()
  46. start := end.Add(-duration)
  47. ctx, span := tracer(t.containerId).Start(context.Background(), name, trace.WithTimestamp(start), trace.WithSpanKind(trace.SpanKindClient))
  48. span.SetAttributes(attrs...)
  49. span.SetAttributes(t.commonAttrs...)
  50. if error {
  51. span.SetStatus(codes.Error, "")
  52. }
  53. t.setContext(ctx)
  54. t.setSpan(span)
  55. }
  56. func (t *Trace) createTraceSpan(name string, duration time.Duration, error bool, attrs ...attribute.KeyValue) {
  57. end := time.Now()
  58. start := end.Add(-duration)
  59. //fmt.Println("createTraceSpan:", t.ctx)
  60. _, span := tracer(t.containerId).Start(t.ctx, name, trace.WithTimestamp(start), trace.WithSpanKind(trace.SpanKindClient))
  61. span.SetAttributes(t.commonAttrs...)
  62. span.SetAttributes(attrs...)
  63. if error {
  64. span.SetStatus(codes.Error, "")
  65. }
  66. span.End(trace.WithTimestamp(end))
  67. }
  68. func (t *Trace) MysqlTraceQuery(query string, error bool, duration time.Duration, destination netaddr.IPPort) {
  69. if t == nil || query == "" {
  70. return
  71. }
  72. t.createTraceSpan(l7.ProtocolMysql.String(), duration, error,
  73. semconv.DBSystemMySQL,
  74. semconv.DBStatement(query),
  75. semconv.NetPeerName(destination.IP().String()),
  76. semconv.NetPeerPort(int(destination.Port())),
  77. )
  78. }
  79. func (t *Trace) RedisTraceQuery(cmd, args string, error bool, duration time.Duration) {
  80. if t == nil || cmd == "" {
  81. return
  82. }
  83. statement := cmd
  84. if args != "" {
  85. statement += " " + args
  86. }
  87. t.createTraceSpan(l7.ProtocolRedis.String(), duration, error,
  88. semconv.DBSystemRedis,
  89. semconv.DBOperation(cmd),
  90. semconv.DBStatement(statement),
  91. )
  92. }
  93. func (t *Trace) HttpTraceRequest(method, path, ip string, port uint16, status l7.Status, duration time.Duration) {
  94. if t == nil || method == "" {
  95. return
  96. }
  97. t.createTraceSpan(l7.ProtocolHTTP.String(), duration, status >= 400,
  98. semconv.HTTPURL(fmt.Sprintf("http://%s%s", t.destination.String(), path)),
  99. semconv.HTTPMethod(method),
  100. semconv.HTTPStatusCode(int(status)),
  101. attribute.String("http.uri", path),
  102. attribute.String("http.ip", ip),
  103. attribute.Int("http.port", int(port)),
  104. )
  105. }
  106. func (t *Trace) FuncTraceQuery(funcname string, duration time.Duration, level int, pid int, nid int) {
  107. if t == nil || funcname == "" {
  108. return
  109. }
  110. t.createTraceSpan(funcname, duration, false,
  111. attribute.Int("level", level),
  112. attribute.Int("pid", pid),
  113. attribute.Int("nid", nid),
  114. )
  115. }