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

Inject OTEL provider into command context #53

Closed
Closed
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
28 changes: 19 additions & 9 deletions cobraotel/cobraotel.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ import (
semconv "go.opentelemetry.io/otel/semconv/v1.7.0"
)

type contextKey string

const TracerProviderContextKey contextKey = "otel-tracer-provider"

// Option is function used to configure OpenTelemetry within a Cobra RunFunc.
type Option func(*Builder)

Expand Down Expand Up @@ -106,6 +110,7 @@ func (b *Builder) RunE() cobrautil.CobraRunFunc {
otel.SetLogger(b.logger)
}

var tracerProvider *trace.TracerProvider
var exporter trace.SpanExporter
var err error

Expand All @@ -124,12 +129,13 @@ func (b *Builder) RunE() cobrautil.CobraRunFunc {
if insecure {
opts = append(opts, otlptracehttp.WithInsecure())
}
exporter, err = otlptrace.New(context.Background(), otlptracehttp.NewClient(opts...))
exporter, err = otlptrace.New(cmd.Context(), otlptracehttp.NewClient(opts...))
if err != nil {
return err
}

if err := initOtelTracer(exporter, serviceName, propagators, sampleRatio); err != nil {
tracerProvider, err = initOtelTracer(exporter, serviceName, propagators, sampleRatio)
if err != nil {
return err
}
case "otlpgrpc":
Expand All @@ -141,18 +147,21 @@ func (b *Builder) RunE() cobrautil.CobraRunFunc {
opts = append(opts, otlptracegrpc.WithInsecure())
}

exporter, err = otlptrace.New(context.Background(), otlptracegrpc.NewClient(opts...))
exporter, err = otlptrace.New(cmd.Context(), otlptracegrpc.NewClient(opts...))
if err != nil {
return err
}

if err := initOtelTracer(exporter, serviceName, propagators, sampleRatio); err != nil {
tracerProvider, err = initOtelTracer(exporter, serviceName, propagators, sampleRatio)
if err != nil {
return err
}
default:
return fmt.Errorf("unknown tracing provider: %s", provider)
}

cmd.SetContext(context.WithValue(cmd.Context(), TracerProviderContextKey, tracerProvider))

b.logger.V(b.preRunLevel).Info(
"configured opentelemetry tracing",
"provider", provider,
Expand All @@ -165,25 +174,26 @@ func (b *Builder) RunE() cobrautil.CobraRunFunc {
}
}

func initOtelTracer(exporter trace.SpanExporter, serviceName string, propagators []string, sampleRatio float64) error {
func initOtelTracer(exporter trace.SpanExporter, serviceName string, propagators []string, sampleRatio float64) (*trace.TracerProvider, error) {
res, err := resource.New(
context.Background(),
resource.WithAttributes(semconv.ServiceNameKey.String(serviceName)),
resource.WithFromEnv(),
resource.WithTelemetrySDK(),
)
if err != nil {
return err
return nil, err
}

otel.SetTracerProvider(trace.NewTracerProvider(
provider := trace.NewTracerProvider(
trace.WithSampler(trace.ParentBased(trace.TraceIDRatioBased(sampleRatio))),
trace.WithBatcher(exporter),
trace.WithResource(res),
))
)
otel.SetTracerProvider(provider)
setTracePropagators(propagators)

return nil
return provider, nil
}

// setTextMapPropagator sets the OpenTelemetry trace propagation format.
Expand Down