diff --git a/pkg/telemetry/telemetry.go b/pkg/telemetry/telemetry.go index e583ecb..94f4a77 100644 --- a/pkg/telemetry/telemetry.go +++ b/pkg/telemetry/telemetry.go @@ -10,8 +10,10 @@ import ( "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/propagation" + "go.opentelemetry.io/otel/sdk/metric" sdkmetric "go.opentelemetry.io/otel/sdk/metric" "go.opentelemetry.io/otel/sdk/resource" + "go.opentelemetry.io/otel/sdk/trace" sdktrace "go.opentelemetry.io/otel/sdk/trace" semconv "go.opentelemetry.io/otel/semconv/v1.24.0" "go.uber.org/zap" @@ -63,7 +65,7 @@ func NewTelemetry(cfg *Config) (*Telemetry, error) { resourceAttr..., ) - spanExporter, err := autoexport.NewSpanExporter(context.Background()) + spanExporter, err := newSpanExporter() if err != nil { return nil, err } @@ -77,7 +79,7 @@ func NewTelemetry(cfg *Config) (*Telemetry, error) { otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}, b3.New())) - metricsReader, err := autoexport.NewMetricReader(context.Background()) + metricsReader, err := newMetricReader() if err != nil { return nil, err } @@ -108,3 +110,31 @@ func NewTelemetryMock() *Telemetry { Logger: NewLoggerMock(), } } + +func newMetricReader() (sdkmetric.Reader, error) { + return autoexport.NewMetricReader(context.Background(), + autoexport.WithFallbackMetricReader(func(_ context.Context) (sdkmetric.Reader, error) { + return metric.NewManualReader(), nil + }), + ) +} + +func newSpanExporter() (sdktrace.SpanExporter, error) { + return autoexport.NewSpanExporter(context.Background(), autoexport.WithFallbackSpanExporter( + func(_ context.Context) (sdktrace.SpanExporter, error) { + return noopSpanExporter{}, nil + }, + )) +} + +type noopSpanExporter struct{} + +var _ trace.SpanExporter = noopSpanExporter{} + +func (e noopSpanExporter) ExportSpans(_ context.Context, _ []trace.ReadOnlySpan) error { + return nil +} + +func (e noopSpanExporter) Shutdown(_ context.Context) error { + return nil +} diff --git a/pkg/telemetry/telemetry_test.go b/pkg/telemetry/telemetry_test.go index 2f69ef8..7ee0880 100644 --- a/pkg/telemetry/telemetry_test.go +++ b/pkg/telemetry/telemetry_test.go @@ -1,12 +1,32 @@ package telemetry import ( + "context" "testing" "github.com/stretchr/testify/require" + "go.opentelemetry.io/otel/sdk/metric" ) func TestTelemetry_Creation(t *testing.T) { _, err := NewTelemetry(DefaultConfig()) require.NoError(t, err) } + +func TestDefaults_noopExporters(t *testing.T) { + // By default all otel providers must be noop. Since we don't have otel setup + // in test environment, this test ensures all providers are noop. + mr, err := newMetricReader() + if err != nil { + t.Fatal(err) + } + // ensures we have a noop metric.ManualReader + _ = mr.(*metric.ManualReader).Shutdown(context.Background()) + + se, err := newSpanExporter() + if err != nil { + t.Fatal(err) + } + // ensures we have a noopSpanExporter + _ = se.(noopSpanExporter).Shutdown(context.Background()) +}