Skip to content

Commit

Permalink
refactor: use OTel autoprop package (#1171)
Browse files Browse the repository at this point in the history
* refactor: use OTel autoprop package

* go mod tidy

Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>
Co-authored-by: Tyler Yahn <codingalias@gmail.com>
  • Loading branch information
3 people authored Jul 12, 2022
1 parent 89df9c8 commit be7fe90
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 132 deletions.
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)
}
9 changes: 5 additions & 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 All @@ -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
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
1 change: 1 addition & 0 deletions example/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions example/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit be7fe90

Please sign in to comment.