TracerProvider in OpenTelemetry: setup, sampling, export and Shutdown
This beginner's article explains TracerProvider in OpenTelemetry with examples in Go, tested with Go 1.27.3, the OpenTelemetry Go SDK and the stdout exporter v1.47.0. TracerProvider holds the trace settings and gives a Tracer to each part of the application, so the rules and destination are set once at startup instead of in every handler. A Tracer starts a span[1] with Start and ends it with End.
A sampler[2] decides which spans are recorded, and this is decided when a span starts. The default is ParentBased with AlwaysSample, and a TracerProvider accepts only one sampler, so conditions are combined inside it, for example ParentBased(TraceIDRatioBased(0.1)) records 10% of spans. A span processor[3] passes ended spans to an exporter: WithSyncer sends them one at a time and suits testing, while WithBatcher collects them and sends them in batches and is recommended for production. Several span processors can be registered.
An exporter sends spans out of the application, for example to a collector over OTLP[4], or prints them as JSON to standard output for local checks. Shutdown waits for pending spans to be sent before the application exits, because spans still in memory are lost without it. The article notes that a failed send or a timeout can leave some spans unsent.
Website OpenTelemetry opentelemetry.io
Open source observability framework that provides APIs, libraries and agents for collecting traces, metrics and logs from cloud native software.
Sources
-
JPZenn JA
OpenTelemetryのTracerProviderとは



