Documentation
¶
Overview ¶
Package otel bootstraps the OpenTelemetry SDK for traces, metrics, and logs over OTLP/HTTP. It returns a *Provider that implements lifecycle.Resource so it can be registered with app.App and shut down last in the LIFO chain (allowing other components to flush telemetry before the exporters close).
Typical usage:
p, err := otel.Setup(ctx, otel.Config{
OTelHost: "otel-collector:4318",
ServiceName: "myservice",
EnableTracer: true,
EnableMetrics: true,
})
if err != nil { log.Fatal(err) }
a := app.New()
a.Add(p) // register first → shuts down last → final flush happens AFTER
// db/redis/etc have stopped emitting.
a.Add(httpServer, grpcServer)
Once Setup has run, the global TracerProvider, MeterProvider, and LoggerProvider are non-noop and can be used by every other core/* package via WithOtel() options.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrEmptyOTelHost = errors.New("otel: OTelHost is empty (set Disabled=true to opt out)")
ErrEmptyOTelHost is returned by Setup when Config.Disabled is false but Config.OTelHost is empty. Flip Disabled=true to opt out explicitly.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// Disabled explicitly turns off OTel setup. When true, Setup returns a
// noop Provider regardless of the other fields. Use for local
// development / tests without a collector.
Disabled bool
// OTelHost is the OTLP/HTTP endpoint (host:port). Required when
// Disabled is false.
OTelHost string
// ServiceName is the value of resource.attribute service.name.
// Required when Disabled is false.
ServiceName string
// ServiceVersion populates service.version. Optional.
ServiceVersion string
// HostName populates host.name. Optional.
HostName string
// K8SNamespace populates k8s.namespace.name. Optional.
K8SNamespace string
// K8SNodeName populates k8s.node.name. Optional.
K8SNodeName string
// K8SPodName populates k8s.pod.name. Optional.
K8SPodName string
// EnableTracer turns on the trace provider with an OTLP/HTTP exporter.
EnableTracer bool
// EnableMetrics turns on the meter provider with an OTLP/HTTP exporter
// and a 3s default send interval.
EnableMetrics bool
// EnableLogger turns on the logger provider with an OTLP/HTTP exporter.
EnableLogger bool
}
Config controls which OpenTelemetry signals are enabled and what resource attributes describe the producing service. Plain fields, no struct tags — consumer apps map their viper keys to fields explicitly inside their own config.NewConfig().
To intentionally run without a collector (local dev, tests), set Disabled=true — Setup will return a noop Provider. An empty OTelHost without Disabled=true is treated as a misconfiguration and returns ErrEmptyOTelHost, so a missing environment variable fails loudly instead of silently disabling observability.
type Option ¶ added in v1.3.0
type Option func(*options)
Option configures Setup.
func WithHistogramBuckets ¶ added in v1.3.0
WithHistogramBuckets overrides the explicit histogram bucket boundaries (in milliseconds). Default: [5, 10, 25, 50, 100, 250, 500, 1000, 2500, 5000, 10000].
Tune to match the latency distribution of your service. Buckets that are too coarse hide regressions; too fine wastes storage.
func WithMetricsSendInterval ¶ added in v1.3.0
WithMetricsSendInterval overrides how often metrics are exported. Default: 3 seconds. Smaller values give finer resolution at the cost of more network traffic.
type Provider ¶ added in v1.3.0
type Provider struct {
// contains filtered or unexported fields
}
Provider holds the SDK shutdown closures registered during Setup. Implements lifecycle.Resource so it integrates with app.App.
func Setup ¶ added in v1.3.0
Setup bootstraps the configured OpenTelemetry signals and returns a Provider that owns them. Sets the global propagator (W3C TraceContext + Baggage) and the global TracerProvider / MeterProvider / LoggerProvider for all other otel-aware libraries to discover.
If cfg.Disabled is true, Setup returns a noop Provider. An empty cfg.OTelHost with Disabled=false is an error (ErrEmptyOTelHost) — this prevents silently running without observability because of a missing env var.
On failure, any partially-initialised SDK is shut down before returning.
func (*Provider) Shutdown ¶ added in v1.3.0
Shutdown invokes every registered SDK shutdown in registration order (trace → metrics → logger), joining errors. Implements lifecycle.Resource.
Idempotent and concurrent-safe: the shutdown runs exactly once; every subsequent call returns the same result (nil on clean shutdown, the joined error otherwise).