Rene Nochebuena 9304bed55b docs(telemetry): correct tier from 5 to 1
telemetry has zero micro-lib dependencies — only the external OTel SDK.
Tier 1 reflects its actual position in the dependency graph. The Tier 5
label was misleading about push/tag ordering; telemetry can be released
independently of all other micro-lib modules.
2026-03-19 06:56:16 -06:00

telemetry

OTel SDK bootstrap — wires up TracerProvider, MeterProvider, and LoggerProvider with OTLP gRPC exporters pointed at Grafana Alloy (→ Tempo / Mimir / Loki).

Tier 5 — import only from application main packages. Never import from framework libraries.

Why Tier 5

Micro-libs use only the OTel API (go.opentelemetry.io/otel) which ships a zero-overhead no-op default. This module activates the real SDK and sets the three OTel globals, so all micro-libs using the global API auto-instrument without knowing about this module.

Installation

require code.nochebuena.dev/go/telemetry v0.1.0

Usage

func main() {
    ctx := context.Background()

    // Bootstrap telemetry before any other component.
    shutdown, err := telemetry.New(ctx, telemetry.Config{
        ServiceName:  "order-service",
        OTLPEndpoint: "alloy:4317",
        OTLPInsecure: true, // dev only
    })
    if err != nil {
        log.Fatalf("telemetry: %v", err)
    }
    defer shutdown(ctx)

    // Start the rest of the application…
}

Config

Field Env var Required Default Description
ServiceName OTEL_SERVICE_NAME Service name in traces/metrics/logs
ServiceVersion OTEL_SERVICE_VERSION unknown Deployed version
Environment OTEL_ENVIRONMENT development Deployment environment
OTLPEndpoint OTEL_EXPORTER_OTLP_ENDPOINT OTLP gRPC address (e.g. alloy:4317)
OTLPInsecure OTEL_EXPORTER_OTLP_INSECURE false Disable TLS (dev only)

What New sets up

Signal SDK Exporter Backend
Traces sdktrace.TracerProvider OTLP gRPC Grafana Alloy → Tempo
Metrics sdkmetric.MeterProvider OTLP gRPC Grafana Alloy → Mimir
Logs sdklog.LoggerProvider OTLP gRPC Grafana Alloy → Loki

Also sets otel.SetTextMapPropagator with W3C TraceContext + Baggage.

Shutdown

The returned func(context.Context) error flushes all pending telemetry and shuts down the three providers. Always call it before process exit.

defer func() {
    shutCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
    defer cancel()
    if err := shutdown(shutCtx); err != nil {
        log.Printf("telemetry shutdown: %v", err)
    }
}()
Description
OpenTelemetry SDK bootstrap with OTLP gRPC exporters for traces, metrics, and logs.
Readme MIT 50 KiB
Release 0.9.0 Latest
2026-03-18 14:14:32 -06:00
Languages
Go 100%