Debian Codegraph @

sdk/trace/internal/observ/tracer.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package observ // import "go.opentelemetry.io/otel/sdk/trace/internal/observ"
5
6 import (
7 "context"
8 "errors"
9 "fmt"
10
11 "go.opentelemetry.io/otel"
12 "go.opentelemetry.io/otel/attribute"
13 "go.opentelemetry.io/otel/metric"
14 "go.opentelemetry.io/otel/sdk"
15 "go.opentelemetry.io/otel/sdk/internal/x"
16 "go.opentelemetry.io/otel/semconv/v1.40.0/otelconv"
17 "go.opentelemetry.io/otel/trace"
18 )
19
20 var meterOpts = []metric.MeterOption{
21 metric.WithInstrumentationVersion(sdk.Version()),
22 metric.WithSchemaURL(SchemaURL),
23 }
24
25 // Tracer is instrumentation for an OTel SDK Tracer.
26 type Tracer struct {
27 enabled bool
28
29 live metric.Int64UpDownCounter
30 started metric.Int64Counter
31 }
32
33 func NewTracer() (Tracer, error) {
34 if !x.Observability.Enabled() {
35 return Tracer{}, nil
36 }
37 meter := otel.GetMeterProvider().Meter(ScopeName, meterOpts...)
38
39 var err error
40 l, e := otelconv.NewSDKSpanLive(meter)
41 if e != nil {
42 e = fmt.Errorf("failed to create span live metric: %w", e)
43 err = errors.Join(err, e)
44 }
45
46 s, e := otelconv.NewSDKSpanStarted(meter)
47 if e != nil {
48 e = fmt.Errorf("failed to create span started metric: %w", e)
49 err = errors.Join(err, e)
50 }
51
52 return Tracer{enabled: true, live: l.Inst(), started: s.Inst()}, err
53 }
54
55 func (t Tracer) Enabled() bool { return t.enabled }
56
57 func (t Tracer) SpanStarted(ctx context.Context, psc trace.SpanContext, span trace.Span) {
58 if !t.started.Enabled(ctx) {
59 return
60 }
61
62 key := spanStartedKey{
63 parent: parentStateNoParent,
64 sampling: samplingStateDrop,
65 }
66
67 if psc.IsValid() {
68 if psc.IsRemote() {
69 key.parent = parentStateRemoteParent
70 } else {
71 key.parent = parentStateLocalParent
72 }
73 }
74
75 if span.IsRecording() {
76 if span.SpanContext().IsSampled() {
77 key.sampling = samplingStateRecordAndSample
78 } else {
79 key.sampling = samplingStateRecordOnly
80 }
81 }
82
83 opts := spanStartedOpts[key]
84 t.started.Add(ctx, 1, opts...)
85 }
86
87 func (t Tracer) SpanLive(ctx context.Context, span trace.Span) {
88 t.spanLive(ctx, 1, span)
89 }
90
91 func (t Tracer) SpanEnded(ctx context.Context, span trace.Span) {
92 t.spanLive(ctx, -1, span)
93 }
94
95 func (t Tracer) spanLive(ctx context.Context, value int64, span trace.Span) {
96 if !t.live.Enabled(ctx) {
97 return
98 }
99
100 key := spanLiveKey{sampled: span.SpanContext().IsSampled()}
101 opts := spanLiveOpts[key]
102 t.live.Add(ctx, value, opts...)
103 }
104
105 type parentState int
106
107 const (
108 parentStateNoParent parentState = iota
109 parentStateLocalParent
110 parentStateRemoteParent
111 )
112
113 type samplingState int
114
115 const (
116 samplingStateDrop samplingState = iota
117 samplingStateRecordOnly
118 samplingStateRecordAndSample
119 )
120
121 type spanStartedKey struct {
122 parent parentState
123 sampling samplingState
124 }
125
126 var spanStartedOpts = map[spanStartedKey][]metric.AddOption{
127 {
128 parentStateNoParent,
129 samplingStateDrop,
130 }: {
131 metric.WithAttributeSet(attribute.NewSet(
132 otelconv.SDKSpanStarted{}.AttrSpanParentOrigin(otelconv.SpanParentOriginNone),
133 otelconv.SDKSpanStarted{}.AttrSpanSamplingResult(otelconv.SpanSamplingResultDrop),
134 )),
135 },
136 {
137 parentStateLocalParent,
138 samplingStateDrop,
139 }: {
140 metric.WithAttributeSet(attribute.NewSet(
141 otelconv.SDKSpanStarted{}.AttrSpanParentOrigin(otelconv.SpanParentOriginLocal),
142 otelconv.SDKSpanStarted{}.AttrSpanSamplingResult(otelconv.SpanSamplingResultDrop),
143 )),
144 },
145 {
146 parentStateRemoteParent,
147 samplingStateDrop,
148 }: {
149 metric.WithAttributeSet(attribute.NewSet(
150 otelconv.SDKSpanStarted{}.AttrSpanParentOrigin(otelconv.SpanParentOriginRemote),
151 otelconv.SDKSpanStarted{}.AttrSpanSamplingResult(otelconv.SpanSamplingResultDrop),
152 )),
153 },
154
155 {
156 parentStateNoParent,
157 samplingStateRecordOnly,
158 }: {
159 metric.WithAttributeSet(attribute.NewSet(
160 otelconv.SDKSpanStarted{}.AttrSpanParentOrigin(otelconv.SpanParentOriginNone),
161 otelconv.SDKSpanStarted{}.AttrSpanSamplingResult(otelconv.SpanSamplingResultRecordOnly),
162 )),
163 },
164 {
165 parentStateLocalParent,
166 samplingStateRecordOnly,
167 }: {
168 metric.WithAttributeSet(attribute.NewSet(
169 otelconv.SDKSpanStarted{}.AttrSpanParentOrigin(otelconv.SpanParentOriginLocal),
170 otelconv.SDKSpanStarted{}.AttrSpanSamplingResult(otelconv.SpanSamplingResultRecordOnly),
171 )),
172 },
173 {
174 parentStateRemoteParent,
175 samplingStateRecordOnly,
176 }: {
177 metric.WithAttributeSet(attribute.NewSet(
178 otelconv.SDKSpanStarted{}.AttrSpanParentOrigin(otelconv.SpanParentOriginRemote),
179 otelconv.SDKSpanStarted{}.AttrSpanSamplingResult(otelconv.SpanSamplingResultRecordOnly),
180 )),
181 },
182
183 {
184 parentStateNoParent,
185 samplingStateRecordAndSample,
186 }: {
187 metric.WithAttributeSet(attribute.NewSet(
188 otelconv.SDKSpanStarted{}.AttrSpanParentOrigin(otelconv.SpanParentOriginNone),
189 otelconv.SDKSpanStarted{}.AttrSpanSamplingResult(otelconv.SpanSamplingResultRecordAndSample),
190 )),
191 },
192 {
193 parentStateLocalParent,
194 samplingStateRecordAndSample,
195 }: {
196 metric.WithAttributeSet(attribute.NewSet(
197 otelconv.SDKSpanStarted{}.AttrSpanParentOrigin(otelconv.SpanParentOriginLocal),
198 otelconv.SDKSpanStarted{}.AttrSpanSamplingResult(otelconv.SpanSamplingResultRecordAndSample),
199 )),
200 },
201 {
202 parentStateRemoteParent,
203 samplingStateRecordAndSample,
204 }: {
205 metric.WithAttributeSet(attribute.NewSet(
206 otelconv.SDKSpanStarted{}.AttrSpanParentOrigin(otelconv.SpanParentOriginRemote),
207 otelconv.SDKSpanStarted{}.AttrSpanSamplingResult(otelconv.SpanSamplingResultRecordAndSample),
208 )),
209 },
210 }
211
212 type spanLiveKey struct {
213 sampled bool
214 }
215
216 var spanLiveOpts = map[spanLiveKey][]metric.AddOption{
217 {true}: {
218 metric.WithAttributeSet(attribute.NewSet(
219 otelconv.SDKSpanLive{}.AttrSpanSamplingResult(
220 otelconv.SpanSamplingResultRecordAndSample,
221 ),
222 )),
223 },
224 {false}: {
225 metric.WithAttributeSet(attribute.NewSet(
226 otelconv.SDKSpanLive{}.AttrSpanSamplingResult(
227 otelconv.SpanSamplingResultRecordOnly,
228 ),
229 )),
230 },
231 }