You've already forked opentelemetry-go
mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2025-09-16 09:26:25 +02:00
Encapsulate SDK Tracer observability (#7331)
Split from #7316
[Follow
guidelines](a5dcd68ebb/CONTRIBUTING.md (encapsulation)
)
and move instrumentation into its own type.
### Benchmarks
#### Added `sdk/trace/internal/observ` benchmarks
```
goos: linux
goarch: amd64
pkg: go.opentelemetry.io/otel/sdk/trace/internal/observ
cpu: Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz
│ enc-trace-sdk-tracer-obs.out │
│ sec/op │
Tracer/SpanStarted-8 7.436n ± 6%
Tracer/SpanLive-8 9.987n ± 8%
Tracer/SpanEnded-8 11.32n ± 7%
NewTracer-8 87.64n ± 6%
geomean 16.48n
│ enc-trace-sdk-tracer-obs.out │
│ B/op │
Tracer/SpanStarted-8 0.000 ± 0%
Tracer/SpanLive-8 0.000 ± 0%
Tracer/SpanEnded-8 0.000 ± 0%
NewTracer-8 0.000 ± 0%
geomean ¹
¹ summaries must be >0 to compute geomean
│ enc-trace-sdk-tracer-obs.out │
│ allocs/op │
Tracer/SpanStarted-8 0.000 ± 0%
Tracer/SpanLive-8 0.000 ± 0%
Tracer/SpanEnded-8 0.000 ± 0%
NewTracer-8 0.000 ± 0%
geomean ¹
¹ summaries must be >0 to compute geomean
```
#### Existing `sdk/trace` benchmarks
```console
> benchstat main.out enc-trace-sdk-tracer-obs.out
goos: linux
goarch: amd64
pkg: go.opentelemetry.io/otel/sdk/trace
cpu: Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz
│ main.out │ enc-trace-sdk-tracer-obs.out │
│ sec/op │ sec/op vs base │
SpanEnd/ObservabilityEnabled-8 188.5n ± 4% 131.5n ± 32% -30.24% (p=0.000 n=10)
TraceStart/ObservabilityEnabled-8 886.9n ± 8% 663.9n ± 2% -25.14% (p=0.000 n=10)
geomean 408.9n 295.5n -27.73%
│ main.out │ enc-trace-sdk-tracer-obs.out │
│ B/op │ B/op vs base │
SpanEnd/ObservabilityEnabled-8 16.00 ± 0% 0.00 ± 0% -100.00% (p=0.000 n=10)
TraceStart/ObservabilityEnabled-8 608.0 ± 0% 576.0 ± 0% -5.26% (p=0.000 n=10)
geomean 98.63 ? ¹ ²
¹ summaries must be >0 to compute geomean
² ratios must be >0 to compute geomean
│ main.out │ enc-trace-sdk-tracer-obs.out │
│ allocs/op │ allocs/op vs base │
SpanEnd/ObservabilityEnabled-8 1.000 ± 0% 0.000 ± 0% -100.00% (p=0.000 n=10)
TraceStart/ObservabilityEnabled-8 5.000 ± 0% 3.000 ± 0% -40.00% (p=0.000 n=10)
geomean 2.236 ? ¹ ²
¹ summaries must be >0 to compute geomean
² ratios must be >0 to compute geomean
```
---------
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
223
sdk/trace/internal/observ/tracer.go
Normal file
223
sdk/trace/internal/observ/tracer.go
Normal file
@@ -0,0 +1,223 @@
|
||||
// Copyright The OpenTelemetry Authors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package observ // import "go.opentelemetry.io/otel/sdk/trace/internal/observ"
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"go.opentelemetry.io/otel"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/metric"
|
||||
"go.opentelemetry.io/otel/sdk"
|
||||
"go.opentelemetry.io/otel/sdk/trace/internal/x"
|
||||
"go.opentelemetry.io/otel/semconv/v1.37.0/otelconv"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
)
|
||||
|
||||
var meterOpts = []metric.MeterOption{
|
||||
metric.WithInstrumentationVersion(sdk.Version()),
|
||||
metric.WithSchemaURL(SchemaURL),
|
||||
}
|
||||
|
||||
// Tracer is instrumentation for an OTel SDK Tracer.
|
||||
type Tracer struct {
|
||||
enabled bool
|
||||
|
||||
live metric.Int64UpDownCounter
|
||||
started metric.Int64Counter
|
||||
}
|
||||
|
||||
func NewTracer() (Tracer, error) {
|
||||
if !x.Observability.Enabled() {
|
||||
return Tracer{}, nil
|
||||
}
|
||||
meter := otel.GetMeterProvider().Meter(ScopeName, meterOpts...)
|
||||
|
||||
var err error
|
||||
l, e := otelconv.NewSDKSpanLive(meter)
|
||||
if e != nil {
|
||||
e = fmt.Errorf("failed to create span live metric: %w", e)
|
||||
err = errors.Join(err, e)
|
||||
}
|
||||
|
||||
s, e := otelconv.NewSDKSpanStarted(meter)
|
||||
if e != nil {
|
||||
e = fmt.Errorf("failed to create span started metric: %w", e)
|
||||
err = errors.Join(err, e)
|
||||
}
|
||||
|
||||
return Tracer{enabled: true, live: l.Inst(), started: s.Inst()}, err
|
||||
}
|
||||
|
||||
func (t Tracer) Enabled() bool { return t.enabled }
|
||||
|
||||
func (t Tracer) SpanStarted(ctx context.Context, psc trace.SpanContext, span trace.Span) {
|
||||
key := spanStartedKey{
|
||||
parent: parentStateNoParent,
|
||||
sampling: samplingStateDrop,
|
||||
}
|
||||
|
||||
if psc.IsValid() {
|
||||
if psc.IsRemote() {
|
||||
key.parent = parentStateRemoteParent
|
||||
} else {
|
||||
key.parent = parentStateLocalParent
|
||||
}
|
||||
}
|
||||
|
||||
if span.IsRecording() {
|
||||
if span.SpanContext().IsSampled() {
|
||||
key.sampling = samplingStateRecordAndSample
|
||||
} else {
|
||||
key.sampling = samplingStateRecordOnly
|
||||
}
|
||||
}
|
||||
|
||||
opts := spanStartedOpts[key]
|
||||
t.started.Add(ctx, 1, opts...)
|
||||
}
|
||||
|
||||
func (t Tracer) SpanLive(ctx context.Context, span trace.Span) {
|
||||
t.spanLive(ctx, 1, span)
|
||||
}
|
||||
|
||||
func (t Tracer) SpanEnded(ctx context.Context, span trace.Span) {
|
||||
t.spanLive(ctx, -1, span)
|
||||
}
|
||||
|
||||
func (t Tracer) spanLive(ctx context.Context, value int64, span trace.Span) {
|
||||
key := spanLiveKey{sampled: span.SpanContext().IsSampled()}
|
||||
opts := spanLiveOpts[key]
|
||||
t.live.Add(ctx, value, opts...)
|
||||
}
|
||||
|
||||
type parentState int
|
||||
|
||||
const (
|
||||
parentStateNoParent parentState = iota
|
||||
parentStateLocalParent
|
||||
parentStateRemoteParent
|
||||
)
|
||||
|
||||
type samplingState int
|
||||
|
||||
const (
|
||||
samplingStateDrop samplingState = iota
|
||||
samplingStateRecordOnly
|
||||
samplingStateRecordAndSample
|
||||
)
|
||||
|
||||
type spanStartedKey struct {
|
||||
parent parentState
|
||||
sampling samplingState
|
||||
}
|
||||
|
||||
var spanStartedOpts = map[spanStartedKey][]metric.AddOption{
|
||||
{
|
||||
parentStateNoParent,
|
||||
samplingStateDrop,
|
||||
}: {
|
||||
metric.WithAttributeSet(attribute.NewSet(
|
||||
otelconv.SDKSpanStarted{}.AttrSpanParentOrigin(otelconv.SpanParentOriginNone),
|
||||
otelconv.SDKSpanStarted{}.AttrSpanSamplingResult(otelconv.SpanSamplingResultDrop),
|
||||
)),
|
||||
},
|
||||
{
|
||||
parentStateLocalParent,
|
||||
samplingStateDrop,
|
||||
}: {
|
||||
metric.WithAttributeSet(attribute.NewSet(
|
||||
otelconv.SDKSpanStarted{}.AttrSpanParentOrigin(otelconv.SpanParentOriginLocal),
|
||||
otelconv.SDKSpanStarted{}.AttrSpanSamplingResult(otelconv.SpanSamplingResultDrop),
|
||||
)),
|
||||
},
|
||||
{
|
||||
parentStateRemoteParent,
|
||||
samplingStateDrop,
|
||||
}: {
|
||||
metric.WithAttributeSet(attribute.NewSet(
|
||||
otelconv.SDKSpanStarted{}.AttrSpanParentOrigin(otelconv.SpanParentOriginRemote),
|
||||
otelconv.SDKSpanStarted{}.AttrSpanSamplingResult(otelconv.SpanSamplingResultDrop),
|
||||
)),
|
||||
},
|
||||
|
||||
{
|
||||
parentStateNoParent,
|
||||
samplingStateRecordOnly,
|
||||
}: {
|
||||
metric.WithAttributeSet(attribute.NewSet(
|
||||
otelconv.SDKSpanStarted{}.AttrSpanParentOrigin(otelconv.SpanParentOriginNone),
|
||||
otelconv.SDKSpanStarted{}.AttrSpanSamplingResult(otelconv.SpanSamplingResultRecordOnly),
|
||||
)),
|
||||
},
|
||||
{
|
||||
parentStateLocalParent,
|
||||
samplingStateRecordOnly,
|
||||
}: {
|
||||
metric.WithAttributeSet(attribute.NewSet(
|
||||
otelconv.SDKSpanStarted{}.AttrSpanParentOrigin(otelconv.SpanParentOriginLocal),
|
||||
otelconv.SDKSpanStarted{}.AttrSpanSamplingResult(otelconv.SpanSamplingResultRecordOnly),
|
||||
)),
|
||||
},
|
||||
{
|
||||
parentStateRemoteParent,
|
||||
samplingStateRecordOnly,
|
||||
}: {
|
||||
metric.WithAttributeSet(attribute.NewSet(
|
||||
otelconv.SDKSpanStarted{}.AttrSpanParentOrigin(otelconv.SpanParentOriginRemote),
|
||||
otelconv.SDKSpanStarted{}.AttrSpanSamplingResult(otelconv.SpanSamplingResultRecordOnly),
|
||||
)),
|
||||
},
|
||||
|
||||
{
|
||||
parentStateNoParent,
|
||||
samplingStateRecordAndSample,
|
||||
}: {
|
||||
metric.WithAttributeSet(attribute.NewSet(
|
||||
otelconv.SDKSpanStarted{}.AttrSpanParentOrigin(otelconv.SpanParentOriginNone),
|
||||
otelconv.SDKSpanStarted{}.AttrSpanSamplingResult(otelconv.SpanSamplingResultRecordAndSample),
|
||||
)),
|
||||
},
|
||||
{
|
||||
parentStateLocalParent,
|
||||
samplingStateRecordAndSample,
|
||||
}: {
|
||||
metric.WithAttributeSet(attribute.NewSet(
|
||||
otelconv.SDKSpanStarted{}.AttrSpanParentOrigin(otelconv.SpanParentOriginLocal),
|
||||
otelconv.SDKSpanStarted{}.AttrSpanSamplingResult(otelconv.SpanSamplingResultRecordAndSample),
|
||||
)),
|
||||
},
|
||||
{
|
||||
parentStateRemoteParent,
|
||||
samplingStateRecordAndSample,
|
||||
}: {
|
||||
metric.WithAttributeSet(attribute.NewSet(
|
||||
otelconv.SDKSpanStarted{}.AttrSpanParentOrigin(otelconv.SpanParentOriginRemote),
|
||||
otelconv.SDKSpanStarted{}.AttrSpanSamplingResult(otelconv.SpanSamplingResultRecordAndSample),
|
||||
)),
|
||||
},
|
||||
}
|
||||
|
||||
type spanLiveKey struct {
|
||||
sampled bool
|
||||
}
|
||||
|
||||
var spanLiveOpts = map[spanLiveKey][]metric.AddOption{
|
||||
{true}: {
|
||||
metric.WithAttributeSet(attribute.NewSet(
|
||||
otelconv.SDKSpanLive{}.AttrSpanSamplingResult(
|
||||
otelconv.SpanSamplingResultRecordAndSample,
|
||||
),
|
||||
)),
|
||||
},
|
||||
{false}: {
|
||||
metric.WithAttributeSet(attribute.NewSet(
|
||||
otelconv.SDKSpanLive{}.AttrSpanSamplingResult(
|
||||
otelconv.SpanSamplingResultRecordOnly,
|
||||
),
|
||||
)),
|
||||
},
|
||||
}
|
296
sdk/trace/internal/observ/tracer_test.go
Normal file
296
sdk/trace/internal/observ/tracer_test.go
Normal file
@@ -0,0 +1,296 @@
|
||||
// Copyright The OpenTelemetry Authors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package observ_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"go.opentelemetry.io/otel"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/metric/noop"
|
||||
"go.opentelemetry.io/otel/sdk/metric/metricdata"
|
||||
"go.opentelemetry.io/otel/sdk/trace"
|
||||
"go.opentelemetry.io/otel/sdk/trace/internal/observ"
|
||||
"go.opentelemetry.io/otel/semconv/v1.37.0/otelconv"
|
||||
tapi "go.opentelemetry.io/otel/trace"
|
||||
)
|
||||
|
||||
func live(dPts ...metricdata.DataPoint[int64]) metricdata.Metrics {
|
||||
return metricdata.Metrics{
|
||||
Name: otelconv.SDKSpanLive{}.Name(),
|
||||
Description: otelconv.SDKSpanLive{}.Description(),
|
||||
Unit: otelconv.SDKSpanLive{}.Unit(),
|
||||
Data: metricdata.Sum[int64]{
|
||||
Temporality: metricdata.CumulativeTemporality,
|
||||
DataPoints: dPts,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func sampledLive(value int64) metricdata.Metrics {
|
||||
set := attribute.NewSet(
|
||||
otelconv.SDKSpanLive{}.AttrSpanSamplingResult(
|
||||
otelconv.SpanSamplingResultRecordAndSample,
|
||||
),
|
||||
)
|
||||
return live(dPt(set, value))
|
||||
}
|
||||
|
||||
func started(dPts ...metricdata.DataPoint[int64]) metricdata.Metrics {
|
||||
return metricdata.Metrics{
|
||||
Name: otelconv.SDKSpanStarted{}.Name(),
|
||||
Description: otelconv.SDKSpanStarted{}.Description(),
|
||||
Unit: otelconv.SDKSpanStarted{}.Unit(),
|
||||
Data: metricdata.Sum[int64]{
|
||||
Temporality: metricdata.CumulativeTemporality,
|
||||
IsMonotonic: true,
|
||||
DataPoints: dPts,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func sampledStarted(value int64) metricdata.Metrics {
|
||||
set := attribute.NewSet(
|
||||
otelconv.SDKSpanStarted{}.AttrSpanParentOrigin(
|
||||
otelconv.SpanParentOriginNone,
|
||||
),
|
||||
otelconv.SDKSpanStarted{}.AttrSpanSamplingResult(
|
||||
otelconv.SpanSamplingResultRecordAndSample,
|
||||
),
|
||||
)
|
||||
return started(dPt(set, value))
|
||||
}
|
||||
|
||||
func TestTracer(t *testing.T) {
|
||||
collect := setup(t)
|
||||
tracer := trace.NewTracerProvider().Tracer(t.Name())
|
||||
|
||||
_, span := tracer.Start(context.Background(), "span")
|
||||
check(t, collect(), sampledLive(1), sampledStarted(1))
|
||||
|
||||
span.End()
|
||||
check(t, collect(), sampledLive(0), sampledStarted(1))
|
||||
}
|
||||
|
||||
func dropStarted(value int64) metricdata.Metrics {
|
||||
set := attribute.NewSet(
|
||||
otelconv.SDKSpanStarted{}.AttrSpanParentOrigin(
|
||||
otelconv.SpanParentOriginNone,
|
||||
),
|
||||
otelconv.SDKSpanStarted{}.AttrSpanSamplingResult(
|
||||
otelconv.SpanSamplingResultDrop,
|
||||
),
|
||||
)
|
||||
return started(dPt(set, value))
|
||||
}
|
||||
|
||||
func TestTracerNonRecording(t *testing.T) {
|
||||
collect := setup(t)
|
||||
tracer := trace.NewTracerProvider(
|
||||
trace.WithSampler(trace.NeverSample()),
|
||||
).Tracer(t.Name())
|
||||
|
||||
_, _ = tracer.Start(context.Background(), "span")
|
||||
check(t, collect(), dropStarted(1))
|
||||
}
|
||||
|
||||
func recLive(value int64) metricdata.Metrics {
|
||||
set := attribute.NewSet(
|
||||
otelconv.SDKSpanLive{}.AttrSpanSamplingResult(
|
||||
otelconv.SpanSamplingResultRecordOnly,
|
||||
),
|
||||
)
|
||||
return live(dPt(set, value))
|
||||
}
|
||||
|
||||
func recStarted(value int64) metricdata.Metrics {
|
||||
set := attribute.NewSet(
|
||||
otelconv.SDKSpanStarted{}.AttrSpanParentOrigin(
|
||||
otelconv.SpanParentOriginNone,
|
||||
),
|
||||
otelconv.SDKSpanStarted{}.AttrSpanSamplingResult(
|
||||
otelconv.SpanSamplingResultRecordOnly,
|
||||
),
|
||||
)
|
||||
return started(dPt(set, value))
|
||||
}
|
||||
|
||||
type recOnly struct{}
|
||||
|
||||
func (recOnly) ShouldSample(p trace.SamplingParameters) trace.SamplingResult {
|
||||
psc := tapi.SpanContextFromContext(p.ParentContext)
|
||||
return trace.SamplingResult{
|
||||
Decision: trace.RecordOnly,
|
||||
Tracestate: psc.TraceState(),
|
||||
}
|
||||
}
|
||||
|
||||
func (recOnly) Description() string { return "RecordingOnly" }
|
||||
|
||||
func TestTracerRecordOnly(t *testing.T) {
|
||||
collect := setup(t)
|
||||
tracer := trace.NewTracerProvider(
|
||||
trace.WithSampler(recOnly{}),
|
||||
).Tracer(t.Name())
|
||||
|
||||
_, _ = tracer.Start(context.Background(), "span")
|
||||
check(t, collect(), recLive(1), recStarted(1))
|
||||
}
|
||||
|
||||
func remoteStarted(value int64) metricdata.Metrics {
|
||||
set := attribute.NewSet(
|
||||
otelconv.SDKSpanStarted{}.AttrSpanParentOrigin(
|
||||
otelconv.SpanParentOriginRemote,
|
||||
),
|
||||
otelconv.SDKSpanStarted{}.AttrSpanSamplingResult(
|
||||
otelconv.SpanSamplingResultRecordAndSample,
|
||||
),
|
||||
)
|
||||
return started(dPt(set, value))
|
||||
}
|
||||
|
||||
func TestTracerRemoteParent(t *testing.T) {
|
||||
collect := setup(t)
|
||||
tracer := trace.NewTracerProvider().Tracer(t.Name())
|
||||
|
||||
ctx := tapi.ContextWithRemoteSpanContext(
|
||||
context.Background(),
|
||||
tapi.NewSpanContext(tapi.SpanContextConfig{
|
||||
TraceID: tapi.TraceID{0x01},
|
||||
SpanID: tapi.SpanID{0x01},
|
||||
TraceFlags: 0x1,
|
||||
Remote: true,
|
||||
}))
|
||||
|
||||
_, _ = tracer.Start(ctx, "span")
|
||||
check(t, collect(), sampledLive(1), remoteStarted(1))
|
||||
}
|
||||
|
||||
func chainStarted(parent, child int64) metricdata.Metrics {
|
||||
noParentSet := attribute.NewSet(
|
||||
otelconv.SDKSpanStarted{}.AttrSpanParentOrigin(
|
||||
otelconv.SpanParentOriginNone,
|
||||
),
|
||||
otelconv.SDKSpanStarted{}.AttrSpanSamplingResult(
|
||||
otelconv.SpanSamplingResultRecordAndSample,
|
||||
),
|
||||
)
|
||||
localSet := attribute.NewSet(
|
||||
otelconv.SDKSpanStarted{}.AttrSpanParentOrigin(
|
||||
otelconv.SpanParentOriginLocal,
|
||||
),
|
||||
otelconv.SDKSpanStarted{}.AttrSpanSamplingResult(
|
||||
otelconv.SpanSamplingResultRecordAndSample,
|
||||
),
|
||||
)
|
||||
return started(dPt(noParentSet, parent), dPt(localSet, child))
|
||||
}
|
||||
|
||||
func TestTracerLocalParent(t *testing.T) {
|
||||
collect := setup(t)
|
||||
tracer := trace.NewTracerProvider().Tracer(t.Name())
|
||||
|
||||
ctx, parent := tracer.Start(context.Background(), "parent")
|
||||
_, child := tracer.Start(ctx, "child")
|
||||
|
||||
check(t, collect(), sampledLive(2), chainStarted(1, 1))
|
||||
|
||||
child.End()
|
||||
parent.End()
|
||||
|
||||
check(t, collect(), sampledLive(0), chainStarted(1, 1))
|
||||
}
|
||||
|
||||
func TestNewTracerObservabilityDisabled(t *testing.T) {
|
||||
// Do not set OTEL_GO_X_OBSERVABILITY
|
||||
tracer, err := observ.NewTracer()
|
||||
assert.NoError(t, err)
|
||||
assert.False(t, tracer.Enabled())
|
||||
}
|
||||
|
||||
func TestNewTracerErrors(t *testing.T) {
|
||||
t.Setenv("OTEL_GO_X_OBSERVABILITY", "true")
|
||||
|
||||
orig := otel.GetMeterProvider()
|
||||
t.Cleanup(func() { otel.SetMeterProvider(orig) })
|
||||
|
||||
mp := &errMeterProvider{err: assert.AnError}
|
||||
otel.SetMeterProvider(mp)
|
||||
|
||||
_, err := observ.NewTracer()
|
||||
require.ErrorIs(t, err, assert.AnError, "new instrument errors")
|
||||
|
||||
assert.ErrorContains(t, err, "span live metric")
|
||||
assert.ErrorContains(t, err, "span started metric")
|
||||
}
|
||||
|
||||
func BenchmarkTracer(b *testing.B) {
|
||||
b.Setenv("OTEL_GO_X_OBSERVABILITY", "true")
|
||||
|
||||
orig := otel.GetMeterProvider()
|
||||
b.Cleanup(func() { otel.SetMeterProvider(orig) })
|
||||
|
||||
// Ensure deterministic benchmark by using noop meter.
|
||||
otel.SetMeterProvider(noop.NewMeterProvider())
|
||||
|
||||
tracer, err := observ.NewTracer()
|
||||
require.NoError(b, err)
|
||||
require.True(b, tracer.Enabled())
|
||||
|
||||
t := otel.GetTracerProvider().Tracer(b.Name())
|
||||
ctx, span := t.Start(context.Background(), "parent")
|
||||
psc := span.SpanContext()
|
||||
span.End()
|
||||
|
||||
b.Run("SpanStarted", func(b *testing.B) {
|
||||
b.ReportAllocs()
|
||||
b.RunParallel(func(p *testing.PB) {
|
||||
for p.Next() {
|
||||
tracer.SpanStarted(ctx, psc, span)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
b.Run("SpanLive", func(b *testing.B) {
|
||||
b.ReportAllocs()
|
||||
b.RunParallel(func(p *testing.PB) {
|
||||
for p.Next() {
|
||||
tracer.SpanLive(ctx, span)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
b.Run("SpanEnded", func(b *testing.B) {
|
||||
b.ReportAllocs()
|
||||
b.RunParallel(func(p *testing.PB) {
|
||||
for p.Next() {
|
||||
tracer.SpanEnded(ctx, span)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func BenchmarkNewTracer(b *testing.B) {
|
||||
b.Setenv("OTEL_GO_X_OBSERVABILITY", "true")
|
||||
|
||||
orig := otel.GetMeterProvider()
|
||||
b.Cleanup(func() { otel.SetMeterProvider(orig) })
|
||||
|
||||
// Ensure deterministic benchmark by using noop meter.
|
||||
otel.SetMeterProvider(noop.NewMeterProvider())
|
||||
|
||||
var tracer observ.Tracer
|
||||
|
||||
b.ReportAllocs()
|
||||
b.ResetTimer()
|
||||
for b.Loop() {
|
||||
tracer, _ = observ.NewTracer()
|
||||
}
|
||||
|
||||
_ = tracer
|
||||
}
|
@@ -5,20 +5,15 @@ package trace // import "go.opentelemetry.io/otel/sdk/trace"
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
|
||||
"go.opentelemetry.io/otel"
|
||||
"go.opentelemetry.io/otel/internal/global"
|
||||
"go.opentelemetry.io/otel/metric"
|
||||
"go.opentelemetry.io/otel/sdk"
|
||||
"go.opentelemetry.io/otel/sdk/instrumentation"
|
||||
"go.opentelemetry.io/otel/sdk/resource"
|
||||
"go.opentelemetry.io/otel/sdk/trace/internal/x"
|
||||
semconv "go.opentelemetry.io/otel/semconv/v1.37.0"
|
||||
"go.opentelemetry.io/otel/semconv/v1.37.0/otelconv"
|
||||
"go.opentelemetry.io/otel/sdk/trace/internal/observ"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
"go.opentelemetry.io/otel/trace/embedded"
|
||||
"go.opentelemetry.io/otel/trace/noop"
|
||||
@@ -165,17 +160,14 @@ func (p *TracerProvider) Tracer(name string, opts ...trace.TracerOption) trace.T
|
||||
t = &tracer{
|
||||
provider: p,
|
||||
instrumentationScope: is,
|
||||
observabilityEnabled: x.Observability.Enabled(),
|
||||
}
|
||||
if t.observabilityEnabled {
|
||||
var err error
|
||||
t.spanLiveMetric, t.spanStartedMetric, err = newInst()
|
||||
if err != nil {
|
||||
msg := "failed to create observability metrics for tracer: %w"
|
||||
err := fmt.Errorf(msg, err)
|
||||
otel.Handle(err)
|
||||
}
|
||||
|
||||
var err error
|
||||
t.inst, err = observ.NewTracer()
|
||||
if err != nil {
|
||||
otel.Handle(err)
|
||||
}
|
||||
|
||||
p.namedTracer[is] = t
|
||||
}
|
||||
return t, ok
|
||||
@@ -201,23 +193,6 @@ func (p *TracerProvider) Tracer(name string, opts ...trace.TracerOption) trace.T
|
||||
return t
|
||||
}
|
||||
|
||||
func newInst() (otelconv.SDKSpanLive, otelconv.SDKSpanStarted, error) {
|
||||
m := otel.GetMeterProvider().Meter(
|
||||
obsScopeName,
|
||||
metric.WithInstrumentationVersion(sdk.Version()),
|
||||
metric.WithSchemaURL(semconv.SchemaURL),
|
||||
)
|
||||
|
||||
var err error
|
||||
spanLiveMetric, e := otelconv.NewSDKSpanLive(m)
|
||||
err = errors.Join(err, e)
|
||||
|
||||
spanStartedMetric, e := otelconv.NewSDKSpanStarted(m)
|
||||
err = errors.Join(err, e)
|
||||
|
||||
return spanLiveMetric, spanStartedMetric, err
|
||||
}
|
||||
|
||||
// RegisterSpanProcessor adds the given SpanProcessor to the list of SpanProcessors.
|
||||
func (p *TracerProvider) RegisterSpanProcessor(sp SpanProcessor) {
|
||||
// This check prevents calls during a shutdown.
|
||||
|
@@ -412,11 +412,7 @@ func TestTracerProviderObservability(t *testing.T) {
|
||||
require.IsType(t, &tracer{}, tr)
|
||||
|
||||
tStruct := tr.(*tracer)
|
||||
assert.True(t, tStruct.observabilityEnabled, "observability should be enabled")
|
||||
|
||||
// Verify instruments are created
|
||||
assert.NotNil(t, tStruct.spanLiveMetric, "spanLiveMetric should be created")
|
||||
assert.NotNil(t, tStruct.spanStartedMetric, "spanStartedMetric should be created")
|
||||
assert.True(t, tStruct.inst.Enabled(), "observability should be enabled")
|
||||
|
||||
// Verify errors are passed to the otel handler
|
||||
handlerErrs := handler.errs
|
||||
|
@@ -506,7 +506,7 @@ func (s *recordingSpan) End(options ...trace.SpanEndOption) {
|
||||
}
|
||||
s.mu.Unlock()
|
||||
|
||||
if s.tracer.observabilityEnabled {
|
||||
if s.tracer.inst.Enabled() {
|
||||
ctx := s.origCtx
|
||||
if ctx == nil {
|
||||
// This should not happen as the origCtx should be set, but
|
||||
@@ -514,10 +514,7 @@ func (s *recordingSpan) End(options ...trace.SpanEndOption) {
|
||||
// error.
|
||||
ctx = trace.ContextWithSpan(context.Background(), s)
|
||||
}
|
||||
defer func(ctx context.Context) {
|
||||
set := spanLiveSet(s.spanContext.IsSampled())
|
||||
s.tracer.spanLiveMetric.AddSet(ctx, -1, set)
|
||||
}(ctx)
|
||||
defer s.tracer.inst.SpanEnded(ctx, s)
|
||||
}
|
||||
|
||||
sps := s.tracer.provider.getSpanProcessors()
|
||||
|
@@ -28,6 +28,7 @@ import (
|
||||
"go.opentelemetry.io/otel/sdk/metric/metricdata"
|
||||
"go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest"
|
||||
"go.opentelemetry.io/otel/sdk/resource"
|
||||
"go.opentelemetry.io/otel/sdk/trace/internal/observ"
|
||||
semconv "go.opentelemetry.io/otel/semconv/v1.37.0"
|
||||
"go.opentelemetry.io/otel/semconv/v1.37.0/otelconv"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
@@ -2259,9 +2260,9 @@ func TestObservability(t *testing.T) {
|
||||
|
||||
want := metricdata.ScopeMetrics{
|
||||
Scope: instrumentation.Scope{
|
||||
Name: "go.opentelemetry.io/otel/sdk/trace",
|
||||
Name: observ.ScopeName,
|
||||
Version: sdk.Version(),
|
||||
SchemaURL: semconv.SchemaURL,
|
||||
SchemaURL: observ.SchemaURL,
|
||||
},
|
||||
Metrics: []metricdata.Metrics{
|
||||
{
|
||||
@@ -2320,9 +2321,9 @@ func TestObservability(t *testing.T) {
|
||||
|
||||
want = metricdata.ScopeMetrics{
|
||||
Scope: instrumentation.Scope{
|
||||
Name: "go.opentelemetry.io/otel/sdk/trace",
|
||||
Name: observ.ScopeName,
|
||||
Version: sdk.Version(),
|
||||
SchemaURL: semconv.SchemaURL,
|
||||
SchemaURL: observ.SchemaURL,
|
||||
},
|
||||
Metrics: []metricdata.Metrics{
|
||||
{
|
||||
@@ -2386,9 +2387,9 @@ func TestObservability(t *testing.T) {
|
||||
|
||||
want := metricdata.ScopeMetrics{
|
||||
Scope: instrumentation.Scope{
|
||||
Name: "go.opentelemetry.io/otel/sdk/trace",
|
||||
Name: observ.ScopeName,
|
||||
Version: sdk.Version(),
|
||||
SchemaURL: semconv.SchemaURL,
|
||||
SchemaURL: observ.SchemaURL,
|
||||
},
|
||||
Metrics: []metricdata.Metrics{
|
||||
{
|
||||
@@ -2435,9 +2436,9 @@ func TestObservability(t *testing.T) {
|
||||
|
||||
want := metricdata.ScopeMetrics{
|
||||
Scope: instrumentation.Scope{
|
||||
Name: "go.opentelemetry.io/otel/sdk/trace",
|
||||
Name: observ.ScopeName,
|
||||
Version: sdk.Version(),
|
||||
SchemaURL: semconv.SchemaURL,
|
||||
SchemaURL: observ.SchemaURL,
|
||||
},
|
||||
Metrics: []metricdata.Metrics{
|
||||
{
|
||||
@@ -2512,9 +2513,9 @@ func TestObservability(t *testing.T) {
|
||||
|
||||
want := metricdata.ScopeMetrics{
|
||||
Scope: instrumentation.Scope{
|
||||
Name: "go.opentelemetry.io/otel/sdk/trace",
|
||||
Name: observ.ScopeName,
|
||||
Version: sdk.Version(),
|
||||
SchemaURL: semconv.SchemaURL,
|
||||
SchemaURL: observ.SchemaURL,
|
||||
},
|
||||
Metrics: []metricdata.Metrics{
|
||||
{
|
||||
@@ -2578,9 +2579,9 @@ func TestObservability(t *testing.T) {
|
||||
|
||||
want := metricdata.ScopeMetrics{
|
||||
Scope: instrumentation.Scope{
|
||||
Name: "go.opentelemetry.io/otel/sdk/trace",
|
||||
Name: observ.ScopeName,
|
||||
Version: sdk.Version(),
|
||||
SchemaURL: semconv.SchemaURL,
|
||||
SchemaURL: observ.SchemaURL,
|
||||
},
|
||||
Metrics: []metricdata.Metrics{
|
||||
{
|
||||
@@ -2651,9 +2652,9 @@ func TestObservability(t *testing.T) {
|
||||
|
||||
want = metricdata.ScopeMetrics{
|
||||
Scope: instrumentation.Scope{
|
||||
Name: "go.opentelemetry.io/otel/sdk/trace",
|
||||
Name: observ.ScopeName,
|
||||
Version: sdk.Version(),
|
||||
SchemaURL: semconv.SchemaURL,
|
||||
SchemaURL: observ.SchemaURL,
|
||||
},
|
||||
Metrics: []metricdata.Metrics{
|
||||
{
|
||||
|
@@ -7,9 +7,8 @@ import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/sdk/instrumentation"
|
||||
"go.opentelemetry.io/otel/semconv/v1.37.0/otelconv"
|
||||
"go.opentelemetry.io/otel/sdk/trace/internal/observ"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
"go.opentelemetry.io/otel/trace/embedded"
|
||||
)
|
||||
@@ -20,9 +19,7 @@ type tracer struct {
|
||||
provider *TracerProvider
|
||||
instrumentationScope instrumentation.Scope
|
||||
|
||||
observabilityEnabled bool
|
||||
spanLiveMetric otelconv.SDKSpanLive
|
||||
spanStartedMetric otelconv.SDKSpanStarted
|
||||
inst observ.Tracer
|
||||
}
|
||||
|
||||
var _ trace.Tracer = &tracer{}
|
||||
@@ -53,7 +50,7 @@ func (tr *tracer) Start(
|
||||
|
||||
s := tr.newSpan(ctx, name, &config)
|
||||
newCtx := trace.ContextWithSpan(ctx, s)
|
||||
if tr.observabilityEnabled {
|
||||
if tr.inst.Enabled() {
|
||||
if o, ok := s.(interface{ setOrigCtx(context.Context) }); ok {
|
||||
// If this is a recording span, store the original context.
|
||||
// This allows later retrieval of baggage and other information
|
||||
@@ -63,8 +60,7 @@ func (tr *tracer) Start(
|
||||
o.setOrigCtx(newCtx)
|
||||
}
|
||||
psc := trace.SpanContextFromContext(ctx)
|
||||
set := spanStartedSet(psc, s)
|
||||
tr.spanStartedMetric.AddSet(newCtx, 1, set)
|
||||
tr.inst.SpanStarted(newCtx, psc, s)
|
||||
}
|
||||
|
||||
if rw, ok := s.(ReadWriteSpan); ok && s.IsRecording() {
|
||||
@@ -176,12 +172,11 @@ func (tr *tracer) newRecordingSpan(
|
||||
s.SetAttributes(sr.Attributes...)
|
||||
s.SetAttributes(config.Attributes()...)
|
||||
|
||||
if tr.observabilityEnabled {
|
||||
if tr.inst.Enabled() {
|
||||
// Propagate any existing values from the context with the new span to
|
||||
// the measurement context.
|
||||
ctx = trace.ContextWithSpan(ctx, s)
|
||||
set := spanLiveSet(s.spanContext.IsSampled())
|
||||
tr.spanLiveMetric.AddSet(ctx, 1, set)
|
||||
tr.inst.SpanLive(ctx, s)
|
||||
}
|
||||
|
||||
return s
|
||||
@@ -191,112 +186,3 @@ func (tr *tracer) newRecordingSpan(
|
||||
func (tr *tracer) newNonRecordingSpan(sc trace.SpanContext) nonRecordingSpan {
|
||||
return nonRecordingSpan{tracer: tr, sc: sc}
|
||||
}
|
||||
|
||||
type parentState int
|
||||
|
||||
const (
|
||||
parentStateNoParent parentState = iota
|
||||
parentStateLocalParent
|
||||
parentStateRemoteParent
|
||||
)
|
||||
|
||||
type samplingState int
|
||||
|
||||
const (
|
||||
samplingStateDrop samplingState = iota
|
||||
samplingStateRecordOnly
|
||||
samplingStateRecordAndSample
|
||||
)
|
||||
|
||||
type spanStartedSetKey struct {
|
||||
parent parentState
|
||||
sampling samplingState
|
||||
}
|
||||
|
||||
var spanStartedSetCache = map[spanStartedSetKey]attribute.Set{
|
||||
{parentStateNoParent, samplingStateDrop}: attribute.NewSet(
|
||||
otelconv.SDKSpanStarted{}.AttrSpanParentOrigin(otelconv.SpanParentOriginNone),
|
||||
otelconv.SDKSpanStarted{}.AttrSpanSamplingResult(otelconv.SpanSamplingResultDrop),
|
||||
),
|
||||
{parentStateLocalParent, samplingStateDrop}: attribute.NewSet(
|
||||
otelconv.SDKSpanStarted{}.AttrSpanParentOrigin(otelconv.SpanParentOriginLocal),
|
||||
otelconv.SDKSpanStarted{}.AttrSpanSamplingResult(otelconv.SpanSamplingResultDrop),
|
||||
),
|
||||
{parentStateRemoteParent, samplingStateDrop}: attribute.NewSet(
|
||||
otelconv.SDKSpanStarted{}.AttrSpanParentOrigin(otelconv.SpanParentOriginRemote),
|
||||
otelconv.SDKSpanStarted{}.AttrSpanSamplingResult(otelconv.SpanSamplingResultDrop),
|
||||
),
|
||||
|
||||
{parentStateNoParent, samplingStateRecordOnly}: attribute.NewSet(
|
||||
otelconv.SDKSpanStarted{}.AttrSpanParentOrigin(otelconv.SpanParentOriginNone),
|
||||
otelconv.SDKSpanStarted{}.AttrSpanSamplingResult(otelconv.SpanSamplingResultRecordOnly),
|
||||
),
|
||||
{parentStateLocalParent, samplingStateRecordOnly}: attribute.NewSet(
|
||||
otelconv.SDKSpanStarted{}.AttrSpanParentOrigin(otelconv.SpanParentOriginLocal),
|
||||
otelconv.SDKSpanStarted{}.AttrSpanSamplingResult(otelconv.SpanSamplingResultRecordOnly),
|
||||
),
|
||||
{parentStateRemoteParent, samplingStateRecordOnly}: attribute.NewSet(
|
||||
otelconv.SDKSpanStarted{}.AttrSpanParentOrigin(otelconv.SpanParentOriginRemote),
|
||||
otelconv.SDKSpanStarted{}.AttrSpanSamplingResult(otelconv.SpanSamplingResultRecordOnly),
|
||||
),
|
||||
|
||||
{parentStateNoParent, samplingStateRecordAndSample}: attribute.NewSet(
|
||||
otelconv.SDKSpanStarted{}.AttrSpanParentOrigin(otelconv.SpanParentOriginNone),
|
||||
otelconv.SDKSpanStarted{}.AttrSpanSamplingResult(otelconv.SpanSamplingResultRecordAndSample),
|
||||
),
|
||||
{parentStateLocalParent, samplingStateRecordAndSample}: attribute.NewSet(
|
||||
otelconv.SDKSpanStarted{}.AttrSpanParentOrigin(otelconv.SpanParentOriginLocal),
|
||||
otelconv.SDKSpanStarted{}.AttrSpanSamplingResult(otelconv.SpanSamplingResultRecordAndSample),
|
||||
),
|
||||
{parentStateRemoteParent, samplingStateRecordAndSample}: attribute.NewSet(
|
||||
otelconv.SDKSpanStarted{}.AttrSpanParentOrigin(otelconv.SpanParentOriginRemote),
|
||||
otelconv.SDKSpanStarted{}.AttrSpanSamplingResult(otelconv.SpanSamplingResultRecordAndSample),
|
||||
),
|
||||
}
|
||||
|
||||
func spanStartedSet(psc trace.SpanContext, span trace.Span) attribute.Set {
|
||||
key := spanStartedSetKey{
|
||||
parent: parentStateNoParent,
|
||||
sampling: samplingStateDrop,
|
||||
}
|
||||
|
||||
if psc.IsValid() {
|
||||
if psc.IsRemote() {
|
||||
key.parent = parentStateRemoteParent
|
||||
} else {
|
||||
key.parent = parentStateLocalParent
|
||||
}
|
||||
}
|
||||
|
||||
if span.IsRecording() {
|
||||
if span.SpanContext().IsSampled() {
|
||||
key.sampling = samplingStateRecordAndSample
|
||||
} else {
|
||||
key.sampling = samplingStateRecordOnly
|
||||
}
|
||||
}
|
||||
|
||||
return spanStartedSetCache[key]
|
||||
}
|
||||
|
||||
type spanLiveSetKey struct {
|
||||
sampled bool
|
||||
}
|
||||
|
||||
var spanLiveSetCache = map[spanLiveSetKey]attribute.Set{
|
||||
{true}: attribute.NewSet(
|
||||
otelconv.SDKSpanLive{}.AttrSpanSamplingResult(
|
||||
otelconv.SpanSamplingResultRecordAndSample,
|
||||
),
|
||||
),
|
||||
{false}: attribute.NewSet(
|
||||
otelconv.SDKSpanLive{}.AttrSpanSamplingResult(
|
||||
otelconv.SpanSamplingResultRecordOnly,
|
||||
),
|
||||
),
|
||||
}
|
||||
|
||||
func spanLiveSet(sampled bool) attribute.Set {
|
||||
key := spanLiveSetKey{sampled: sampled}
|
||||
return spanLiveSetCache[key]
|
||||
}
|
||||
|
Reference in New Issue
Block a user