tracing.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. }
  74. func NewTrace(containerId string, destination netaddr.IPPort) *Trace {
  75. if tracer == nil {
  76. return nil
  77. }
  78. return &Trace{containerId: containerId, destination: destination, commonAttrs: []attribute.KeyValue{
  79. semconv.NetPeerName(destination.IP().String()),
  80. semconv.NetPeerPort(int(destination.Port())),
  81. }}
  82. }
  83. func (t *Trace) createSpan(name string, duration time.Duration, error bool, attrs ...attribute.KeyValue) {
  84. end := time.Now()
  85. start := end.Add(-duration)
  86. _, span := tracer(t.containerId).Start(nil, name, trace.WithTimestamp(start), trace.WithSpanKind(trace.SpanKindClient))
  87. span.SetAttributes(attrs...)
  88. span.SetAttributes(t.commonAttrs...)
  89. if error {
  90. span.SetStatus(codes.Error, "")
  91. }
  92. span.End(trace.WithTimestamp(end))
  93. }
  94. func (t *Trace) HttpRequest(method, path string, status l7.Status, duration time.Duration) {
  95. if t == nil || method == "" {
  96. return
  97. }
  98. t.createSpan(method, duration, status >= 400,
  99. semconv.HTTPURL(fmt.Sprintf("http://%s%s", t.destination.String(), path)),
  100. semconv.HTTPMethod(method),
  101. semconv.HTTPStatusCode(int(status)),
  102. )
  103. }
  104. func (t *Trace) Http2Request(method, path, scheme string, status l7.Status, duration time.Duration) {
  105. if t == nil {
  106. return
  107. }
  108. if method == "" {
  109. method = "unknown"
  110. }
  111. if path == "" {
  112. path = "/unknown"
  113. }
  114. if scheme == "" {
  115. scheme = "unknown"
  116. }
  117. t.createSpan(method, duration, status > 400,
  118. semconv.HTTPURL(fmt.Sprintf("%s://%s%s", scheme, t.destination.String(), path)),
  119. semconv.HTTPMethod(method),
  120. semconv.HTTPStatusCode(int(status)),
  121. )
  122. }
  123. func (t *Trace) PostgresQuery(query string, error bool, duration time.Duration) {
  124. if t == nil || query == "" {
  125. return
  126. }
  127. t.createSpan("query", duration, error,
  128. semconv.DBSystemPostgreSQL,
  129. semconv.DBStatement(query),
  130. )
  131. }
  132. func (t *Trace) MysqlQuery(query string, error bool, duration time.Duration) {
  133. if t == nil || query == "" {
  134. return
  135. }
  136. t.createSpan("query", duration, error,
  137. semconv.DBSystemMySQL,
  138. semconv.DBStatement(query),
  139. )
  140. }
  141. func (t *Trace) MongoQuery(query string, error bool, duration time.Duration) {
  142. if t == nil || query == "" {
  143. return
  144. }
  145. t.createSpan("query", duration, error,
  146. semconv.DBSystemMongoDB,
  147. semconv.DBStatement(query),
  148. )
  149. }
  150. func (t *Trace) MemcachedQuery(cmd string, items []string, error bool, duration time.Duration) {
  151. if t == nil || cmd == "" {
  152. return
  153. }
  154. attrs := []attribute.KeyValue{
  155. semconv.DBSystemMemcached,
  156. semconv.DBOperation(cmd),
  157. }
  158. if len(items) == 1 {
  159. attrs = append(attrs, MemcacheDBItemKeyName.String(items[0]))
  160. } else if len(items) > 1 {
  161. attrs = append(attrs, MemcacheDBItemKeyName.StringSlice(items))
  162. }
  163. t.createSpan(cmd, duration, error, attrs...)
  164. }
  165. func (t *Trace) RedisQuery(cmd, args string, error bool, duration time.Duration) {
  166. if t == nil || cmd == "" {
  167. return
  168. }
  169. statement := cmd
  170. if args != "" {
  171. statement += " " + args
  172. }
  173. t.createSpan(cmd, duration, error,
  174. semconv.DBSystemRedis,
  175. semconv.DBOperation(cmd),
  176. semconv.DBStatement(statement),
  177. )
  178. }
  179. /**
  180. * Trace
  181. */
  182. func (t *Trace) setContext(ctx context.Context) {
  183. t.lock.Lock()
  184. defer t.lock.Unlock()
  185. t.ctx = ctx
  186. }
  187. func (t *Trace) setSpan(span trace.Span) {
  188. t.lock.Lock()
  189. defer t.lock.Unlock()
  190. t.span = span
  191. }
  192. func (t *Trace) TraceStart(method, path string, status l7.Status, duration time.Duration) {
  193. if t == nil || method == "" {
  194. return
  195. }
  196. t.createParentSpan("APPLICATION", duration, status >= 400,
  197. semconv.HTTPURL(fmt.Sprintf("http://%s%s", t.destination.String(), path)),
  198. semconv.HTTPMethod(method),
  199. //semconv.HTTPStatusCode(int(status)),
  200. attribute.String("http.uri", path),
  201. )
  202. }
  203. func (t *Trace) TraceEnd(r *l7.RequestData) {
  204. if t == nil {
  205. return
  206. }
  207. t.span.SetAttributes(semconv.HTTPStatusCode(int(r.Status)))
  208. t.span.End(trace.WithTimestamp(time.Now()))
  209. }
  210. func (t *Trace) createParentSpan(name string, duration time.Duration, error bool, attrs ...attribute.KeyValue) {
  211. end := time.Now()
  212. start := end.Add(-duration)
  213. ctx, span := tracer(t.containerId).Start(context.Background(), name, trace.WithTimestamp(start), trace.WithSpanKind(trace.SpanKindClient))
  214. span.SetAttributes(attrs...)
  215. span.SetAttributes(t.commonAttrs...)
  216. if error {
  217. span.SetStatus(codes.Error, "")
  218. }
  219. t.setContext(ctx)
  220. t.setSpan(span)
  221. }
  222. func (t *Trace) MysqlTraceQuery(query string, error bool, duration time.Duration, destination netaddr.IPPort) {
  223. if t == nil || query == "" {
  224. return
  225. }
  226. t.createTraceSpan("MYSQL", duration, error,
  227. semconv.DBSystemMySQL,
  228. semconv.DBStatement(query),
  229. semconv.NetPeerName(destination.IP().String()),
  230. semconv.NetPeerPort(int(destination.Port())),
  231. )
  232. }
  233. func (t *Trace) createTraceSpan(name string, duration time.Duration, error bool, attrs ...attribute.KeyValue) {
  234. end := time.Now()
  235. start := end.Add(-duration)
  236. fmt.Println("createTraceSpan:", t.ctx)
  237. _, span := tracer(t.containerId).Start(t.ctx, name, trace.WithTimestamp(start), trace.WithSpanKind(trace.SpanKindClient))
  238. span.SetAttributes(t.commonAttrs...)
  239. span.SetAttributes(attrs...)
  240. if error {
  241. span.SetStatus(codes.Error, "")
  242. }
  243. span.End(trace.WithTimestamp(end))
  244. }