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