auto.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package trace // import "go.opentelemetry.io/otel/trace"
  4. import (
  5. "context"
  6. "encoding/json"
  7. "fmt"
  8. "math"
  9. "os"
  10. "reflect"
  11. "runtime"
  12. "strconv"
  13. "strings"
  14. "sync"
  15. "sync/atomic"
  16. "time"
  17. "unicode/utf8"
  18. "go.opentelemetry.io/otel/attribute"
  19. "go.opentelemetry.io/otel/codes"
  20. semconv "go.opentelemetry.io/otel/semconv/v1.26.0"
  21. "go.opentelemetry.io/otel/trace/embedded"
  22. "go.opentelemetry.io/otel/trace/internal/telemetry"
  23. )
  24. // newAutoTracerProvider returns an auto-instrumentable [trace.TracerProvider].
  25. // If an [go.opentelemetry.io/auto.Instrumentation] is configured to instrument
  26. // the process using the returned TracerProvider, all of the telemetry it
  27. // produces will be processed and handled by that Instrumentation. By default,
  28. // if no Instrumentation instruments the TracerProvider it will not generate
  29. // any trace telemetry.
  30. func newAutoTracerProvider() TracerProvider { return tracerProviderInstance }
  31. var tracerProviderInstance = new(autoTracerProvider)
  32. type autoTracerProvider struct{ embedded.TracerProvider }
  33. var _ TracerProvider = autoTracerProvider{}
  34. func (p autoTracerProvider) Tracer(name string, opts ...TracerOption) Tracer {
  35. cfg := NewTracerConfig(opts...)
  36. return autoTracer{
  37. name: name,
  38. version: cfg.InstrumentationVersion(),
  39. schemaURL: cfg.SchemaURL(),
  40. }
  41. }
  42. type autoTracer struct {
  43. embedded.Tracer
  44. name, schemaURL, version string
  45. }
  46. var _ Tracer = autoTracer{}
  47. func (t autoTracer) Start(ctx context.Context, name string, opts ...SpanStartOption) (context.Context, Span) {
  48. var psc SpanContext
  49. sampled := true
  50. span := new(autoSpan)
  51. // Ask eBPF for sampling decision and span context info.
  52. t.start(ctx, span, &psc, &sampled, &span.spanContext)
  53. span.sampled.Store(sampled)
  54. ctx = ContextWithSpan(ctx, span)
  55. if sampled {
  56. // Only build traces if sampled.
  57. cfg := NewSpanStartConfig(opts...)
  58. span.traces, span.span = t.traces(name, cfg, span.spanContext, psc)
  59. }
  60. return ctx, span
  61. }
  62. // Expected to be implemented in eBPF.
  63. //
  64. //go:noinline
  65. func (t *autoTracer) start(
  66. ctx context.Context,
  67. spanPtr *autoSpan,
  68. psc *SpanContext,
  69. sampled *bool,
  70. sc *SpanContext,
  71. ) {
  72. start(ctx, spanPtr, psc, sampled, sc)
  73. }
  74. // start is used for testing.
  75. var start = func(context.Context, *autoSpan, *SpanContext, *bool, *SpanContext) {}
  76. func (t autoTracer) traces(name string, cfg SpanConfig, sc, psc SpanContext) (*telemetry.Traces, *telemetry.Span) {
  77. span := &telemetry.Span{
  78. TraceID: telemetry.TraceID(sc.TraceID()),
  79. SpanID: telemetry.SpanID(sc.SpanID()),
  80. Flags: uint32(sc.TraceFlags()),
  81. TraceState: sc.TraceState().String(),
  82. ParentSpanID: telemetry.SpanID(psc.SpanID()),
  83. Name: name,
  84. Kind: spanKind(cfg.SpanKind()),
  85. }
  86. span.Attrs, span.DroppedAttrs = convCappedAttrs(maxSpan.Attrs, cfg.Attributes())
  87. links := cfg.Links()
  88. if limit := maxSpan.Links; limit == 0 {
  89. n := int64(len(links))
  90. if n > 0 {
  91. span.DroppedLinks = uint32(min(n, math.MaxUint32)) // nolint: gosec // Bounds checked.
  92. }
  93. } else {
  94. if limit > 0 {
  95. n := int64(max(len(links)-limit, 0))
  96. span.DroppedLinks = uint32(min(n, math.MaxUint32)) // nolint: gosec // Bounds checked.
  97. links = links[n:]
  98. }
  99. span.Links = convLinks(links)
  100. }
  101. if t := cfg.Timestamp(); !t.IsZero() {
  102. span.StartTime = cfg.Timestamp()
  103. } else {
  104. span.StartTime = time.Now()
  105. }
  106. return &telemetry.Traces{
  107. ResourceSpans: []*telemetry.ResourceSpans{
  108. {
  109. ScopeSpans: []*telemetry.ScopeSpans{
  110. {
  111. Scope: &telemetry.Scope{
  112. Name: t.name,
  113. Version: t.version,
  114. },
  115. Spans: []*telemetry.Span{span},
  116. SchemaURL: t.schemaURL,
  117. },
  118. },
  119. },
  120. },
  121. }, span
  122. }
  123. func spanKind(kind SpanKind) telemetry.SpanKind {
  124. switch kind {
  125. case SpanKindInternal:
  126. return telemetry.SpanKindInternal
  127. case SpanKindServer:
  128. return telemetry.SpanKindServer
  129. case SpanKindClient:
  130. return telemetry.SpanKindClient
  131. case SpanKindProducer:
  132. return telemetry.SpanKindProducer
  133. case SpanKindConsumer:
  134. return telemetry.SpanKindConsumer
  135. }
  136. return telemetry.SpanKind(0) // undefined.
  137. }
  138. type autoSpan struct {
  139. embedded.Span
  140. spanContext SpanContext
  141. sampled atomic.Bool
  142. mu sync.Mutex
  143. traces *telemetry.Traces
  144. span *telemetry.Span
  145. }
  146. func (s *autoSpan) SpanContext() SpanContext {
  147. if s == nil {
  148. return SpanContext{}
  149. }
  150. // s.spanContext is immutable, do not acquire lock s.mu.
  151. return s.spanContext
  152. }
  153. func (s *autoSpan) IsRecording() bool {
  154. if s == nil {
  155. return false
  156. }
  157. return s.sampled.Load()
  158. }
  159. func (s *autoSpan) SetStatus(c codes.Code, msg string) {
  160. if s == nil || !s.sampled.Load() {
  161. return
  162. }
  163. s.mu.Lock()
  164. defer s.mu.Unlock()
  165. if s.span.Status == nil {
  166. s.span.Status = new(telemetry.Status)
  167. }
  168. s.span.Status.Message = msg
  169. switch c {
  170. case codes.Unset:
  171. s.span.Status.Code = telemetry.StatusCodeUnset
  172. case codes.Error:
  173. s.span.Status.Code = telemetry.StatusCodeError
  174. case codes.Ok:
  175. s.span.Status.Code = telemetry.StatusCodeOK
  176. }
  177. }
  178. func (s *autoSpan) SetAttributes(attrs ...attribute.KeyValue) {
  179. if s == nil || !s.sampled.Load() {
  180. return
  181. }
  182. s.mu.Lock()
  183. defer s.mu.Unlock()
  184. limit := maxSpan.Attrs
  185. if limit == 0 {
  186. // No attributes allowed.
  187. n := int64(len(attrs))
  188. if n > 0 {
  189. s.span.DroppedAttrs += uint32(min(n, math.MaxUint32)) // nolint: gosec // Bounds checked.
  190. }
  191. return
  192. }
  193. m := make(map[string]int)
  194. for i, a := range s.span.Attrs {
  195. m[a.Key] = i
  196. }
  197. for _, a := range attrs {
  198. val := convAttrValue(a.Value)
  199. if val.Empty() {
  200. s.span.DroppedAttrs++
  201. continue
  202. }
  203. if idx, ok := m[string(a.Key)]; ok {
  204. s.span.Attrs[idx] = telemetry.Attr{
  205. Key: string(a.Key),
  206. Value: val,
  207. }
  208. } else if limit < 0 || len(s.span.Attrs) < limit {
  209. s.span.Attrs = append(s.span.Attrs, telemetry.Attr{
  210. Key: string(a.Key),
  211. Value: val,
  212. })
  213. m[string(a.Key)] = len(s.span.Attrs) - 1
  214. } else {
  215. s.span.DroppedAttrs++
  216. }
  217. }
  218. }
  219. // convCappedAttrs converts up to limit attrs into a []telemetry.Attr. The
  220. // number of dropped attributes is also returned.
  221. func convCappedAttrs(limit int, attrs []attribute.KeyValue) ([]telemetry.Attr, uint32) {
  222. n := len(attrs)
  223. if limit == 0 {
  224. var out uint32
  225. if n > 0 {
  226. out = uint32(min(int64(n), math.MaxUint32)) // nolint: gosec // Bounds checked.
  227. }
  228. return nil, out
  229. }
  230. if limit < 0 {
  231. // Unlimited.
  232. return convAttrs(attrs), 0
  233. }
  234. if n < 0 {
  235. n = 0
  236. }
  237. limit = min(n, limit)
  238. return convAttrs(attrs[:limit]), uint32(n - limit) // nolint: gosec // Bounds checked.
  239. }
  240. func convAttrs(attrs []attribute.KeyValue) []telemetry.Attr {
  241. if len(attrs) == 0 {
  242. // Avoid allocations if not necessary.
  243. return nil
  244. }
  245. out := make([]telemetry.Attr, 0, len(attrs))
  246. for _, attr := range attrs {
  247. key := string(attr.Key)
  248. val := convAttrValue(attr.Value)
  249. if val.Empty() {
  250. continue
  251. }
  252. out = append(out, telemetry.Attr{Key: key, Value: val})
  253. }
  254. return out
  255. }
  256. func convAttrValue(value attribute.Value) telemetry.Value {
  257. switch value.Type() {
  258. case attribute.BOOL:
  259. return telemetry.BoolValue(value.AsBool())
  260. case attribute.INT64:
  261. return telemetry.Int64Value(value.AsInt64())
  262. case attribute.FLOAT64:
  263. return telemetry.Float64Value(value.AsFloat64())
  264. case attribute.STRING:
  265. v := truncate(maxSpan.AttrValueLen, value.AsString())
  266. return telemetry.StringValue(v)
  267. case attribute.BOOLSLICE:
  268. slice := value.AsBoolSlice()
  269. out := make([]telemetry.Value, 0, len(slice))
  270. for _, v := range slice {
  271. out = append(out, telemetry.BoolValue(v))
  272. }
  273. return telemetry.SliceValue(out...)
  274. case attribute.INT64SLICE:
  275. slice := value.AsInt64Slice()
  276. out := make([]telemetry.Value, 0, len(slice))
  277. for _, v := range slice {
  278. out = append(out, telemetry.Int64Value(v))
  279. }
  280. return telemetry.SliceValue(out...)
  281. case attribute.FLOAT64SLICE:
  282. slice := value.AsFloat64Slice()
  283. out := make([]telemetry.Value, 0, len(slice))
  284. for _, v := range slice {
  285. out = append(out, telemetry.Float64Value(v))
  286. }
  287. return telemetry.SliceValue(out...)
  288. case attribute.STRINGSLICE:
  289. slice := value.AsStringSlice()
  290. out := make([]telemetry.Value, 0, len(slice))
  291. for _, v := range slice {
  292. v = truncate(maxSpan.AttrValueLen, v)
  293. out = append(out, telemetry.StringValue(v))
  294. }
  295. return telemetry.SliceValue(out...)
  296. }
  297. return telemetry.Value{}
  298. }
  299. // truncate returns a truncated version of s such that it contains less than
  300. // the limit number of characters. Truncation is applied by returning the limit
  301. // number of valid characters contained in s.
  302. //
  303. // If limit is negative, it returns the original string.
  304. //
  305. // UTF-8 is supported. When truncating, all invalid characters are dropped
  306. // before applying truncation.
  307. //
  308. // If s already contains less than the limit number of bytes, it is returned
  309. // unchanged. No invalid characters are removed.
  310. func truncate(limit int, s string) string {
  311. // This prioritize performance in the following order based on the most
  312. // common expected use-cases.
  313. //
  314. // - Short values less than the default limit (128).
  315. // - Strings with valid encodings that exceed the limit.
  316. // - No limit.
  317. // - Strings with invalid encodings that exceed the limit.
  318. if limit < 0 || len(s) <= limit {
  319. return s
  320. }
  321. // Optimistically, assume all valid UTF-8.
  322. var b strings.Builder
  323. count := 0
  324. for i, c := range s {
  325. if c != utf8.RuneError {
  326. count++
  327. if count > limit {
  328. return s[:i]
  329. }
  330. continue
  331. }
  332. _, size := utf8.DecodeRuneInString(s[i:])
  333. if size == 1 {
  334. // Invalid encoding.
  335. b.Grow(len(s) - 1)
  336. _, _ = b.WriteString(s[:i])
  337. s = s[i:]
  338. break
  339. }
  340. }
  341. // Fast-path, no invalid input.
  342. if b.Cap() == 0 {
  343. return s
  344. }
  345. // Truncate while validating UTF-8.
  346. for i := 0; i < len(s) && count < limit; {
  347. c := s[i]
  348. if c < utf8.RuneSelf {
  349. // Optimization for single byte runes (common case).
  350. _ = b.WriteByte(c)
  351. i++
  352. count++
  353. continue
  354. }
  355. _, size := utf8.DecodeRuneInString(s[i:])
  356. if size == 1 {
  357. // We checked for all 1-byte runes above, this is a RuneError.
  358. i++
  359. continue
  360. }
  361. _, _ = b.WriteString(s[i : i+size])
  362. i += size
  363. count++
  364. }
  365. return b.String()
  366. }
  367. func (s *autoSpan) End(opts ...SpanEndOption) {
  368. if s == nil || !s.sampled.Swap(false) {
  369. return
  370. }
  371. // s.end exists so the lock (s.mu) is not held while s.ended is called.
  372. s.ended(s.end(opts))
  373. }
  374. func (s *autoSpan) end(opts []SpanEndOption) []byte {
  375. s.mu.Lock()
  376. defer s.mu.Unlock()
  377. cfg := NewSpanEndConfig(opts...)
  378. if t := cfg.Timestamp(); !t.IsZero() {
  379. s.span.EndTime = cfg.Timestamp()
  380. } else {
  381. s.span.EndTime = time.Now()
  382. }
  383. b, _ := json.Marshal(s.traces) // TODO: do not ignore this error.
  384. return b
  385. }
  386. // Expected to be implemented in eBPF.
  387. //
  388. //go:noinline
  389. func (*autoSpan) ended(buf []byte) { ended(buf) }
  390. // ended is used for testing.
  391. var ended = func([]byte) {}
  392. func (s *autoSpan) RecordError(err error, opts ...EventOption) {
  393. if s == nil || err == nil || !s.sampled.Load() {
  394. return
  395. }
  396. cfg := NewEventConfig(opts...)
  397. attrs := cfg.Attributes()
  398. attrs = append(attrs,
  399. semconv.ExceptionType(typeStr(err)),
  400. semconv.ExceptionMessage(err.Error()),
  401. )
  402. if cfg.StackTrace() {
  403. buf := make([]byte, 2048)
  404. n := runtime.Stack(buf, false)
  405. attrs = append(attrs, semconv.ExceptionStacktrace(string(buf[0:n])))
  406. }
  407. s.mu.Lock()
  408. defer s.mu.Unlock()
  409. s.addEvent(semconv.ExceptionEventName, cfg.Timestamp(), attrs)
  410. }
  411. func typeStr(i any) string {
  412. t := reflect.TypeOf(i)
  413. if t.PkgPath() == "" && t.Name() == "" {
  414. // Likely a builtin type.
  415. return t.String()
  416. }
  417. return fmt.Sprintf("%s.%s", t.PkgPath(), t.Name())
  418. }
  419. func (s *autoSpan) AddEvent(name string, opts ...EventOption) {
  420. if s == nil || !s.sampled.Load() {
  421. return
  422. }
  423. cfg := NewEventConfig(opts...)
  424. s.mu.Lock()
  425. defer s.mu.Unlock()
  426. s.addEvent(name, cfg.Timestamp(), cfg.Attributes())
  427. }
  428. // addEvent adds an event with name and attrs at tStamp to the span. The span
  429. // lock (s.mu) needs to be held by the caller.
  430. func (s *autoSpan) addEvent(name string, tStamp time.Time, attrs []attribute.KeyValue) {
  431. limit := maxSpan.Events
  432. if limit == 0 {
  433. s.span.DroppedEvents++
  434. return
  435. }
  436. if limit > 0 && len(s.span.Events) == limit {
  437. // Drop head while avoiding allocation of more capacity.
  438. copy(s.span.Events[:limit-1], s.span.Events[1:])
  439. s.span.Events = s.span.Events[:limit-1]
  440. s.span.DroppedEvents++
  441. }
  442. e := &telemetry.SpanEvent{Time: tStamp, Name: name}
  443. e.Attrs, e.DroppedAttrs = convCappedAttrs(maxSpan.EventAttrs, attrs)
  444. s.span.Events = append(s.span.Events, e)
  445. }
  446. func (s *autoSpan) AddLink(link Link) {
  447. if s == nil || !s.sampled.Load() {
  448. return
  449. }
  450. l := maxSpan.Links
  451. s.mu.Lock()
  452. defer s.mu.Unlock()
  453. if l == 0 {
  454. s.span.DroppedLinks++
  455. return
  456. }
  457. if l > 0 && len(s.span.Links) == l {
  458. // Drop head while avoiding allocation of more capacity.
  459. copy(s.span.Links[:l-1], s.span.Links[1:])
  460. s.span.Links = s.span.Links[:l-1]
  461. s.span.DroppedLinks++
  462. }
  463. s.span.Links = append(s.span.Links, convLink(link))
  464. }
  465. func convLinks(links []Link) []*telemetry.SpanLink {
  466. out := make([]*telemetry.SpanLink, 0, len(links))
  467. for _, link := range links {
  468. out = append(out, convLink(link))
  469. }
  470. return out
  471. }
  472. func convLink(link Link) *telemetry.SpanLink {
  473. l := &telemetry.SpanLink{
  474. TraceID: telemetry.TraceID(link.SpanContext.TraceID()),
  475. SpanID: telemetry.SpanID(link.SpanContext.SpanID()),
  476. TraceState: link.SpanContext.TraceState().String(),
  477. Flags: uint32(link.SpanContext.TraceFlags()),
  478. }
  479. l.Attrs, l.DroppedAttrs = convCappedAttrs(maxSpan.LinkAttrs, link.Attributes)
  480. return l
  481. }
  482. func (s *autoSpan) SetName(name string) {
  483. if s == nil || !s.sampled.Load() {
  484. return
  485. }
  486. s.mu.Lock()
  487. defer s.mu.Unlock()
  488. s.span.Name = name
  489. }
  490. func (*autoSpan) TracerProvider() TracerProvider { return newAutoTracerProvider() }
  491. // maxSpan are the span limits resolved during startup.
  492. var maxSpan = newSpanLimits()
  493. type spanLimits struct {
  494. // Attrs is the number of allowed attributes for a span.
  495. //
  496. // This is resolved from the environment variable value for the
  497. // OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT key if it exists. Otherwise, the
  498. // environment variable value for OTEL_ATTRIBUTE_COUNT_LIMIT, or 128 if
  499. // that is not set, is used.
  500. Attrs int
  501. // AttrValueLen is the maximum attribute value length allowed for a span.
  502. //
  503. // This is resolved from the environment variable value for the
  504. // OTEL_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT key if it exists. Otherwise, the
  505. // environment variable value for OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT, or -1
  506. // if that is not set, is used.
  507. AttrValueLen int
  508. // Events is the number of allowed events for a span.
  509. //
  510. // This is resolved from the environment variable value for the
  511. // OTEL_SPAN_EVENT_COUNT_LIMIT key, or 128 is used if that is not set.
  512. Events int
  513. // EventAttrs is the number of allowed attributes for a span event.
  514. //
  515. // The is resolved from the environment variable value for the
  516. // OTEL_EVENT_ATTRIBUTE_COUNT_LIMIT key, or 128 is used if that is not set.
  517. EventAttrs int
  518. // Links is the number of allowed Links for a span.
  519. //
  520. // This is resolved from the environment variable value for the
  521. // OTEL_SPAN_LINK_COUNT_LIMIT, or 128 is used if that is not set.
  522. Links int
  523. // LinkAttrs is the number of allowed attributes for a span link.
  524. //
  525. // This is resolved from the environment variable value for the
  526. // OTEL_LINK_ATTRIBUTE_COUNT_LIMIT, or 128 is used if that is not set.
  527. LinkAttrs int
  528. }
  529. func newSpanLimits() spanLimits {
  530. return spanLimits{
  531. Attrs: firstEnv(
  532. 128,
  533. "OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT",
  534. "OTEL_ATTRIBUTE_COUNT_LIMIT",
  535. ),
  536. AttrValueLen: firstEnv(
  537. -1, // Unlimited.
  538. "OTEL_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT",
  539. "OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT",
  540. ),
  541. Events: firstEnv(128, "OTEL_SPAN_EVENT_COUNT_LIMIT"),
  542. EventAttrs: firstEnv(128, "OTEL_EVENT_ATTRIBUTE_COUNT_LIMIT"),
  543. Links: firstEnv(128, "OTEL_SPAN_LINK_COUNT_LIMIT"),
  544. LinkAttrs: firstEnv(128, "OTEL_LINK_ATTRIBUTE_COUNT_LIMIT"),
  545. }
  546. }
  547. // firstEnv returns the parsed integer value of the first matching environment
  548. // variable from keys. The defaultVal is returned if the value is not an
  549. // integer or no match is found.
  550. func firstEnv(defaultVal int, keys ...string) int {
  551. for _, key := range keys {
  552. strV := os.Getenv(key)
  553. if strV == "" {
  554. continue
  555. }
  556. v, err := strconv.Atoi(strV)
  557. if err == nil {
  558. return v
  559. }
  560. // Ignore invalid environment variable.
  561. }
  562. return defaultVal
  563. }