Skip to content

Commit

Permalink
Start adding metrics-related tests
Browse files Browse the repository at this point in the history
  • Loading branch information
punya committed Aug 23, 2023
1 parent a751442 commit 8ba585b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 14 deletions.
1 change: 1 addition & 0 deletions exporters/autoexport/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ require (
go.opentelemetry.io/otel v1.16.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.16.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlpmetric v0.39.0 // indirect
go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v0.39.0
go.opentelemetry.io/otel/metric v1.16.0 // indirect
go.opentelemetry.io/otel/sdk/metric v0.39.0
go.opentelemetry.io/otel/trace v1.16.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions exporters/autoexport/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.16.0 h1:TVQp/
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.16.0/go.mod h1:I33vtIe0sR96wfrUcilIzLoA3mLHhRmz9S9Te0S3gDo=
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.16.0 h1:iqjq9LAB8aK++sKVcELezzn655JnBNdsDhghU4G/So8=
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.16.0/go.mod h1:hGXzO5bhhSHZnKvrDaXB82Y9DRFour0Nz/KrBh7reWw=
go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v0.39.0 h1:fl2WmyenEf6LYYlfHAtCUEDyGcpwJNqD4dHGO7PVm4w=
go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v0.39.0/go.mod h1:csyQxQ0UHHKVA8KApS7eUO/klMO5sd/av5CNZNU4O6w=
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.16.0 h1:+XWJd3jf75RXJq29mxbuXhCXFDG3S3R4vBUeSI2P7tE=
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.16.0/go.mod h1:hqgzBPTf4yONMFgdZvL/bK42R/iinTyVQtiWihs3SZc=
go.opentelemetry.io/otel/metric v1.16.0 h1:RbrpwVG1Hfv85LgnZ7+txXioPDoh6EdbZHo26Q3hqOo=
Expand Down
56 changes: 42 additions & 14 deletions exporters/autoexport/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,21 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"go.opentelemetry.io/otel/exporters/stdout/stdoutmetric"
"go.opentelemetry.io/otel/exporters/stdout/stdouttrace"
"go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/trace"
)

var stdoutFactory = func(ctx context.Context) (trace.SpanExporter, error) {
var stdoutMetricFactory = func(ctx context.Context) (metric.Reader, error) {
exp, err := stdoutmetric.New()
if err != nil {
return nil, err
}
return metric.NewPeriodicReader(exp), nil
}

var stdoutSpanFactory = func(ctx context.Context) (trace.SpanExporter, error) {
exp, err := stdouttrace.New()
if err != nil {
return nil, err
Expand All @@ -36,18 +46,36 @@ var stdoutFactory = func(ctx context.Context) (trace.SpanExporter, error) {
}

func TestCanStoreExporterFactory(t *testing.T) {
r := newSpanExporterRegistry()
assert.NotPanics(t, func() {
require.NoError(t, r.store("first", stdoutFactory))
t.Run("spans", func(t *testing.T) {
r := newSpanExporterRegistry()
assert.NotPanics(t, func() {
require.NoError(t, r.store("first", stdoutSpanFactory))
})
})
t.Run("metrics", func(t *testing.T) {
r := newMetricReaderRegistry()
assert.NotPanics(t, func() {
require.NoError(t, r.store("first", stdoutMetricFactory))
})
})
}

func TestLoadOfUnknownExporterReturnsError(t *testing.T) {
r := newSpanExporterRegistry()
assert.NotPanics(t, func() {
exp, err := r.load(context.Background(), "non-existent")
assert.Equal(t, err, errUnknownExporter, "empty registry should hold nothing")
assert.Nil(t, exp, "non-nil exporter returned")
t.Run("spans", func(t *testing.T) {
r := newSpanExporterRegistry()
assert.NotPanics(t, func() {
exp, err := r.load(context.Background(), "non-existent")
assert.Equal(t, err, errUnknownExporter, "empty registry should hold nothing")
assert.Nil(t, exp, "non-nil exporter returned")
})
})
t.Run("metrics", func(t *testing.T) {
r := newMetricReaderRegistry()
assert.NotPanics(t, func() {
exp, err := r.load(context.Background(), "non-existent")
assert.Equal(t, err, errUnknownExporter, "empty registry should hold nothing")
assert.Nil(t, exp, "non-nil exporter returned")
})
})
}

Expand All @@ -56,7 +84,7 @@ func TestRegistryIsConcurrentSafe(t *testing.T) {

r := newSpanExporterRegistry()
assert.NotPanics(t, func() {
require.NoError(t, r.store(exporterName, stdoutFactory))
require.NoError(t, r.store(exporterName, stdoutSpanFactory))
})

var wg sync.WaitGroup
Expand All @@ -65,7 +93,7 @@ func TestRegistryIsConcurrentSafe(t *testing.T) {
go func() {
defer wg.Done()
assert.NotPanics(t, func() {
require.ErrorIs(t, r.store(exporterName, stdoutFactory), errDuplicateRegistration)
require.ErrorIs(t, r.store(exporterName, stdoutSpanFactory), errDuplicateRegistration)
})
}()

Expand Down Expand Up @@ -107,7 +135,7 @@ func TestDefaultOTLPExporterFactoriesAreAutomaticallyRegistered(t *testing.T) {

func TestEnvRegistryCanRegisterExporterFactory(t *testing.T) {
const exporterName = "custom"
RegisterSpanExporter(exporterName, stdoutFactory)
RegisterSpanExporter(exporterName, stdoutSpanFactory)
t.Cleanup(func() { spanExporterRegistry.drop(exporterName) })

exp, err := spanExporterRegistry.load(context.Background(), exporterName)
Expand All @@ -117,11 +145,11 @@ func TestEnvRegistryCanRegisterExporterFactory(t *testing.T) {

func TestEnvRegistryPanicsOnDuplicateRegisterCalls(t *testing.T) {
const exporterName = "custom"
RegisterSpanExporter(exporterName, stdoutFactory)
RegisterSpanExporter(exporterName, stdoutSpanFactory)
t.Cleanup(func() { spanExporterRegistry.drop(exporterName) })

errString := fmt.Sprintf("%s: %q", errDuplicateRegistration, exporterName)
assert.PanicsWithError(t, errString, func() {
RegisterSpanExporter(exporterName, stdoutFactory)
RegisterSpanExporter(exporterName, stdoutSpanFactory)
})
}

0 comments on commit 8ba585b

Please sign in to comment.