Documentation
¶
Overview ¶
Package telemetry sets up OpenTelemetry tracing and metrics for hex applications.
One Setup call wires:
- a tracer provider with the configured exporter,
- a meter provider with the configured exporter,
- a resource carrying service name/version,
- propagation (tracecontext + baggage),
- global registration so any library reading `otel.Tracer(...)` sees them.
The returned Provider carries a single Shutdown method that flushes and closes every configured exporter, ready to defer at the end of main.
See ADR-0014.
Example:
tp, err := telemetry.Setup(ctx, telemetry.Options{
ServiceName: "myapp",
ServiceVersion: build.Version(),
Exporter: telemetry.ExporterStdout,
})
if err != nil { return err }
defer tp.Shutdown(context.Background())
tracer := telemetry.Tracer("myapp/http")
ctx, span := tracer.Start(ctx, "handle-request")
defer span.End()
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Exporter ¶
type Exporter int
Exporter selects the exporter backend.
const ( // ExporterNone disables the exporter entirely — spans/metrics are // discarded. Useful in tests. ExporterNone Exporter = iota // ExporterStdout writes spans and metrics as JSON to stdout. // Default for dev. ExporterStdout // ExporterOTLP ships to an OTLP-compatible collector over gRPC. // Production default. Endpoint is picked from OTEL_EXPORTER_OTLP_ENDPOINT // environment variable per the OTel spec. ExporterOTLP )
type Options ¶
type Options struct {
// ServiceName identifies the service in traces and metrics. Required.
ServiceName string
// ServiceVersion is optional but strongly recommended for correlating
// telemetry to specific builds. Feed hex/build.Version() here.
ServiceVersion string
// Environment tags telemetry with the deployment environment (e.g.
// "dev", "staging", "production"). Optional.
Environment string
// Exporter selects the exporter. Defaults to ExporterStdout.
Exporter Exporter
// ResourceAttributes are additional key/value pairs attached to every
// emitted span and metric.
ResourceAttributes map[string]string
// TraceExporter, MetricExporter override the exporter for one signal.
// Useful when a consumer wants OTLP for traces and stdout for metrics
// (or a custom exporter neither shipped here supports).
TraceExporter sdktrace.SpanExporter
MetricExporter sdkmetric.Exporter
}
Options configures Setup.
type Provider ¶
type Provider struct {
// contains filtered or unexported fields
}
Provider carries the tracer and meter providers plus a Shutdown method that flushes and closes them.
func Setup ¶
Setup configures OTel with opts and returns a Provider whose Shutdown method should be deferred at the end of main.
func (*Provider) MeterProvider ¶
func (p *Provider) MeterProvider() *sdkmetric.MeterProvider
MeterProvider returns the underlying SDK meter provider.
func (*Provider) Resource ¶
Resource returns the attached resource. Handy for asserting attributes in tests.
func (*Provider) Shutdown ¶
Shutdown flushes and closes the tracer and meter providers. Idempotent — subsequent calls after the first are no-ops.
func (*Provider) TracerProvider ¶
func (p *Provider) TracerProvider() *sdktrace.TracerProvider
TracerProvider returns the underlying SDK tracer provider. Escape hatch for consumers that need to add SpanProcessor, override sampling, etc.