diff --git a/distro/config.go b/distro/config.go index c13d6a58f..d891dc3c1 100644 --- a/distro/config.go +++ b/distro/config.go @@ -18,13 +18,9 @@ import ( "crypto/tls" "fmt" "os" - "strings" "github.com/go-logr/logr" - "go.opentelemetry.io/contrib/propagators/aws/xray" - "go.opentelemetry.io/contrib/propagators/b3" - "go.opentelemetry.io/contrib/propagators/jaeger" - "go.opentelemetry.io/contrib/propagators/ot" + "go.opentelemetry.io/contrib/propagators/autoprop" "go.opentelemetry.io/otel/propagation" "go.opentelemetry.io/otel/sdk/trace" ) @@ -59,7 +55,6 @@ const ( // Default configuration values. const ( defaultAccessToken = "" - defaultPropagator = "tracecontext,baggage" defaultTraceExporter = "otlp" defaultLogLevel = "info" @@ -100,7 +95,7 @@ func newConfig(opts ...Option) *config { // Apply default field values if they were not set. if c.Propagator == nil { - c.setPropagator(envOr(otelPropagatorsKey, defaultPropagator)) + c.Propagator = autoprop.NewTextMapPropagator() } if c.TraceExporterFunc == nil { key := envOr(otelTracesExporterKey, defaultTraceExporter) @@ -117,83 +112,6 @@ func newConfig(opts ...Option) *config { return c } -type nonePropagatorType struct{ propagation.TextMapPropagator } - -// nonePropagator signals the disablement of setting a global -// TextMapPropagator. -var nonePropagator = nonePropagatorType{} - -// propagators maps environment variable values to TextMapPropagator creation -// functions. -var propagators = map[string]func() propagation.TextMapPropagator{ - // W3C Trace Context. - "tracecontext": func() propagation.TextMapPropagator { - return propagation.TraceContext{} - }, - // W3C Baggage - "baggage": func() propagation.TextMapPropagator { - return propagation.Baggage{} - }, - // B3 Single - "b3": func() propagation.TextMapPropagator { - return b3.New(b3.WithInjectEncoding(b3.B3SingleHeader)) - }, - // B3 Multi - "b3multi": func() propagation.TextMapPropagator { - return b3.New(b3.WithInjectEncoding(b3.B3MultipleHeader)) - }, - // Jaeger - "jaeger": func() propagation.TextMapPropagator { - return jaeger.Jaeger{} - }, - // AWS X-Ray. - "xray": func() propagation.TextMapPropagator { - return xray.Propagator{} - }, - // OpenTracing Trace - "ottrace": func() propagation.TextMapPropagator { - return ot.OT{} - }, - // None, explicitly do not set a global propagator. - "none": func() propagation.TextMapPropagator { - return nonePropagator - }, -} - -func (c *config) setPropagator(name string) { - var props []propagation.TextMapPropagator - for _, part := range strings.Split(name, ",") { - factory, ok := propagators[part] - if !ok { - // Skip invalid data. - c.Logger.Info("invalid propagator name", "name", part) - continue - } - - p := factory() - if p == nonePropagator { - // Make sure the disablement of the global propagator does not get - // lost as a composite below. - c.Propagator = p - return - } - props = append(props, p) - } - - switch len(props) { - case 0: - // Default to "tracecontext,baggage". - c.Propagator = propagation.NewCompositeTextMapPropagator( - propagation.TraceContext{}, - propagation.Baggage{}, - ) - case 1: - c.Propagator = props[0] - default: - c.Propagator = propagation.NewCompositeTextMapPropagator(props...) - } -} - // envOr returns the environment variable value associated with key if it // exists, otherwise it returns alt. func envOr(key, alt string) string { diff --git a/distro/config_test.go b/distro/config_test.go index 3fb3a428d..1b666d65c 100644 --- a/distro/config_test.go +++ b/distro/config_test.go @@ -17,13 +17,8 @@ package distro import ( "testing" - "github.com/go-logr/logr" testr "github.com/go-logr/logr/testing" "github.com/stretchr/testify/assert" - "go.opentelemetry.io/contrib/propagators/aws/xray" - "go.opentelemetry.io/contrib/propagators/b3" - "go.opentelemetry.io/contrib/propagators/jaeger" - "go.opentelemetry.io/contrib/propagators/ot" "go.opentelemetry.io/otel/propagation" ) @@ -101,39 +96,3 @@ func testEnvironmentOverrides(t *testing.T, tc *configFieldTest) { }(ev.Key, ev.Value) } } - -func TestSetPropagatorComposite(t *testing.T) { - c := config{Logger: logr.Discard()} - c.setPropagator("tracecontext,baggage,b3,b3multi,jaeger,xray,ottrace,garbage") - assert.Equal(t, propagation.NewCompositeTextMapPropagator( - propagation.TraceContext{}, - propagation.Baggage{}, - b3.New(b3.WithInjectEncoding(b3.B3SingleHeader)), - b3.New(b3.WithInjectEncoding(b3.B3MultipleHeader)), - jaeger.Jaeger{}, - xray.Propagator{}, - ot.OT{}, - ), c.Propagator) -} - -func TestSetPropagatorDefault(t *testing.T) { - c := config{Logger: logr.Discard()} - c.setPropagator("") - assert.Equal(t, propagation.NewCompositeTextMapPropagator( - propagation.TraceContext{}, - propagation.Baggage{}, - ), c.Propagator) -} - -func TestSetPropagatorCompositeWithNone(t *testing.T) { - // Assumes specification as stated: - // - // "none": No automatically configured propagator. - // - // means if "none" is included in the value, no propagator should be - // configured. Therefore, setPropagator needs to return just the - // nonePropagator value to signal this behavior. - c := config{Logger: logr.Discard()} - c.setPropagator("tracecontext,baggage,none") - assert.Equal(t, nonePropagator, c.Propagator) -} diff --git a/distro/go.mod b/distro/go.mod index fa2700396..d0cd449b8 100644 --- a/distro/go.mod +++ b/distro/go.mod @@ -8,10 +8,7 @@ require ( github.com/signalfx/splunk-otel-go v1.0.0 github.com/stretchr/testify v1.8.0 github.com/tonglil/buflogr v0.0.0-20220114010534-d490b3990d7e - go.opentelemetry.io/contrib/propagators/aws v1.8.0 - go.opentelemetry.io/contrib/propagators/b3 v1.8.0 - go.opentelemetry.io/contrib/propagators/jaeger v1.8.0 - go.opentelemetry.io/contrib/propagators/ot v1.8.0 + go.opentelemetry.io/contrib/propagators/autoprop v0.33.0 go.opentelemetry.io/otel v1.8.0 go.opentelemetry.io/otel/exporters/jaeger v1.8.0 go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.8.0 @@ -29,6 +26,10 @@ require ( github.com/golang/protobuf v1.5.2 // indirect github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect + go.opentelemetry.io/contrib/propagators/aws v1.8.0 // indirect + go.opentelemetry.io/contrib/propagators/b3 v1.8.0 // indirect + go.opentelemetry.io/contrib/propagators/jaeger v1.8.0 // indirect + go.opentelemetry.io/contrib/propagators/ot v1.8.0 // indirect go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.8.0 // indirect go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.8.0 // indirect go.opentelemetry.io/otel/trace v1.8.0 // indirect diff --git a/distro/go.sum b/distro/go.sum index 004f25a8a..549aa07d4 100644 --- a/distro/go.sum +++ b/distro/go.sum @@ -175,6 +175,8 @@ go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8= go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= go.opencensus.io v0.22.3/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= go.opencensus.io v0.22.4/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= +go.opentelemetry.io/contrib/propagators/autoprop v0.33.0 h1:vYrciMtw1n9n7OeZ3OF51fORYEGjfHXt7JSbSmgxJsg= +go.opentelemetry.io/contrib/propagators/autoprop v0.33.0/go.mod h1:m1BVaXLEU1iSomaBqQ0tGcEfAQVMD6FI7yGLq5h4rIA= go.opentelemetry.io/contrib/propagators/aws v1.8.0 h1:Ky4pY6IKdGPPSEv/ffkA1/LRBIpz4ohhuOhrk545YQk= go.opentelemetry.io/contrib/propagators/aws v1.8.0/go.mod h1:0ptHDgrpjxekjXayHNG0fTUdFnehgJVQlDk4P7hfDKM= go.opentelemetry.io/contrib/propagators/b3 v1.8.0 h1:l6+IOjo7VzRWuYeKM/uDUkKOQvG22jDV3l8zs9+f1w0= diff --git a/distro/otel.go b/distro/otel.go index 76692aa23..befb2e55e 100644 --- a/distro/otel.go +++ b/distro/otel.go @@ -76,9 +76,7 @@ func Run(opts ...Option) (SDK, error) { c.Logger.Info("SPLUNK_METRICS_ENDPOINT set; not supported by this distro") } - if c.Propagator != nil && c.Propagator != nonePropagator { - otel.SetTextMapPropagator(c.Propagator) - } + otel.SetTextMapPropagator(c.Propagator) if c.TraceExporterFunc == nil { c.Logger.V(1).Info("OTEL_TRACES_EXPORTER set to none/nil: Tracing disabled") diff --git a/example/go.mod b/example/go.mod index 4e11cfbd2..364cffa95 100644 --- a/example/go.mod +++ b/example/go.mod @@ -19,6 +19,7 @@ require ( github.com/golang/protobuf v1.5.2 // indirect github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0 // indirect github.com/signalfx/splunk-otel-go v1.0.0 // indirect + go.opentelemetry.io/contrib/propagators/autoprop v0.33.0 // indirect go.opentelemetry.io/contrib/propagators/aws v1.8.0 // indirect go.opentelemetry.io/contrib/propagators/b3 v1.8.0 // indirect go.opentelemetry.io/contrib/propagators/jaeger v1.8.0 // indirect diff --git a/example/go.sum b/example/go.sum index ab39b47dd..355f5a60e 100644 --- a/example/go.sum +++ b/example/go.sum @@ -177,6 +177,8 @@ go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux v0.33 go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux v0.33.0/go.mod h1:sqJmmInWWqMCphq1GXR9yUMZs5u7aRw/ZM3CKTrpUOw= go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.33.0 h1:Z0lVKLXU+jxGf3ANoh+UWx9Ai5bjpQVnZXI1zEzvqS0= go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.33.0/go.mod h1:U5rUt7Rw6zuORsWNfpMRy8XMNKLrmIlv/4HgLVW/d5M= +go.opentelemetry.io/contrib/propagators/autoprop v0.33.0 h1:vYrciMtw1n9n7OeZ3OF51fORYEGjfHXt7JSbSmgxJsg= +go.opentelemetry.io/contrib/propagators/autoprop v0.33.0/go.mod h1:m1BVaXLEU1iSomaBqQ0tGcEfAQVMD6FI7yGLq5h4rIA= go.opentelemetry.io/contrib/propagators/aws v1.8.0 h1:Ky4pY6IKdGPPSEv/ffkA1/LRBIpz4ohhuOhrk545YQk= go.opentelemetry.io/contrib/propagators/aws v1.8.0/go.mod h1:0ptHDgrpjxekjXayHNG0fTUdFnehgJVQlDk4P7hfDKM= go.opentelemetry.io/contrib/propagators/b3 v1.8.0 h1:l6+IOjo7VzRWuYeKM/uDUkKOQvG22jDV3l8zs9+f1w0=