Skip to content

otelconfig provides a helper to quickly initialize OpenTelemetry tracing for a Go application. Then one can use standard OTEL_ env vars to customize tracing behavior.

License

udhos/otelconfig

Repository files navigation

license Go Report Card Go Reference

otelconfig

otelconfig provides a helper to quickly initialize OpenTelemetry tracing for a Go application. Then one can use standard OTEL_ env vars to customize tracing behavior.

Example

See examples/oteltrace-example/main.go.

Usage

import "github.com/udhos/otelconfig/oteltrace"
import "go.opentelemetry.io/otel/trace"

// NOOP env var disables tracing
noopEnv := os.Getenv("NOOP")
noop := noopEnv != ""
log.Printf("NOOP=%s noop=%t", noopEnv, noop)

//
// initialize tracing
//

var tracer trace.Tracer

if noop {
    tracer = oteltrace.NewNoopTracer()
} else {
    options := oteltrace.TraceOptions{
        DefaultService:     "my-program",
        NoopTracerProvider: false,
        Debug:              true,
    }

    tr, cancel, errTracer := oteltrace.TraceStart(options)

    if errTracer != nil {
        log.Fatalf("tracer: %v", errTracer)
    }

    defer cancel()

    tracer = tr
}

// use tracer to create spans

work(context.TODO(), tracer)

// ...

func work(ctx context.Context, tracer trace.Tracer) {
	_, span := tracer.Start(ctx, "work")
	defer span.End()
}

Configuration

General configuration: https://opentelemetry.io/docs/concepts/sdk-configuration/general-sdk-configuration/

Exporter configuration: https://opentelemetry.io/docs/concepts/sdk-configuration/otlp-exporter-configuration/

export OTELCONFIG_EXPORTER=jaeger|grpc|http|stdout  ;#     Protocol    default: grpc
export OTEL_TRACES_EXPORTER=jaeger|otlp             ;#     Data Format default: otlp
export OTEL_PROPAGATORS=b3multi                     ;# [1] Propagator  default: tracecontext,baggage
export OTEL_EXPORTER_OTLP_ENDPOINT=http://host:port ;#     Endpoint    default: [2]

# [1] Propagators: tracecontext,baggage,b3,b3multi,jaeger,xray,ottrace,none
#
# [2] Default endpoint: http://localhost:4317 for grpc
#                       http://localhost:4318 for http
#
# Service name precedence from higher to lower:
# 1. OTEL_SERVICE_NAME=mysrv
# 2. OTEL_RESOURCE_ATTRIBUTES=service.name=mysrv
# 3. TraceOptions.DefaultService="mysrv"

Examples:

# Example for Jaeger
export OTELCONFIG_EXPORTER=jaeger
export OTEL_TRACES_EXPORTER=jaeger
export OTEL_PROPAGATORS=b3multi
export OTEL_EXPORTER_OTLP_ENDPOINT=http://jaeger-collector:14268
oteltrace-example

# Example for gRPC and OTLP
export OTELCONFIG_EXPORTER=grpc
export OTEL_TRACES_EXPORTER=otlp
export OTEL_PROPAGATORS=b3multi
export OTEL_EXPORTER_OTLP_ENDPOINT=http://jaeger-collector:4317
oteltrace-example

# Example for HTTP and OTLP
export OTELCONFIG_EXPORTER=http
export OTEL_TRACES_EXPORTER=otlp
export OTEL_PROPAGATORS=b3multi
export OTEL_EXPORTER_OTLP_ENDPOINT=http://jaeger-collector:4318
oteltrace-example

About

otelconfig provides a helper to quickly initialize OpenTelemetry tracing for a Go application. Then one can use standard OTEL_ env vars to customize tracing behavior.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published