Documentation
¶
Overview ¶
Package observability wires OpenTelemetry traces, metrics, and logs to an OTLP/HTTP collector with one call, mirroring the env-var contract of the @digiform-by-gs/observability Node package.
Unlike the Node package there is no preload entry point, because Go instrumentation is explicit wrapping rather than module patching — the init-order problem simply does not exist here. Wrap your handlers and clients; nothing is monkey-patched behind your back.
Index ¶
- func Meter(name string, opts ...metric.MeterOption) metric.Meter
- func Tracer(name string, opts ...trace.TracerOption) trace.Tracer
- type Config
- type Observability
- type Option
- func WithEndpoint(v string) Option
- func WithEnvironment(v string) Option
- func WithHeaders(headers map[string]string) Option
- func WithLogLevel(v string) Option
- func WithMetricInterval(d time.Duration) Option
- func WithResourceAttributes(attrs map[string]string) Option
- func WithServiceName(v string) Option
- func WithServiceVersion(v string) Option
- func WithoutRuntimeMetrics() Option
- func WithoutStdoutLogs() Option
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Meter ¶
func Meter(name string, opts ...metric.MeterOption) metric.Meter
Meter returns a named meter from the global provider.
Create instruments once at package or struct level, never per request, and keep attribute values low-cardinality — every distinct combination is a separate series, and Mimir now rejects writes past the configured limits.
func Tracer ¶
func Tracer(name string, opts ...trace.TracerOption) trace.Tracer
Tracer returns a named tracer from the global provider — the analogue of the Node package's getTracer().
These read the global providers rather than the Observability struct, so packages deep in your service can obtain a tracer without threading the struct through every constructor. They are safe to call before New(): you get a no-op that starts working once New() registers the real providers.
Types ¶
type Config ¶
type Config struct {
ServiceName string
ServiceVersion string
Environment string
Endpoint string
ResourceAttributes map[string]string
MetricInterval time.Duration
LogLevel string
// Headers are sent on every OTLP export request (all three signals) —
// e.g. {"Authorization": "Bearer <key>"} for an authenticated collector.
//
// Deliberately NOT resolved from OTEL_EXPORTER_OTLP_HEADERS here: the
// exporters read that env var themselves, including the per-signal
// OTEL_EXPORTER_OTLP_TRACES_HEADERS overrides. Parsing it in the wrapper
// would shadow that spec behaviour.
//
// DIVERGENCE FROM NODE — setting this REPLACES any headers that came from
// OTEL_EXPORTER_OTLP_HEADERS rather than merging with them, because
// upstream's otlpconfig.WithHeaders assigns the map outright. The Node
// package's exporters merge. Use one mechanism or the other, never both:
// mixing them silently drops the env-only headers in Go and keeps them in
// Node. Guarded by TestOTLPHeadersOptionReplacesEnvironment.
Headers map[string]string
// DisableRuntimeMetrics turns off Go runtime instrumentation (GC pauses,
// goroutine count, heap). Left on by default: those series are the single
// best predictor of a p99 that is about to get worse.
DisableRuntimeMetrics bool
// DisableStdoutLogs stops mirroring log records to stdout. The mirror
// exists so logs remain visible when the collector is unreachable.
DisableStdoutLogs bool
}
Config is the resolved configuration. Field-for-field this mirrors ResolvedConfig in packages/observability/src/types.ts so that a service author moving between the Go and Node stacks configures them identically.
type Observability ¶
type Observability struct {
// contains filtered or unexported fields
}
Observability owns the SDK providers. Hold it in main and defer Shutdown.
func New ¶
func New(ctx context.Context, opts ...Option) (*Observability, error)
New initialises the SDK and registers the global providers.
It deliberately does NOT install signal handlers. main() owns signal.NotifyContext; a library grabbing SIGTERM fights http.Server.Shutdown and produces truncated shutdowns that are miserable to debug.
func (*Observability) Config ¶
func (o *Observability) Config() Config
Config exposes the resolved configuration (useful for logging what was actually picked up at boot).
func (*Observability) Logger ¶
func (o *Observability) Logger() *slog.Logger
Logger returns the slog.Logger bridged to the OTel Logs API.
Use the Context-suffixed methods — InfoContext, ErrorContext, ... — or the record carries no trace_id. Plain logger.Info compiles and runs and silently produces an uncorrelated log; the sloglint rule in .golangci.yml exists to catch that, and is not optional.
type Option ¶
type Option func(*Config)
Option mutates configuration before defaults are resolved. Options take precedence over environment variables, which take precedence over defaults — the same order as the Node package's resolveConfig.
func WithEndpoint ¶
func WithEnvironment ¶
func WithHeaders ¶
WithHeaders merges extra headers onto every OTLP export request. Mirrors the Node package's `headers` option. Call it more than once and the maps merge.
func WithLogLevel ¶
func WithMetricInterval ¶
func WithResourceAttributes ¶
WithResourceAttributes merges additional resource attributes. Values here win over anything parsed from OTEL_RESOURCE_ATTRIBUTES.
func WithServiceName ¶
func WithServiceVersion ¶
func WithoutRuntimeMetrics ¶
func WithoutRuntimeMetrics() Option
func WithoutStdoutLogs ¶
func WithoutStdoutLogs() Option
Directories
¶
| Path | Synopsis |
|---|---|
|
Package amqp carries OpenTelemetry context across RabbitMQ, which is the one place cross-service correlation has to be built rather than configured.
|
Package amqp carries OpenTelemetry context across RabbitMQ, which is the one place cross-service correlation has to be built rather than configured. |
|
httpx
module
|
|
|
Package redisx wires go-redis to OpenTelemetry.
|
Package redisx wires go-redis to OpenTelemetry. |
|
Package sqlx wires database/sql to OpenTelemetry via XSAM/otelsql.
|
Package sqlx wires database/sql to OpenTelemetry via XSAM/otelsql. |