Skip to content

Getting started

This walkthrough stands up an OTLP tracer provider, wires it into the global OpenTelemetry API, and shuts it down cleanly. Metrics and logs follow the same shape.

Install

go get gitlab.com/phpboyscout/go/observability

The module carries the OpenTelemetry SDK and cockroachdb/errors — nothing else.

Build a tracer provider

Two ingredients: a resource identifying your service, and a Settings describing the OTLP target. Then tracing.NewProvider assembles the exporter and provider:

package main

import (
    "context"
    "log"

    "go.opentelemetry.io/otel"

    "gitlab.com/phpboyscout/go/observability/otelcore"
    "gitlab.com/phpboyscout/go/observability/tracing"
)

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

    res := otelcore.Resource("my-service", "1.4.0")

    settings := otelcore.Settings{
        Enabled:  true,
        Endpoint: "https://otel-collector.internal:4318",
    }

    tp, err := tracing.NewProvider(ctx, res, settings, tracing.WithSampling(0.1))
    if err != nil {
        log.Fatal(err)
    }
    defer func() { _ = tp.Shutdown(ctx) }()

    otel.SetTracerProvider(tp)

    // ... your service; spans now export to the collector.
}

WithSampling(0.1) samples 10% of traces; omit it for the provider's default.

Let the environment configure it

Leave Endpoint empty and the exporter falls back to the standard OpenTelemetry environment variables — so an operator can point the service at a collector without touching your config:

settings := otelcore.Settings{Enabled: true} // Endpoint == "" ⇒ OTEL_* env fallback
export OTEL_EXPORTER_OTLP_ENDPOINT=https://otel-collector.internal:4318

This is deliberate, not an oversight — see the config model.

Next steps