Skip to content
This repository has been archived by the owner on Nov 29, 2024. It is now read-only.

Commit

Permalink
fix suggestions
Browse files Browse the repository at this point in the history
Error handling
context propagation threw env var
refacto initTracerProvider
  • Loading branch information
melchiormoulin committed Jul 5, 2022
1 parent 56cc874 commit bf624c4
Showing 1 changed file with 22 additions and 17 deletions.
39 changes: 22 additions & 17 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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() {

Expand Down

0 comments on commit bf624c4

Please sign in to comment.