1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-12-03 23:21:27 +02:00

Encapsulate SDK BatchSpanProcessor observability (#7332)

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-bsp-obs.out │
                         │          sec/op           │
BSP/Processed-8                        0.6394n ±  2%
BSP/ProcessedQueueFull-8               0.6806n ±  3%
BSP/Callback-8                          3.591µ ± 12%
geomean                                 11.60n

                         │ enc-trace-sdk-bsp-obs.out │
                         │           B/op            │
BSP/Processed-8                         0.000 ± 0%
BSP/ProcessedQueueFull-8                0.000 ± 0%
BSP/Callback-8                        2.626Ki ± 0%
geomean                                            ¹
¹ summaries must be >0 to compute geomean

                         │ enc-trace-sdk-bsp-obs.out │
                         │         allocs/op         │
BSP/Processed-8                         0.000 ± 0%
BSP/ProcessedQueueFull-8                0.000 ± 0%
BSP/Callback-8                          16.00 ± 0%
geomean                                            ¹
¹ summaries must be >0 to compute geomean
```

#### Existing `sdk/trace` benchmarks

None
This commit is contained in:
Tyler Yahn
2025-09-15 11:18:19 -07:00
committed by GitHub
parent 6243f21805
commit e4ab314112
6 changed files with 508 additions and 103 deletions

View File

@@ -25,11 +25,14 @@ import (
sdkmetric "go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/metric/metricdata"
"go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest"
"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"
)
const componentID = 0
type testBatchExporter struct {
mu sync.Mutex
spans []ReadOnlySpan
@@ -693,6 +696,9 @@ func TestBatchSpanProcessorMetricsDisabled(t *testing.T) {
}
func TestBatchSpanProcessorMetrics(t *testing.T) {
// Reset for deterministic component ID.
processorIDCounter.Store(componentID)
t.Setenv("OTEL_GO_X_OBSERVABILITY", "true")
tp := basicTracerProvider(t)
reader := sdkmetric.NewManualReader()
@@ -710,7 +716,6 @@ func TestBatchSpanProcessorMetrics(t *testing.T) {
WithMaxQueueSize(2),
WithMaxExportBatchSize(2),
)
internalBsp := bsp.(*batchSpanProcessor)
tp.RegisterSpanProcessor(bsp)
tr := tp.Tracer("TestBatchSpanProcessorMetrics")
@@ -719,15 +724,25 @@ func TestBatchSpanProcessorMetrics(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
assert.NoError(t, me.waitForSpans(ctx, 2))
assertObsScopeMetrics(t, internalBsp.componentNameAttr, reader,
expectMetrics{queueCapacity: 2, queueSize: 0, successProcessed: 2})
assertObsScopeMetrics(t, reader, expectMetrics{
queueCapacity: 2,
queueSize: 0,
successProcessed: 2,
})
// Generate 3 spans. 2 fill the queue, and 1 is dropped because the queue is full.
generateSpan(t, tr, testOption{genNumSpans: 3})
assertObsScopeMetrics(t, internalBsp.componentNameAttr, reader,
expectMetrics{queueCapacity: 2, queueSize: 2, queueFullProcessed: 1, successProcessed: 2})
assertObsScopeMetrics(t, reader, expectMetrics{
queueCapacity: 2,
queueSize: 2,
queueFullProcessed: 1,
successProcessed: 2,
})
}
func TestBatchSpanProcessorBlockingMetrics(t *testing.T) {
// Reset for deterministic component ID.
processorIDCounter.Store(componentID)
t.Setenv("OTEL_GO_X_OBSERVABILITY", "true")
tp := basicTracerProvider(t)
reader := sdkmetric.NewManualReader()
@@ -747,7 +762,6 @@ func TestBatchSpanProcessorBlockingMetrics(t *testing.T) {
WithMaxQueueSize(2),
WithMaxExportBatchSize(2),
)
internalBsp := bsp.(*batchSpanProcessor)
tp.RegisterSpanProcessor(bsp)
tr := tp.Tracer("TestBatchSpanProcessorBlockingMetrics")
@@ -756,23 +770,33 @@ func TestBatchSpanProcessorBlockingMetrics(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
assert.NoError(t, me.waitForSpans(ctx, 2))
assertObsScopeMetrics(t, internalBsp.componentNameAttr, reader,
expectMetrics{queueCapacity: 2, queueSize: 0, successProcessed: 2})
assertObsScopeMetrics(t, reader, expectMetrics{
queueCapacity: 2,
queueSize: 0,
successProcessed: 2,
})
// Generate 2 spans to fill the queue.
generateSpan(t, tr, testOption{genNumSpans: 2})
go func() {
// Generate a span which blocks because the queue is full.
generateSpan(t, tr, testOption{genNumSpans: 1})
}()
assertObsScopeMetrics(t, internalBsp.componentNameAttr, reader,
expectMetrics{queueCapacity: 2, queueSize: 2, successProcessed: 2})
assertObsScopeMetrics(t, reader, expectMetrics{
queueCapacity: 2,
queueSize: 2,
successProcessed: 2,
})
// Use ForceFlush to force the span that is blocking on the full queue to be dropped.
ctx, cancel = context.WithTimeout(context.Background(), 10*time.Millisecond)
defer cancel()
assert.Error(t, tp.ForceFlush(ctx))
assertObsScopeMetrics(t, internalBsp.componentNameAttr, reader,
expectMetrics{queueCapacity: 2, queueSize: 2, queueFullProcessed: 1, successProcessed: 2})
assertObsScopeMetrics(t, reader, expectMetrics{
queueCapacity: 2,
queueSize: 2,
queueFullProcessed: 1,
successProcessed: 2,
})
}
type expectMetrics struct {
@@ -782,13 +806,16 @@ type expectMetrics struct {
queueFullProcessed int64
}
func assertObsScopeMetrics(t *testing.T, componentNameAttr attribute.KeyValue, reader sdkmetric.Reader,
func assertObsScopeMetrics(
t *testing.T,
reader sdkmetric.Reader,
expectation expectMetrics,
) {
t.Helper()
gotResourceMetrics := new(metricdata.ResourceMetrics)
assert.NoError(t, reader.Collect(context.Background(), gotResourceMetrics))
componentNameAttr := observ.BSPComponentName(componentID)
baseAttrs := attribute.NewSet(
semconv.OTelComponentTypeBatchingSpanProcessor,
componentNameAttr,
@@ -832,7 +859,7 @@ func assertObsScopeMetrics(t *testing.T, componentNameAttr attribute.KeyValue, r
Attributes: attribute.NewSet(
semconv.OTelComponentTypeBatchingSpanProcessor,
componentNameAttr,
semconv.ErrorTypeKey.String(string(queueFull)),
observ.ErrQueueFull,
),
})
}
@@ -854,9 +881,9 @@ func assertObsScopeMetrics(t *testing.T, componentNameAttr attribute.KeyValue, r
wantScopeMetric := 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: wantMetrics,
}