Skip to content

Commit f7d2882

Browse files
feat: sdk/trace: span processed metric for simple span processor (#7374)
Fixes #7004 This PR adds support for experimental otel.sdk.processor.span.processed metric in simple span processor. Definition of metric at: https://github.com/open-telemetry/semantic-conventions/blob/v1.36.0/docs/otel/sdk-metrics.md Experimental metrics are behind a feature flag: `OTEL_GO_X_OBSERVABILITY` <details> <summary>Observability Implementation Checklist</summary> ## Observability Implementation Checklist Based on the [project Observability guidelines](https://github.com/open-telemetry/opentelemetry-go/blob/e4ab3141123d0811125a69823dbbe4d9ec5a9b8f/CONTRIBUTING.md#observability), ensure the following are completed: ### Environment Variable Activation * [x] Observability features are disabled by default * [x] Features are activated through the `OTEL_GO_X_OBSERVABILITY` environment variable * [x] Use consistent pattern with `x.Observability.Enabled()` check [^1] * [x] Follow established experimental feature pattern [^2][^3] [^1]: https://github.com/open-telemetry/opentelemetry-go/blob/e4ab3141123d0811125a69823dbbe4d9ec5a9b8f/exporters/stdout/stdouttrace/internal/observ/instrumentation.go#L101-L103 [^2]: https://github.com/open-telemetry/opentelemetry-go/blob/e4ab3141123d0811125a69823dbbe4d9ec5a9b8f/exporters/stdout/stdouttrace/internal/x/x.go [^3]: https://github.com/open-telemetry/opentelemetry-go/blob/e4ab3141123d0811125a69823dbbe4d9ec5a9b8f/sdk/internal/x/x.go ### Encapsulation * [x] Instrumentation is encapsulated within a dedicated `struct` (e.g., [`Instrumentation`](https://github.com/open-telemetry/opentelemetry-go/blob/e4ab3141123d0811125a69823dbbe4d9ec5a9b8f/exporters/stdout/stdouttrace/internal/observ/instrumentation.go#L86-L94)) * [x] Instrumentation is not mixed into the instrumented component * [x] Instrumentation code is in its own file or package if complex/reused * [x] Instrumentation setup doesn't bloat the main component code ### Initialization * [x] Initialization is only done when observability is enabled * [x] Setup is explicit and side-effect free * [x] Return errors from initialization when appropriate * [x] Use the global Meter provider (e.g., `otel.GetMeterProvider()`) * [x] Include proper meter configuration with: * [x] Instrumentation package name is used for the Meter * [x] Instrumentation version (e.g. [`Version`](https://github.com/open-telemetry/opentelemetry-go/blob/e4ab3141123d0811125a69823dbbe4d9ec5a9b8f/exporters/stdout/stdouttrace/internal/observ/instrumentation.go#L40-L43)) * [x] Schema URL (e.g. [`SchemaURL`](https://github.com/open-telemetry/opentelemetry-go/blob/e4ab3141123d0811125a69823dbbe4d9ec5a9b8f/exporters/stdout/stdouttrace/internal/observ/instrumentation.go#L36-L38)) ### Performance * [x] Little to no overhead when observability is disabled * [x] Expensive operations are only executed when observability is enabled * [x] When enabled, instrumentation code paths are optimized to reduce allocation/computation overhead #### Attribute and Option Allocation Management * [x] Use `sync.Pool` for attribute slices and options with dynamic attributes * [x] Pool objects are properly reset before returning to pool * [x] Pools are scoped for maximum efficiency while ensuring correctness #### Caching * [x] Static attribute sets known at compile time are pre-computed and cached * [x] Common attribute combinations use lookup tables/maps #### Benchmarking * [x] Benchmarks provided for all instrumentation code * [ ] Benchmark scenarios include both enabled and disabled observability * [x] Benchmark results show impact on allocs/op, B/op, and ns/op (use `b.ReportAllocs()` in benchmarks) ### Error Handling and Robustness * [x] Errors are reported back to caller when possible * [x] Partial failures are handled gracefully * [x] Use partially initialized components when available * [x] Return errors to caller instead of only using `otel.Handle()` * [x] Use `otel.Handle()` only when component cannot report error to user ### Context Propagation * [x] Observability measurements receive the context from the function being measured (don't break context propagation by using `context.Background()`) ### Semantic Conventions Compliance * [x] All metrics follow [OpenTelemetry Semantic Conventions](https://github.com/open-telemetry/semantic-conventions/blob/5ee549b1ce30fe11fcb9b7e3bd35ebfb363f467f/docs/otel/sdk-metrics.md) * [x] Use the [`otelconv`](https://pkg.go.dev/go.opentelemetry.io/[email protected]/semconv/v1.37.0/otelconv) convenience package for metric semantic conventions * [x] Component names follow semantic conventions * [x] Use package path scope type as stable identifier for non-standard components * [x] Component names are stable unique identifiers * [x] Use global counter for uniqueness if necessary * [x] Component ID counter is resettable for deterministic testing ### Testing * [x] Use deterministic testing with isolated state * [x] Restore previous state after tests (`t.Cleanup()`) * [x] Isolate meter provider for testing * [x] Use `t.Setenv()` for environment variable testing * [x] Reset component ID counter for deterministic component names * [x] Test order doesn't affect results </details> ### Benchmarks ```console > benchstat bmark.result goos: darwin goarch: arm64 pkg: go.opentelemetry.io/otel/sdk/trace/internal/observ cpu: Apple M1 Pro │ bmark.result │ │ sec/op │ SSP/SpanProcessed-8 146.7n ± 15% SSP/SpanProcessedWithError-8 205.1n ± 3% geomean 173.5n │ bmark.result │ │ B/op │ SSP/SpanProcessed-8 280.0 ± 0% SSP/SpanProcessedWithError-8 408.0 ± 0% geomean 338.0 │ bmark.result │ │ allocs/op │ SSP/SpanProcessed-8 3.000 ± 0% SSP/SpanProcessedWithError-8 3.000 ± 0% geomean 3.000 ``` --------- Co-authored-by: Tyler Yahn <[email protected]>
1 parent 2e5fdd1 commit f7d2882

File tree

5 files changed

+415
-1
lines changed

5 files changed

+415
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
1919
- Add experimental observability metrics in `go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc`. (#7353)
2020
- Add experimental observability metrics in `go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc`. (#7459)
2121
- Add experimental observability metrics in `go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp`. (#7486)
22+
- Add experimental observability metrics for simple span processor in `go.opentelemetry.io/otel/sdk/trace`. (#7374)
2223

2324
### Fixed
2425

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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+
"fmt"
9+
"sync"
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+
semconv "go.opentelemetry.io/otel/semconv/v1.37.0"
17+
"go.opentelemetry.io/otel/semconv/v1.37.0/otelconv"
18+
)
19+
20+
var measureAttrsPool = sync.Pool{
21+
New: func() any {
22+
// "component.name" + "component.type" + "error.type"
23+
const n = 1 + 1 + 1
24+
s := make([]attribute.KeyValue, 0, n)
25+
// Return a pointer to a slice instead of a slice itself
26+
// to avoid allocations on every call.
27+
return &s
28+
},
29+
}
30+
31+
// SSP is the instrumentation for an OTel SDK SimpleSpanProcessor.
32+
type SSP struct {
33+
spansProcessedCounter metric.Int64Counter
34+
addOpts []metric.AddOption
35+
attrs []attribute.KeyValue
36+
}
37+
38+
// SSPComponentName returns the component name attribute for a
39+
// SimpleSpanProcessor with the given ID.
40+
func SSPComponentName(id int64) attribute.KeyValue {
41+
t := otelconv.ComponentTypeSimpleSpanProcessor
42+
name := fmt.Sprintf("%s/%d", t, id)
43+
return semconv.OTelComponentName(name)
44+
}
45+
46+
// NewSSP returns instrumentation for an OTel SDK SimpleSpanProcessor with the
47+
// provided ID.
48+
//
49+
// If the experimental observability is disabled, nil is returned.
50+
func NewSSP(id int64) (*SSP, error) {
51+
if !x.Observability.Enabled() {
52+
return nil, nil
53+
}
54+
55+
meter := otel.GetMeterProvider().Meter(
56+
ScopeName,
57+
metric.WithInstrumentationVersion(sdk.Version()),
58+
metric.WithSchemaURL(SchemaURL),
59+
)
60+
spansProcessedCounter, err := otelconv.NewSDKProcessorSpanProcessed(meter)
61+
if err != nil {
62+
err = fmt.Errorf("failed to create SSP processed spans metric: %w", err)
63+
}
64+
65+
componentName := SSPComponentName(id)
66+
componentType := spansProcessedCounter.AttrComponentType(otelconv.ComponentTypeSimpleSpanProcessor)
67+
attrs := []attribute.KeyValue{componentName, componentType}
68+
addOpts := []metric.AddOption{metric.WithAttributeSet(attribute.NewSet(attrs...))}
69+
70+
return &SSP{
71+
spansProcessedCounter: spansProcessedCounter.Inst(),
72+
addOpts: addOpts,
73+
attrs: attrs,
74+
}, err
75+
}
76+
77+
// SpanProcessed records that a span has been processed by the SimpleSpanProcessor.
78+
// If err is non-nil, it records the processing error as an attribute.
79+
func (ssp *SSP) SpanProcessed(ctx context.Context, err error) {
80+
ssp.spansProcessedCounter.Add(ctx, 1, ssp.addOption(err)...)
81+
}
82+
83+
func (ssp *SSP) addOption(err error) []metric.AddOption {
84+
if err == nil {
85+
return ssp.addOpts
86+
}
87+
attrs := measureAttrsPool.Get().(*[]attribute.KeyValue)
88+
defer func() {
89+
*attrs = (*attrs)[:0] // reset the slice for reuse
90+
measureAttrsPool.Put(attrs)
91+
}()
92+
*attrs = append(*attrs, ssp.attrs...)
93+
*attrs = append(*attrs, semconv.ErrorType(err))
94+
// Do not inefficiently make a copy of attrs by using
95+
// WithAttributes instead of WithAttributeSet.
96+
return []metric.AddOption{metric.WithAttributeSet(attribute.NewSet(*attrs...))}
97+
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
package observ_test
4+
5+
import (
6+
"errors"
7+
"testing"
8+
9+
"github.com/stretchr/testify/assert"
10+
"github.com/stretchr/testify/require"
11+
12+
"go.opentelemetry.io/otel"
13+
"go.opentelemetry.io/otel/attribute"
14+
"go.opentelemetry.io/otel/metric/noop"
15+
"go.opentelemetry.io/otel/sdk/trace/internal/observ"
16+
semconv "go.opentelemetry.io/otel/semconv/v1.37.0"
17+
)
18+
19+
const sspComponentID = 0
20+
21+
func TestSSPComponentName(t *testing.T) {
22+
got := observ.SSPComponentName(10)
23+
want := semconv.OTelComponentName("simple_span_processor/10")
24+
assert.Equal(t, want, got)
25+
}
26+
27+
func TestNewSSPError(t *testing.T) {
28+
t.Setenv("OTEL_GO_X_OBSERVABILITY", "true")
29+
30+
orig := otel.GetMeterProvider()
31+
t.Cleanup(func() { otel.SetMeterProvider(orig) })
32+
33+
mp := &errMeterProvider{err: assert.AnError}
34+
otel.SetMeterProvider(mp)
35+
36+
_, err := observ.NewSSP(sspComponentID)
37+
require.ErrorIs(t, err, assert.AnError, "new instrument errors")
38+
assert.ErrorContains(t, err, "create SSP processed spans metric")
39+
}
40+
41+
func TestNewSSPDisabled(t *testing.T) {
42+
ssp, err := observ.NewSSP(sspComponentID)
43+
assert.NoError(t, err)
44+
assert.Nil(t, ssp)
45+
}
46+
47+
func TestSSPSpanProcessed(t *testing.T) {
48+
ctx := t.Context()
49+
collect := setup(t)
50+
ssp, err := observ.NewSSP(sspComponentID)
51+
assert.NoError(t, err)
52+
53+
ssp.SpanProcessed(ctx, nil)
54+
check(t, collect(), processed(dPt(sspSet(), 1)))
55+
ssp.SpanProcessed(ctx, nil)
56+
ssp.SpanProcessed(ctx, nil)
57+
check(t, collect(), processed(dPt(sspSet(), 3)))
58+
59+
processErr := errors.New("error processing span")
60+
ssp.SpanProcessed(ctx, processErr)
61+
check(t, collect(), processed(
62+
dPt(sspSet(), 3),
63+
dPt(sspSet(semconv.ErrorType(processErr)), 1),
64+
))
65+
}
66+
67+
func BenchmarkSSP(b *testing.B) {
68+
b.Setenv("OTEL_GO_X_OBSERVABILITY", "true")
69+
70+
newSSP := func(b *testing.B) *observ.SSP {
71+
b.Helper()
72+
ssp, err := observ.NewSSP(sspComponentID)
73+
require.NoError(b, err)
74+
require.NotNil(b, ssp)
75+
return ssp
76+
}
77+
78+
b.Run("SpanProcessed", func(b *testing.B) {
79+
orig := otel.GetMeterProvider()
80+
b.Cleanup(func() {
81+
otel.SetMeterProvider(orig)
82+
})
83+
84+
// Ensure deterministic benchmark by using noop meter.
85+
otel.SetMeterProvider(noop.NewMeterProvider())
86+
87+
ssp := newSSP(b)
88+
ctx := b.Context()
89+
90+
b.ResetTimer()
91+
b.ReportAllocs()
92+
b.RunParallel(func(pb *testing.PB) {
93+
for pb.Next() {
94+
ssp.SpanProcessed(ctx, nil)
95+
}
96+
})
97+
})
98+
99+
b.Run("SpanProcessedWithError", func(b *testing.B) {
100+
orig := otel.GetMeterProvider()
101+
b.Cleanup(func() {
102+
otel.SetMeterProvider(orig)
103+
})
104+
105+
// Ensure deterministic benchmark by using noop meter.
106+
otel.SetMeterProvider(noop.NewMeterProvider())
107+
108+
ssp := newSSP(b)
109+
ctx := b.Context()
110+
processErr := errors.New("error processing span")
111+
112+
b.ResetTimer()
113+
b.ReportAllocs()
114+
b.RunParallel(func(pb *testing.PB) {
115+
for pb.Next() {
116+
ssp.SpanProcessed(ctx, processErr)
117+
}
118+
})
119+
})
120+
}
121+
122+
func sspSet(attrs ...attribute.KeyValue) attribute.Set {
123+
return attribute.NewSet(append([]attribute.KeyValue{
124+
semconv.OTelComponentTypeSimpleSpanProcessor,
125+
observ.SSPComponentName(sspComponentID),
126+
}, attrs...)...)
127+
}

sdk/trace/simple_span_processor.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@ package trace // import "go.opentelemetry.io/otel/sdk/trace"
66
import (
77
"context"
88
"sync"
9+
"sync/atomic"
910

1011
"go.opentelemetry.io/otel"
1112
"go.opentelemetry.io/otel/internal/global"
13+
"go.opentelemetry.io/otel/sdk/trace/internal/observ"
14+
"go.opentelemetry.io/otel/trace"
1215
)
1316

1417
// simpleSpanProcessor is a SpanProcessor that synchronously sends all
@@ -17,6 +20,8 @@ type simpleSpanProcessor struct {
1720
exporterMu sync.Mutex
1821
exporter SpanExporter
1922
stopOnce sync.Once
23+
24+
inst *observ.SSP
2025
}
2126

2227
var _ SpanProcessor = (*simpleSpanProcessor)(nil)
@@ -33,11 +38,26 @@ func NewSimpleSpanProcessor(exporter SpanExporter) SpanProcessor {
3338
ssp := &simpleSpanProcessor{
3439
exporter: exporter,
3540
}
41+
42+
var err error
43+
ssp.inst, err = observ.NewSSP(nextSimpleProcessorID())
44+
if err != nil {
45+
otel.Handle(err)
46+
}
47+
3648
global.Warn("SimpleSpanProcessor is not recommended for production use, consider using BatchSpanProcessor instead.")
3749

3850
return ssp
3951
}
4052

53+
var simpleProcessorIDCounter atomic.Int64
54+
55+
// nextSimpleProcessorID returns an identifier for this simple span processor,
56+
// starting with 0 and incrementing by 1 each time it is called.
57+
func nextSimpleProcessorID() int64 {
58+
return simpleProcessorIDCounter.Add(1) - 1
59+
}
60+
4161
// OnStart does nothing.
4262
func (*simpleSpanProcessor) OnStart(context.Context, ReadWriteSpan) {}
4363

@@ -46,11 +66,20 @@ func (ssp *simpleSpanProcessor) OnEnd(s ReadOnlySpan) {
4666
ssp.exporterMu.Lock()
4767
defer ssp.exporterMu.Unlock()
4868

69+
var err error
4970
if ssp.exporter != nil && s.SpanContext().TraceFlags().IsSampled() {
50-
if err := ssp.exporter.ExportSpans(context.Background(), []ReadOnlySpan{s}); err != nil {
71+
err = ssp.exporter.ExportSpans(context.Background(), []ReadOnlySpan{s})
72+
if err != nil {
5173
otel.Handle(err)
5274
}
5375
}
76+
77+
if ssp.inst != nil {
78+
// Add the span to the context to ensure the metric is recorded
79+
// with the correct span context.
80+
ctx := trace.ContextWithSpanContext(context.Background(), s.SpanContext())
81+
ssp.inst.SpanProcessed(ctx, err)
82+
}
5483
}
5584

5685
// Shutdown shuts down the exporter this SimpleSpanProcessor exports to.

0 commit comments

Comments
 (0)