Debian Codegraph @

sdk/trace/tracer.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package trace // import "go.opentelemetry.io/otel/sdk/trace"
5
6 import (
7 "context"
8 "time"
9
10 "go.opentelemetry.io/otel/sdk/instrumentation"
11 "go.opentelemetry.io/otel/sdk/trace/internal/observ"
12 "go.opentelemetry.io/otel/trace"
13 "go.opentelemetry.io/otel/trace/embedded"
14 )
15
16 type tracer struct {
17 embedded.Tracer
18
19 provider *TracerProvider
20 instrumentationScope instrumentation.Scope
21
22 inst observ.Tracer
23 }
24
25 var _ trace.Tracer = &tracer{}
26
27 // Start starts a Span and returns it along with a context containing it.
28 //
29 // The Span is created with the provided name and as a child of any existing
30 // span context found in the passed context. The created Span will be
31 // configured appropriately by any SpanOption passed.
32 func (tr *tracer) Start(
33 ctx context.Context,
34 name string,
35 options ...trace.SpanStartOption,
36 ) (context.Context, trace.Span) {
37 config := trace.NewSpanStartConfig(options...)
38
39 if ctx == nil {
40 // Prevent trace.ContextWithSpan from panicking.
41 ctx = context.Background()
42 }
43
44 // For local spans created by this SDK, track child span count.
45 if p := trace.SpanFromContext(ctx); p != nil {
46 if sdkSpan, ok := p.(*recordingSpan); ok {
47 sdkSpan.addChild()
48 }
49 }
50
51 s := tr.newSpan(ctx, name, &config)
52 newCtx := trace.ContextWithSpan(ctx, s)
53 if tr.inst.Enabled() {
54 if o, ok := s.(interface{ setOrigCtx(context.Context) }); ok {
55 // If this is a recording span, store the original context.
56 // This allows later retrieval of baggage and other information
57 // that may have been stored in the context at span start time and
58 // to avoid the allocation of repeatedly calling
59 // trace.ContextWithSpan.
60 o.setOrigCtx(newCtx)
61 }
62 psc := trace.SpanContextFromContext(ctx)
63 tr.inst.SpanStarted(newCtx, psc, s)
64 }
65
66 if rw, ok := s.(ReadWriteSpan); ok && s.IsRecording() {
67 sps := tr.provider.getSpanProcessors()
68 for _, sp := range sps {
69 // Use original context.
70 sp.sp.OnStart(ctx, rw)
71 }
72 }
73 if rtt, ok := s.(runtimeTracer); ok {
74 newCtx = rtt.runtimeTrace(newCtx)
75 }
76
77 return newCtx, s
78 }
79
80 type runtimeTracer interface {
81 // runtimeTrace starts a "runtime/trace".Task for the span and
82 // returns a context containing the task.
83 runtimeTrace(ctx context.Context) context.Context
84 }
85
86 // newSpan returns a new configured span.
87 func (tr *tracer) newSpan(ctx context.Context, name string, config *trace.SpanConfig) trace.Span {
88 // If told explicitly to make this a new root use a zero value SpanContext
89 // as a parent which contains an invalid trace ID and is not remote.
90 var psc trace.SpanContext
91 if config.NewRoot() {
92 ctx = trace.ContextWithSpanContext(ctx, psc)
93 } else {
94 psc = trace.SpanContextFromContext(ctx)
95 }
96
97 // If there is a valid parent trace ID, use it to ensure the continuity of
98 // the trace. Always generate a new span ID so other components can rely
99 // on a unique span ID, even if the Span is non-recording.
100 var tid trace.TraceID
101 var sid trace.SpanID
102 if !psc.TraceID().IsValid() {
103 tid, sid = tr.provider.idGenerator.NewIDs(ctx)
104 } else {
105 tid = psc.TraceID()
106 sid = tr.provider.idGenerator.NewSpanID(ctx, tid)
107 }
108
109 samplingResult := tr.provider.sampler.ShouldSample(SamplingParameters{
110 ParentContext: ctx,
111 TraceID: tid,
112 Name: name,
113 Kind: config.SpanKind(),
114 Attributes: config.Attributes(),
115 Links: config.Links(),
116 })
117
118 scc := trace.SpanContextConfig{
119 TraceID: tid,
120 SpanID: sid,
121 TraceState: samplingResult.Tracestate,
122 }
123 if isSampled(samplingResult) {
124 scc.TraceFlags = psc.TraceFlags() | trace.FlagsSampled
125 } else {
126 scc.TraceFlags = psc.TraceFlags() &^ trace.FlagsSampled
127 }
128 sc := trace.NewSpanContext(scc)
129
130 if !isRecording(samplingResult) {
131 return tr.newNonRecordingSpan(sc)
132 }
133 return tr.newRecordingSpan(ctx, psc, sc, name, samplingResult, config)
134 }
135
136 // newRecordingSpan returns a new configured recordingSpan.
137 func (tr *tracer) newRecordingSpan(
138 ctx context.Context,
139 psc, sc trace.SpanContext,
140 name string,
141 sr SamplingResult,
142 config *trace.SpanConfig,
143 ) *recordingSpan {
144 startTime := config.Timestamp()
145 if startTime.IsZero() {
146 startTime = time.Now()
147 }
148
149 s := &recordingSpan{
150 // Do not pre-allocate the attributes slice here! Doing so will
151 // allocate memory that is likely never going to be used, or if used,
152 // will be over-sized. The default Go compiler has been tested to
153 // dynamically allocate needed space very well. Benchmarking has shown
154 // it to be more performant than what we can predetermine here,
155 // especially for the common use case of few to no added
156 // attributes.
157
158 parent: psc,
159 spanContext: sc,
160 spanKind: trace.ValidateSpanKind(config.SpanKind()),
161 name: name,
162 startTime: startTime,
163 events: newEvictedQueueEvent(tr.provider.spanLimits.EventCountLimit),
164 links: newEvictedQueueLink(tr.provider.spanLimits.LinkCountLimit),
165 tracer: tr,
166 }
167
168 for _, l := range config.Links() {
169 s.AddLink(l)
170 }
171
172 s.SetAttributes(sr.Attributes...)
173 s.SetAttributes(config.Attributes()...)
174
175 if tr.inst.Enabled() {
176 // Propagate any existing values from the context with the new span to
177 // the measurement context.
178 ctx = trace.ContextWithSpan(ctx, s)
179 tr.inst.SpanLive(ctx, s)
180 }
181
182 return s
183 }
184
185 // newNonRecordingSpan returns a new configured nonRecordingSpan.
186 func (tr *tracer) newNonRecordingSpan(sc trace.SpanContext) nonRecordingSpan {
187 return nonRecordingSpan{tracer: tr, sc: sc}
188 }