Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

autoexport: Handle OTEL_TRACES_EXPORTER=none #4130

Merged
merged 6 commits into from Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Expand Up @@ -12,7 +12,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

- Add `NewMiddleware` function in `go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp`. (#2964)
- Add the new `go.opentelemetry.io/contrib/instrgen` package to provide auto-generated source code instrumentation. (#3068, #3108)
- The `go.opentelemetry.io/contrib/exporters/autoexport` package to provide configuration of trace exporters with useful defaults and envar support. (#2753, #4100)
- The `go.opentelemetry.io/contrib/exporters/autoexport` package to provide configuration of trace exporters with useful defaults and envar support. (#2753, #4100, #4129)

### Fixed

Expand Down
16 changes: 14 additions & 2 deletions exporters/autoexport/exporter_test.go
Expand Up @@ -28,8 +28,7 @@ import (
func TestOTLPExporterReturnedWhenNoEnvOrFallbackExporterConfigured(t *testing.T) {
exporter, err := NewSpanExporter(context.Background())
assert.NoError(t, err)
assert.NotNil(t, exporter)
assert.IsType(t, &otlptrace.Exporter{}, exporter)
assertOTLPHTTPExporter(t, exporter)
}

func TestFallbackExporterReturnedWhenNoEnvExporterConfigured(t *testing.T) {
Expand All @@ -40,6 +39,7 @@ func TestFallbackExporterReturnedWhenNoEnvExporterConfigured(t *testing.T) {
)
assert.NoError(t, err)
assert.Equal(t, testExporter, exporter)
assert.False(t, IsNone(exporter))
}

func TestEnvExporterIsPreferredOverFallbackExporter(t *testing.T) {
Expand Down Expand Up @@ -81,6 +81,14 @@ func TestEnvExporterOTLPInvalidProtocol(t *testing.T) {
assert.Nil(t, exporter)
}

func TestEnvExporterNone(t *testing.T) {
t.Setenv("OTEL_TRACES_EXPORTER", "none")

exporter, err := NewSpanExporter(context.Background())
assert.NoError(t, err)
assert.True(t, IsNone(exporter))
}

func assertOTLPHTTPExporter(t *testing.T, got trace.SpanExporter) {
t.Helper()

Expand All @@ -91,6 +99,8 @@ func assertOTLPHTTPExporter(t *testing.T, got trace.SpanExporter) {
// Implementation detail hack. This may break when bumping OTLP exporter modules as it uses unexported API.
clientType := reflect.Indirect(reflect.ValueOf(got)).FieldByName("client").Elem().Type().String()
assert.Equal(t, "*otlptracehttp.client", clientType)

assert.False(t, IsNone(got))
}

func assertOTLPGRPCExporter(t *testing.T, got trace.SpanExporter) {
Expand All @@ -103,6 +113,8 @@ func assertOTLPGRPCExporter(t *testing.T, got trace.SpanExporter) {
// Implementation detail hack. This may break when bumping OTLP exporter modules as it uses unexported API.
clientType := reflect.Indirect(reflect.ValueOf(got)).FieldByName("client").Elem().Type().String()
assert.Equal(t, "*otlptracegrpc.client", clientType)

assert.False(t, IsNone(got))
}

type testExporter struct{}
Expand Down
43 changes: 43 additions & 0 deletions exporters/autoexport/noop.go
@@ -0,0 +1,43 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package autoexport // import "go.opentelemetry.io/contrib/exporters/autoexport"

import (
"context"

"go.opentelemetry.io/otel/sdk/trace"
)

// noop is an implementation of trace.SpanExporter that performs no operations.
type noop struct{}

var _ trace.SpanExporter = noop{}

// ExportSpans is part of trace.SpanExporter interface.
func (e noop) ExportSpans(ctx context.Context, spans []trace.ReadOnlySpan) error {
return nil

Check warning on line 30 in exporters/autoexport/noop.go

View check run for this annotation

Codecov / codecov/patch

exporters/autoexport/noop.go#L29-L30

Added lines #L29 - L30 were not covered by tests
}

// Shutdown is part of trace.SpanExporter interface.
func (e noop) Shutdown(ctx context.Context) error {
return nil

Check warning on line 35 in exporters/autoexport/noop.go

View check run for this annotation

Codecov / codecov/patch

exporters/autoexport/noop.go#L34-L35

Added lines #L34 - L35 were not covered by tests
}

// IsNone returns true for the exporter returned by [NewSpanExporter]
// when OTEL_TRACES_EXPORTER environment variable is set to "none".
func IsNone(e trace.SpanExporter) bool {
pellared marked this conversation as resolved.
Show resolved Hide resolved
_, ok := e.(noop)
return ok
}
1 change: 1 addition & 0 deletions exporters/autoexport/registry.go
Expand Up @@ -43,6 +43,7 @@ func newRegistry() registry {
names: map[string]func(context.Context) (trace.SpanExporter, error){
"": buildOTLPExporter,
"otlp": buildOTLPExporter,
"none": func(ctx context.Context) (trace.SpanExporter, error) { return noop{}, nil },
},
}
}
Expand Down
5 changes: 2 additions & 3 deletions exporters/autoexport/registry_test.go
Expand Up @@ -23,7 +23,6 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"go.opentelemetry.io/otel/exporters/otlp/otlptrace"
"go.opentelemetry.io/otel/exporters/stdout/stdouttrace"
"go.opentelemetry.io/otel/sdk/trace"
)
Expand Down Expand Up @@ -99,11 +98,11 @@ func TestSubsequentCallsToGetExporterReturnsNewInstances(t *testing.T) {
func TestDefaultOTLPExporterFactoriesAreAutomaticallyRegistered(t *testing.T) {
exp1, err := spanExporter(context.Background(), "")
assert.Nil(t, err)
assert.IsType(t, &otlptrace.Exporter{}, exp1)
assertOTLPHTTPExporter(t, exp1)

exp2, err := spanExporter(context.Background(), "otlp")
assert.Nil(t, err)
assert.IsType(t, &otlptrace.Exporter{}, exp2)
assertOTLPHTTPExporter(t, exp2)
}

func TestEnvRegistryCanRegisterExporterFactory(t *testing.T) {
Expand Down