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

refactor: use OTel autoprop package #1171

Merged
merged 3 commits into from
Jul 12, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 2 additions & 84 deletions distro/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -59,7 +55,6 @@ const (
// Default configuration values.
const (
defaultAccessToken = ""
defaultPropagator = "tracecontext,baggage"
defaultTraceExporter = "otlp"
defaultLogLevel = "info"

Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand Down
41 changes: 0 additions & 41 deletions distro/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
}
5 changes: 1 addition & 4 deletions distro/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions distro/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
4 changes: 1 addition & 3 deletions distro/otel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down