1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-11-29 23:07:45 +02:00
Files
opentelemetry-go/sdk/trace/internal/observ/observ_test.go
Robert Wu eb7ec3eb96 Simulate failures for histogram creation paths without risking a nil-interface panic (#7518)
Manual reader need to add Observability metric: #7009 .

If the code under test calls meter.Float64Histogram(...), the call will
be dispatched to the embedded mapi.Meter interface field, which is nil →
runtime panic (nil interface method call).

```
go test -timeout 60s -race ./sdk/...
ok  	go.opentelemetry.io/otel/sdk	(cached)
?   	go.opentelemetry.io/otel/sdk/instrumentation	[no test files]
ok  	go.opentelemetry.io/otel/sdk/internal/x	(cached)
ok  	go.opentelemetry.io/otel/sdk/resource	(cached)
ok  	go.opentelemetry.io/otel/sdk/trace	1.995s
?   	go.opentelemetry.io/otel/sdk/trace/internal	[no test files]
ok  	go.opentelemetry.io/otel/sdk/trace/internal/env	(cached)
2025/10/14 23:56:39 internal_logging.go:50: "msg"="Setting meter provider to its current value. No delegate will be configured" "error"="no delegate configured in meter provider"
--- FAIL: TestBSPCallback (0.00s)
panic: runtime error: invalid memory address or nil pointer dereference [recovered, repanicked]
[signal SIGSEGV: segmentation violation code=0x2 addr=0x28 pc=0x1044c8c24]
```

**Resolution**:
Add `Float64Histogram` to `errMeter`

**Context**:


https://github.com/open-telemetry/opentelemetry-go/pull/7500#discussion_r2431050902
2025-10-20 07:50:56 -07:00

103 lines
2.6 KiB
Go

// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package observ_test
import (
"testing"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
mapi "go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/sdk"
"go.opentelemetry.io/otel/sdk/instrumentation"
"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"
)
func setup(t *testing.T) func() metricdata.ScopeMetrics {
t.Setenv("OTEL_GO_X_OBSERVABILITY", "true")
orig := otel.GetMeterProvider()
t.Cleanup(func() { otel.SetMeterProvider(orig) })
reader := metric.NewManualReader()
mp := metric.NewMeterProvider(metric.WithReader(reader))
otel.SetMeterProvider(mp)
return func() metricdata.ScopeMetrics {
var got metricdata.ResourceMetrics
require.NoError(t, reader.Collect(t.Context(), &got))
if len(got.ScopeMetrics) != 1 {
return metricdata.ScopeMetrics{}
}
return got.ScopeMetrics[0]
}
}
func scopeMetrics(metrics ...metricdata.Metrics) metricdata.ScopeMetrics {
return metricdata.ScopeMetrics{
Scope: instrumentation.Scope{
Name: observ.ScopeName,
Version: sdk.Version(),
SchemaURL: observ.SchemaURL,
},
Metrics: metrics,
}
}
func check(t *testing.T, got metricdata.ScopeMetrics, want ...metricdata.Metrics) {
o := []metricdatatest.Option{
metricdatatest.IgnoreTimestamp(),
metricdatatest.IgnoreExemplars(),
}
metricdatatest.AssertEqual(t, scopeMetrics(want...), got, o...)
}
func dPt(set attribute.Set, value int64) metricdata.DataPoint[int64] {
return metricdata.DataPoint[int64]{Attributes: set, Value: value}
}
type errMeterProvider struct {
mapi.MeterProvider
err error
}
func (m *errMeterProvider) Meter(string, ...mapi.MeterOption) mapi.Meter {
return &errMeter{err: m.err}
}
type errMeter struct {
mapi.Meter
err error
}
func (m *errMeter) Int64UpDownCounter(string, ...mapi.Int64UpDownCounterOption) (mapi.Int64UpDownCounter, error) {
return nil, m.err
}
func (m *errMeter) Int64Counter(string, ...mapi.Int64CounterOption) (mapi.Int64Counter, error) {
return nil, m.err
}
func (m *errMeter) Float64Histogram(string, ...mapi.Float64HistogramOption) (mapi.Float64Histogram, error) {
return nil, m.err
}
func (m *errMeter) Int64ObservableUpDownCounter(
string,
...mapi.Int64ObservableUpDownCounterOption,
) (mapi.Int64ObservableUpDownCounter, error) {
return nil, m.err
}
func (m *errMeter) RegisterCallback(mapi.Callback, ...mapi.Observable) (mapi.Registration, error) {
return nil, m.err
}