From bf624c45ba3153a6d8abb884d4bf39327263f92a Mon Sep 17 00:00:00 2001 From: Melchior Moulin Date: Tue, 5 Jul 2022 14:11:18 +0200 Subject: [PATCH] fix suggestions Error handling context propagation threw env var refacto initTracerProvider --- cmd/main.go | 39 ++++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index 1d06c4e..367e3f1 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -7,10 +7,10 @@ package main import ( "context" "flag" + "fmt" "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/exporters/jaeger" "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc" - "go.opentelemetry.io/otel/propagation" "go.opentelemetry.io/otel/sdk/resource" sdktrace "go.opentelemetry.io/otel/sdk/trace" "go.opentelemetry.io/otel/trace" @@ -43,39 +43,44 @@ var ( }, nil) ) - func initTracerProvider(exporterType string) *sdktrace.TracerProvider { + if exporterType == "" { + tp := trace.NewNoopTracerProvider() + otel.SetTracerProvider(tp) + return nil + } resources, _ := resource.New(context.Background(), resource.WithFromEnv(), // pull attributes from OTEL_RESOURCE_ATTRIBUTES and OTEL_SERVICE_NAME environment variables resource.WithProcess(), // This option configures a set of Detectors that discover process information ) - otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{})) - if exporterType != "" { - exporter := exporterTracing(exporterType) - tp := sdktrace.NewTracerProvider( - sdktrace.WithResource(resources), - sdktrace.WithBatcher(exporter), - ) - otel.SetTracerProvider(tp) - return tp - } - otel.SetTracerProvider(trace.NewNoopTracerProvider()) - return nil + exporter := exporterTracing(exporterType) + tp := sdktrace.NewTracerProvider( + sdktrace.WithResource(resources), + sdktrace.WithBatcher(exporter), + ) + otel.SetTracerProvider(tp) + return tp + } func exporterTracing(exporterType string) sdktrace.SpanExporter { //Could not use OTEL_TRACES_EXPORTER https://github.com/open-telemetry/opentelemetry-go/issues/2310 var exporter sdktrace.SpanExporter + var err error //If the env variables needed are defined we set the exporter to Jaeger else it is an opentelemetry exporter if exporterType == "jaeger" { - exporter, _ = jaeger.New(jaeger.WithAgentEndpoint()) //from env variable https://github.com/open-telemetry/opentelemetry-go/tree/main/exporters/jaeger#environment-variables + exporter, err = jaeger.New(jaeger.WithAgentEndpoint()) //from env variable https://github.com/open-telemetry/opentelemetry-go/tree/main/exporters/jaeger#environment-variables } else if exporterType == "otlp" { - exporter, _ = otlptracegrpc.New(context.Background()) //from env variable https://github.com/open-telemetry/opentelemetry-go/blob/main/exporters/otlp/otlptrace/README.md + exporter, err = otlptracegrpc.New(context.Background()) //from env variable https://github.com/open-telemetry/opentelemetry-go/blob/main/exporters/otlp/otlptrace/README.md + } + if err != nil { + panic(fmt.Errorf("error creating tracing exporter %s", err)) } return exporter } -var saramaLogger = log.New(io.Discard, "[Sarama] ", log.Ldate | log.Lmicroseconds) + +var saramaLogger = log.New(io.Discard, "[Sarama] ", log.Ldate|log.Lmicroseconds) func main() {