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: OTLP exporter defaults to http/protobuf protocol instead of grpc #4100

Merged
Merged
Show file tree
Hide file tree
Changes from all 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)
- The `go.opentelemetry.io/contrib/exporters/autoexport` package to provide configuration of trace exporters with useful defaults and envar support. (#2753, #4100)

### Fixed

Expand Down
4 changes: 2 additions & 2 deletions exporters/autoexport/exporter.go
Expand Up @@ -90,8 +90,8 @@ func NewSpanExporter(ctx context.Context, opts ...Option) (trace.SpanExporter, e
// environment variable.
// nil is returned if no exporter is defined for the environment variable.
func makeExporterFromEnv(ctx context.Context) (trace.SpanExporter, error) {
expType, defined := os.LookupEnv(otelTracesExportersEnvKey)
if !defined {
expType := os.Getenv(otelTracesExportersEnvKey)
if expType == "" {
return nil, nil
}
return spanExporter(ctx, expType)
Expand Down
60 changes: 56 additions & 4 deletions exporters/autoexport/exporter_test.go
Expand Up @@ -16,6 +16,7 @@ package autoexport

import (
"context"
"reflect"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -26,7 +27,7 @@ import (

func TestOTLPExporterReturnedWhenNoEnvOrFallbackExporterConfigured(t *testing.T) {
exporter, err := NewSpanExporter(context.Background())
assert.Nil(t, err)
assert.NoError(t, err)
assert.NotNil(t, exporter)
assert.IsType(t, &otlptrace.Exporter{}, exporter)
}
Expand All @@ -37,7 +38,7 @@ func TestFallbackExporterReturnedWhenNoEnvExporterConfigured(t *testing.T) {
context.Background(),
WithFallbackSpanExporter(testExporter),
)
assert.Nil(t, err)
assert.NoError(t, err)
assert.Equal(t, testExporter, exporter)
}

Expand All @@ -49,8 +50,59 @@ func TestEnvExporterIsPreferredOverFallbackExporter(t *testing.T) {
context.Background(),
WithFallbackSpanExporter(testExporter),
)
assert.Nil(t, err)
assert.IsType(t, &otlptrace.Exporter{}, exporter)
assert.NoError(t, err)
assertOTLPHTTPExporter(t, exporter)
}

func TestEnvExporterOTLPOverHTTP(t *testing.T) {
t.Setenv("OTEL_TRACES_EXPORTER", "otlp")
t.Setenv("OTEL_EXPORTER_OTLP_PROTOCOL", "http/protobuf")

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

func TestEnvExporterOTLPOverGRPC(t *testing.T) {
t.Setenv("OTEL_TRACES_EXPORTER", "otlp")
t.Setenv("OTEL_EXPORTER_OTLP_PROTOCOL", "grpc")

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

func TestEnvExporterOTLPInvalidProtocol(t *testing.T) {
t.Setenv("OTEL_TRACES_EXPORTER", "otlp")
t.Setenv("OTEL_EXPORTER_OTLP_PROTOCOL", "invalid")

exporter, err := NewSpanExporter(context.Background())
assert.Error(t, err)
assert.Nil(t, exporter)
}

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

if !assert.IsType(t, &otlptrace.Exporter{}, got) {
return
}

// 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)
}

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

if !assert.IsType(t, &otlptrace.Exporter{}, got) {
return
}

// 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)
}

type testExporter struct{}
Expand Down
7 changes: 4 additions & 3 deletions exporters/autoexport/registry.go
Expand Up @@ -130,10 +130,11 @@ func spanExporter(ctx context.Context, name string) (trace.SpanExporter, error)

// buildOTLPExporter creates an OTLP exporter using the environment variable
// OTEL_EXPORTER_OTLP_PROTOCOL to determine the exporter protocol.
// Defaults to http/protobuf protocol.
func buildOTLPExporter(ctx context.Context) (trace.SpanExporter, error) {
proto := "grpc"
if protoStr, ok := os.LookupEnv(otelExporterOTLPProtoEnvKey); ok {
proto = protoStr
proto := os.Getenv(otelExporterOTLPProtoEnvKey)
if proto == "" {
proto = "http/protobuf"
}

switch proto {
Expand Down
11 changes: 6 additions & 5 deletions exporters/autoexport/registry_test.go
Expand Up @@ -75,7 +75,7 @@ func TestRegistryIsConcurrentSafe(t *testing.T) {
defer wg.Done()
assert.NotPanics(t, func() {
exp, err := r.load(context.Background(), exporterName)
assert.Nil(t, err, "missing exporter in registry")
assert.NoError(t, err, "missing exporter in registry")
assert.IsType(t, &stdouttrace.Exporter{}, exp)
})
}()
Expand All @@ -86,12 +86,13 @@ func TestRegistryIsConcurrentSafe(t *testing.T) {
func TestSubsequentCallsToGetExporterReturnsNewInstances(t *testing.T) {
const exporterType = "otlp"
exp1, err := spanExporter(context.Background(), exporterType)
assert.Nil(t, err)
assert.IsType(t, &otlptrace.Exporter{}, exp1)
assert.NoError(t, err)
assertOTLPHTTPExporter(t, exp1)

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

assert.NotSame(t, exp1, exp2)
}

Expand Down