observability

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2026 License: Apache-2.0 Imports: 30 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExtractNATS

func ExtractNATS(ctx context.Context, msg HeaderHolder) context.Context

ExtractNATS extracts the propagation context from the NATS message header. Use this in the Worker/Subscriber when a message is received.

func InitProvider

func InitProvider(ctx context.Context, serviceName string, cfg ProviderConfig) (func(context.Context) error, http.Handler, error)

InitProvider sets up the OpenTelemetry pipeline, registering only the signals enabled in cfg. Always installs the W3C TraceContext + Baggage propagator (cheap, harmless when traces are off — in-process span extraction still works against a no-op tracer provider).

Returns a shutdown function (always non-nil) and a Prometheus HTTP handler (non-nil iff PrometheusEnabled). The handler reads from a private prometheus.Registry — global registry pollution is avoided.

func InjectNATS

func InjectNATS(ctx context.Context, msg *nats.Msg)

InjectNATS injects the propagation context into the NATS message header. Use this in the API/Producer before publishing.

func NewLogger

func NewLogger(component string, level *slog.LevelVar, isJSON bool, otlpSampleRate float64) *slog.Logger

NewLogger creates a production-ready logger with component tags and trace support. otlpSampleRate (in [0.0, 1.0]) caps OTLP export of DEBUG/INFO records; WARN/ERROR always export at 100% — dropping them silently during incidents is too dangerous to expose as a knob. Stdout receives 100% of records regardless.

func RegisterSystemMetrics

func RegisterSystemMetrics(natsServer *server.Server, dedup dedupe.Deduplicator) error

RegisterSystemMetrics creates asynchronous gauges that periodically pull stats from embedded systems (NATS, Pebble) and push them to OpenTelemetry.

Types

type HeaderHolder

type HeaderHolder interface {
	Headers() nats.Header
}

type ProviderConfig

type ProviderConfig struct {
	TracesEnabled     bool
	TracesSampleRate  float64
	MetricsEnabled    bool
	PrometheusEnabled bool
	LogsEnabled       bool
}

ProviderConfig wires the metrics/traces/logs pipeline. Each output is independently gated; SampleRate values must be in [0.0, 1.0] and only apply to head-based trace sampling. Log sampling is enforced inside NewLogger (per-level).

MetricsEnabled drives the OTLP-push metric exporter; PrometheusEnabled drives the Prometheus exposition reader. Either, both, or neither may be set — the underlying OTel MeterProvider is the shared substrate. When PrometheusEnabled is true InitProvider returns a non-nil promHandler.

The OTLP exporters take no endpoint/TLS/header options here: the OpenTelemetry SDK reads those from the standard OTEL_EXPORTER_OTLP_* env vars — endpoint (with `https://` selecting TLS via system root CAs), a custom CA via _CERTIFICATE, mutual TLS via _CLIENT_CERTIFICATE/_CLIENT_KEY, and auth _HEADERS. A malformed header is logged and skipped by the SDK (fail-soft), not fatal.

type TraceHandler

type TraceHandler struct {
	slog.Handler
}

TraceHandler intercepts every log call to inject OpenTelemetry IDs

func (*TraceHandler) Handle

func (h *TraceHandler) Handle(ctx context.Context, r slog.Record) error

Handle pulls the trace_id and span_id from the context if they exist

func (*TraceHandler) WithAttrs

func (h *TraceHandler) WithAttrs(attrs []slog.Attr) slog.Handler

WithAttrs and WithGroup must re-wrap so chained Logger.With / WithGroup calls keep going through TraceHandler.Handle. Without these, Go promotes the calls to the embedded slog.Handler and the returned handler is the unwrapped inner handler — silently dropping stdout trace-ID injection.

func (*TraceHandler) WithGroup

func (h *TraceHandler) WithGroup(name string) slog.Handler

Jump to

Keyboard shortcuts

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