obs

package module
v0.0.0-...-22bed5c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 27, 2026 License: Apache-2.0 Imports: 34 Imported by: 0

README

obs

standardized OTel configuration - nothing of interest

Documentation

Overview

Package obs is the atomdrift shared OpenTelemetry wiring.

Services call Init once during startup with a Config (zero values fall back to OTEL_* env vars), defer the returned shutdown into their signal-handling path, and then use Meter, Tracer, Job, and the HTTP helpers as needed.

Each OTLP signal (metrics, traces, logs) is enabled only when its endpoint is configured — either via [Config.Endpoint] or via the standard OTEL_EXPORTER_OTLP_*_ENDPOINT env vars. The Prometheus scrape handler exposed by MetricsHandler is always available so callers can mount /_/metrik regardless of OTLP wiring.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Init

func Init(ctx context.Context, cfg Config) (func(context.Context) error, error)

Init wires up OpenTelemetry for the calling service. It is safe to call exactly once per process; a second call returns an error.

Init installs global trace, meter, and logger providers; replaces slog.Default with a handler that tees to stderr and the OTel log pipeline; and enables push exporters only for signals whose endpoint is configured. The returned shutdown should be invoked with a deadline-bearing context during graceful termination.

func Job

func Job(ctx context.Context, name string, fn func(context.Context) error) error

Job runs fn inside a span named after name and records both the duration histogram and an outcome-labeled counter. The returned error is fn's error verbatim, after being recorded on the span.

Safe to call before Init: if instruments are unset, Job still invokes fn but emits no telemetry.

func Meter

func Meter() metric.Meter

Meter returns a Meter scoped to the obs package. Callers that want a service-scoped meter should use otel.Meter("their-package") instead.

func MetricsHandler

func MetricsHandler() http.Handler

MetricsHandler returns the Prometheus scrape handler backed by the OTel meter provider. Mount it at /_/metrik on the service's existing mux. Responds 503 until Init has run.

func Middleware

func Middleware(next http.Handler) http.Handler

Middleware wraps an http.Handler with OTel HTTP server instrumentation: trace propagation, a span per request, and the standard http.server.request.duration / http.server.active_requests metrics. The span name is derived from the matched route pattern when the underlying handler is an http.ServeMux (Go 1.22+); callers using third-party routers should pass a route-aware wrapper.

Safe to call before Init — the underlying otelhttp uses the global tracer/meter, which are no-op until Init replaces them.

func NoOp

func NoOp() func(context.Context) error

NoOp returns a shutdown that does nothing. Useful in tests so test binaries don't try to dial an OTLP endpoint.

func OTLPSlogHandler

func OTLPSlogHandler(serviceName string) slog.Handler

OTLPSlogHandler returns just the OTel-bridge slog handler — records passed through it are exported via the global LoggerProvider. Useful for services with bespoke logging chains (e.g. file + stderr fan-out) that want to add OTLP as one more sink. Compose it into your own fan-out and call slog.SetDefault yourself; pair with Config.DisableSlog = true so Init doesn't fight you.

func PoolStats

func PoolStats(name string, pool *pgxpool.Pool) error

PoolStats wires async gauges that expose pgxpool connection-pool counters under db.pool.*. Call once after the pool is created. Multiple pools may be registered with distinct names (e.g. "hopper", "replica"); the name becomes a "pool" attribute.

Safe to call before Init: the global no-op meter accepts the instrument creation and the callbacks do nothing useful until Init installs the real provider.

func TeeSlog

func TeeSlog(base slog.Handler, serviceName string) slog.Handler

TeeSlog returns a handler that fans every record to base and to the OTel logger provider. The shape of base (stdout vs stderr, JSON vs text, level filter) is preserved exactly. Use this when you already have a configured handler and just want to add OTLP as a second sink.

func Tracer

func Tracer() trace.Tracer

Tracer returns a Tracer scoped to the obs package.

Types

type Config

type Config struct {
	// ServiceName is the OTel service.name resource attribute. Required.
	ServiceName string

	// ServiceVersion is the OTel service.version resource attribute.
	// If empty, falls back to debug.ReadBuildInfo() then "devel".
	ServiceVersion string

	// Environment is the OTel deployment.environment resource attribute.
	// If empty, falls back to OTEL_DEPLOYMENT_ENVIRONMENT then "dev".
	Environment string

	// Endpoint is the OTLP/HTTP base endpoint applied to any signal
	// that does not have a signal-specific OTEL_EXPORTER_OTLP_*_ENDPOINT
	// override. If empty and no per-signal env vars are set, OTLP push
	// is disabled (the SDK still installs no-op providers).
	Endpoint string

	// Insecure forces http:// when true. If false, the URL scheme of the
	// resolved endpoint decides.
	Insecure bool

	// SampleRatio is the trace head-sampling ratio (0.0–1.0). If zero,
	// falls back to OTEL_TRACES_SAMPLER_ARG, then 0.1.
	SampleRatio float64

	// DisableSlog skips Init's slog.SetDefault. Use this when the
	// caller has its own fan-out chain (e.g. stderr + file + OTLP) and
	// wants to compose [OTLPSlogHandler] in itself.
	DisableSlog bool
}

Config controls obs initialization. The zero value is invalid (ServiceName is required); all other fields fall back to env vars or sensible defaults.

Directories

Path Synopsis
cmd
smoketest command
Smoketest exercises obs end-to-end against a local OTLP receiver.
Smoketest exercises obs end-to-end against a local OTLP receiver.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL