tracing.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. package tracing
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/coroot/coroot-node-agent/common"
  6. "github.com/coroot/coroot-node-agent/ebpftracer/l7"
  7. "github.com/coroot/coroot-node-agent/flags"
  8. "go.opentelemetry.io/otel/attribute"
  9. "go.opentelemetry.io/otel/codes"
  10. "go.opentelemetry.io/otel/exporters/otlp/otlptrace"
  11. "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp"
  12. "go.opentelemetry.io/otel/sdk/resource"
  13. sdktrace "go.opentelemetry.io/otel/sdk/trace"
  14. semconv "go.opentelemetry.io/otel/semconv/v1.18.0"
  15. "go.opentelemetry.io/otel/trace"
  16. "inet.af/netaddr"
  17. "k8s.io/klog/v2"
  18. "sync"
  19. "time"
  20. )
  21. const (
  22. MemcacheDBItemKeyName attribute.Key = "db.memcached.item"
  23. )
  24. var (
  25. tracer func(containerId string) trace.Tracer
  26. )
  27. func Init(machineId, hostname, version string) {
  28. endpointUrl := *flags.TracesEndpoint
  29. if endpointUrl == nil {
  30. klog.Infoln("no OpenTelemetry traces collector endpoint configured")
  31. return
  32. }
  33. klog.Infoln("OpenTelemetry traces collector endpoint:", endpointUrl.String())
  34. path := endpointUrl.Path
  35. if path == "" {
  36. path = "/"
  37. }
  38. opts := []otlptracehttp.Option{
  39. otlptracehttp.WithEndpoint(endpointUrl.Host),
  40. otlptracehttp.WithURLPath(path),
  41. otlptracehttp.WithHeaders(common.AuthHeaders()),
  42. }
  43. if endpointUrl.Scheme != "https" {
  44. opts = append(opts, otlptracehttp.WithInsecure())
  45. }
  46. client := otlptracehttp.NewClient(opts...)
  47. exporter, err := otlptrace.New(context.Background(), client)
  48. if err != nil {
  49. klog.Exitln(err)
  50. }
  51. batcher := sdktrace.WithBatcher(exporter)
  52. tracer = func(containerId string) trace.Tracer {
  53. provider := sdktrace.NewTracerProvider(
  54. batcher,
  55. sdktrace.WithResource(resource.NewWithAttributes(
  56. semconv.SchemaURL,
  57. semconv.HostName(hostname),
  58. semconv.HostID(machineId),
  59. semconv.ServiceName(common.ContainerIdToOtelServiceName(containerId)),
  60. semconv.ContainerID(containerId),
  61. )),
  62. )
  63. return provider.Tracer("coroot-node-agent", trace.WithInstrumentationVersion(version))
  64. }
  65. }
  66. type Trace struct {
  67. containerId string
  68. destination netaddr.IPPort
  69. commonAttrs []attribute.KeyValue
  70. ctx context.Context
  71. span trace.Span
  72. lock sync.RWMutex
  73. currenEventCount *uint32
  74. needEventCount uint32
  75. startEventReady bool
  76. endEventReady bool
  77. createAt time.Time
  78. }
  79. func NewTraceFromEvent(containerId string) *Trace {
  80. if tracer == nil {
  81. return nil
  82. }
  83. var currenEventCount uint32
  84. return &Trace{containerId: containerId, currenEventCount: &currenEventCount}
  85. }
  86. func NewTrace(containerId string, destination netaddr.IPPort) *Trace {
  87. if tracer == nil {
  88. return nil
  89. }
  90. return &Trace{containerId: containerId, destination: destination, commonAttrs: []attribute.KeyValue{
  91. semconv.NetPeerName(destination.IP().String()),
  92. semconv.NetPeerPort(int(destination.Port())),
  93. }}
  94. }
  95. func (t *Trace) createSpan(name string, duration time.Duration, error bool, attrs ...attribute.KeyValue) {
  96. end := time.Now()
  97. start := end.Add(-duration)
  98. _, span := tracer(t.containerId).Start(nil, name, trace.WithTimestamp(start), trace.WithSpanKind(trace.SpanKindClient))
  99. span.SetAttributes(attrs...)
  100. span.SetAttributes(t.commonAttrs...)
  101. if error {
  102. span.SetStatus(codes.Error, "")
  103. }
  104. span.End(trace.WithTimestamp(end))
  105. }
  106. func (t *Trace) HttpRequest(method, path string, status l7.Status, duration time.Duration) {
  107. if t == nil || method == "" {
  108. return
  109. }
  110. t.createSpan(method, duration, status >= 400,
  111. semconv.HTTPURL(fmt.Sprintf("http://%s%s", t.destination.String(), path)),
  112. semconv.HTTPMethod(method),
  113. semconv.HTTPStatusCode(int(status)),
  114. )
  115. }
  116. func (t *Trace) Http2Request(method, path, scheme string, status l7.Status, duration time.Duration) {
  117. if t == nil {
  118. return
  119. }
  120. if method == "" {
  121. method = "unknown"
  122. }
  123. if path == "" {
  124. path = "/unknown"
  125. }
  126. if scheme == "" {
  127. scheme = "unknown"
  128. }
  129. t.createSpan(method, duration, status > 400,
  130. semconv.HTTPURL(fmt.Sprintf("%s://%s%s", scheme, t.destination.String(), path)),
  131. semconv.HTTPMethod(method),
  132. semconv.HTTPStatusCode(int(status)),
  133. )
  134. }
  135. func (t *Trace) PostgresQuery(query string, error bool, duration time.Duration) {
  136. if t == nil || query == "" {
  137. return
  138. }
  139. t.createSpan("query", duration, error,
  140. semconv.DBSystemPostgreSQL,
  141. semconv.DBStatement(query),
  142. )
  143. }
  144. func (t *Trace) MysqlQuery(query string, error bool, duration time.Duration) {
  145. if t == nil || query == "" {
  146. return
  147. }
  148. t.createSpan("query", duration, error,
  149. semconv.DBSystemMySQL,
  150. semconv.DBStatement(query),
  151. )
  152. }
  153. func (t *Trace) MongoQuery(query string, error bool, duration time.Duration) {
  154. if t == nil || query == "" {
  155. return
  156. }
  157. t.createSpan("query", duration, error,
  158. semconv.DBSystemMongoDB,
  159. semconv.DBStatement(query),
  160. )
  161. }
  162. func (t *Trace) MemcachedQuery(cmd string, items []string, error bool, duration time.Duration) {
  163. if t == nil || cmd == "" {
  164. return
  165. }
  166. attrs := []attribute.KeyValue{
  167. semconv.DBSystemMemcached,
  168. semconv.DBOperation(cmd),
  169. }
  170. if len(items) == 1 {
  171. attrs = append(attrs, MemcacheDBItemKeyName.String(items[0]))
  172. } else if len(items) > 1 {
  173. attrs = append(attrs, MemcacheDBItemKeyName.StringSlice(items))
  174. }
  175. t.createSpan(cmd, duration, error, attrs...)
  176. }
  177. func (t *Trace) RedisQuery(cmd, args string, error bool, duration time.Duration) {
  178. if t == nil || cmd == "" {
  179. return
  180. }
  181. statement := cmd
  182. if args != "" {
  183. statement += " " + args
  184. }
  185. t.createSpan(cmd, duration, error,
  186. semconv.DBSystemRedis,
  187. semconv.DBOperation(cmd),
  188. semconv.DBStatement(statement),
  189. )
  190. }