obs

package
v0.111.2 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2026 License: Apache-2.0 Imports: 13 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package obs provides fleet-canonical runtime observability: a localhost-only debug server exposing net/http/pprof plus a /metrics mirror, and a helper to (idempotently) register the Go runtime + process Prometheus collectors.

Why

Diagnosing slow RSS creep, goroutine leaks, and GC pressure before a container OOMs requires pprof. Exposing pprof on a service's public listener is dangerous (heap/profile endpoints leak memory contents and let an attacker pin CPU). This package binds pprof to 127.0.0.1 only, so it is reachable from inside the container / over an SSH tunnel but never from the gateway.

obs is OPT-IN, not auto-loaded. A service that wants pprof + the localhost debug/metrics server imports this package and calls Init() once in main(); services that do not import obs neither compile in net/http/pprof nor start a debug listener. (v0.61.0 briefly auto-started this from server.New; v0.63.0 reverted that — see the Adoption section below.)

Runtime metrics

go-common/promx.Init already registers collectors.NewGoCollector and NewProcessCollector on the shared registry, so every service using go-common/server already exposes go_goroutines, go_memstats_*, go_gc_duration_seconds, and process_resident_memory_bytes at its public GET /metrics. RegisterRuntimeMetrics exists for the rare service that wires its own *prometheus.Registry and wants the same runtime surface without forking the collector wiring; it is AlreadyRegistered-safe, so calling it on a registry that already has the collectors is a harmless no-op.

Adoption

Opt-in, one line in main() — for every service, including those built on go-common/server:

stop, _ := obs.Init()      // honours DEBUG_ADDR; default 127.0.0.1:6060
defer stop()

Disable via DEBUG_ADDR=off or OBS_DISABLE=1. A service that never imports obs pays nothing — no pprof handlers, no debug listener.

Index

Constants

View Source
const DefaultDebugAddr = "127.0.0.1:6060"

DefaultDebugAddr is the bind address used when DEBUG_ADDR is unset. 127.0.0.1 ONLY — pprof must never be reachable off-host.

View Source
const EnvDebugAddr = "DEBUG_ADDR"

EnvDebugAddr is the env var that overrides the debug server bind address. Set to "off" / "disabled" / "0" / "none" (or set OBS_DISABLE) to turn the debug server off entirely.

View Source
const EnvDisable = "OBS_DISABLE"

EnvDisable, when truthy ("1", "true", "yes", "on"), disables the debug server regardless of DEBUG_ADDR.

Variables

This section is empty.

Functions

func RegisterRuntimeMetrics

func RegisterRuntimeMetrics(reg prometheus.Registerer) error

RegisterRuntimeMetrics registers the Go runtime + process Prometheus collectors (go_goroutines, go_memstats_*, go_gc_duration_seconds, process_resident_memory_bytes, …) on reg.

It is idempotent and AlreadyRegistered-safe: if a collector is already present on reg (the common case for go-common/server services, where promx.Init has already registered them on the shared registry) the duplicate is silently ignored and no error is returned. Any other registration error is returned.

Most services do NOT need to call this — promx.Init already did it on the shared registry. It exists for services that build their own *prometheus.Registry and want the standard runtime surface.

Types

type StopFunc

type StopFunc func()

StopFunc gracefully shuts the debug server down. It is always safe to call (including when the server was disabled — then it is a no-op) and safe to call more than once.

func Init

func Init() (StopFunc, error)

Init starts the localhost debug server using the env-resolved address (DEBUG_ADDR, default 127.0.0.1:6060; OBS_DISABLE / DEBUG_ADDR=off to disable). It is the one-liner standalone services add to main():

stop, err := obs.Init()
if err != nil { log.Printf("obs: debug server not started: %v", err) }
defer stop()

When disabled it returns a no-op StopFunc and a nil error, so callers never need to special-case the disabled path. Call this once in main(); it is opt-in and is NOT started automatically by go-common/server.

func MustInit

func MustInit() StopFunc

MustInit is Init but panics on error. Prefer Init in production; this is a convenience for short-lived tools where a failed pprof bind should be loud.

func StartDebugServer

func StartDebugServer(addr string) (StopFunc, error)

StartDebugServer starts a localhost-only HTTP server serving net/http/pprof under /debug/pprof/* and a Prometheus /metrics mirror of the shared promx registry, bound to addr.

Security: addr SHOULD be loopback (127.0.0.1 / [::1]). pprof exposes heap contents and lets a caller pin CPU; it must not be public. A non-loopback addr is honoured (some operators tunnel via a sidecar) but is the caller's explicit risk.

If addr is empty the server is disabled and a no-op StopFunc + nil error is returned — this is the env-driven "off" path. Otherwise the listener is bound synchronously (so a bind failure is returned to the caller, not lost on a background goroutine) and served on a goroutine.

Jump to

Keyboard shortcuts

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