tracing.go 6.4 KB

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