Skip to content

Commit

Permalink
🔧 #248 Disable span and metrics by default (#254)
Browse files Browse the repository at this point in the history
  • Loading branch information
gernest authored May 12, 2024
1 parent bd03442 commit 321b002
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
34 changes: 32 additions & 2 deletions pkg/telemetry/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
20 changes: 20 additions & 0 deletions pkg/telemetry/telemetry_test.go
Original file line number Diff line number Diff line change
@@ -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())
}

0 comments on commit 321b002

Please sign in to comment.