observability

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2026 License: GPL-3.0 Imports: 31 Imported by: 0

Documentation

Index

Constants

View Source
const LevelCritical slog.Level = slog.LevelError + 4

LevelCritical is emitted for unrecoverable conditions (panics, data corruption, failed invariants). Sits above slog.LevelError so it surfaces above normal error noise in log aggregators. Value mirrors the OTel SeverityFatal range so the otelslog bridge maps it correctly.

Variables

View Source
var StderrSink io.Writer = os.Stderr

StderrSink is the destination for log output configured as "stderr" (log.app.output / log.http.output) when no explicit writer is supplied. Defaults to os.Stderr. The server test suite repoints it at GinkgoWriter (via testutil) so the HTTP access logger — constructed deep inside BuildApp with no injection seam — doesn't interleave JSON lines with spec output.

Functions

func NewContextEnrichingHandler

func NewContextEnrichingHandler(h slog.Handler) slog.Handler

NewContextEnrichingHandler wraps h so that every record emitted through it gets request-scoped attributes pulled from ctx: request_id, user_id, user_email, http.route. Attrs are added only when ctx carries the source; records from non-HTTP code paths remain unchanged.

Types

type Config

type Config struct {
	ServiceName      string
	ServiceVersion   string
	ServiceCommit    string
	ServiceBuildDate string

	// StderrWriter receives human-readable log output. Defaults to os.Stderr.
	// Tests can override with GinkgoWriter.
	StderrWriter io.Writer
}

Config carries service identity + stderr log sink. OTLP endpoint + log level/format are read from the config singleton at Setup time — they're operator-tunable. Service identity comes from -ldflags (version, commit, build date) so it's always a direct arg.

type HTTPLogger

type HTTPLogger struct {
	// contains filtered or unexported fields
}

HTTPLogger emits one access-log line per non-skipped HTTP request to a dedicated sink (stderr or a rotating file) and, when an OTel logger provider is registered, to the OTel logs pipeline. Returned nil when http access logging is disabled by config.

func NewHTTPLogger

func NewHTTPLogger(cfg config.HTTPLog) (*HTTPLogger, error)

NewHTTPLogger constructs an HTTPLogger from cfg. Returns nil, nil when cfg.Enabled is false. Errors when output path is relative or format is not one of "json" or "combined".

func (*HTTPLogger) Close

func (l *HTTPLogger) Close() error

Close flushes and closes the underlying writer. No-op for stderr.

func (*HTTPLogger) Middleware

func (l *HTTPLogger) Middleware(skip SkipFunc) func(http.Handler) http.Handler

Middleware emits one access-log line per non-skipped request, captured on the way out of the handler chain so status/bytes/duration reflect the final response. When the HTTPLogger is nil (disabled), returns a passthrough so callers don't have to branch.

type Shutdown

type Shutdown func(ctx context.Context) error

Shutdown flushes and shuts down all OTel providers (tracer, meter, logger).

func Setup

func Setup(ctx context.Context, cfg Config) (slog.Handler, Shutdown, error)

Setup initializes the full observability pipeline and returns the slog handler the caller must install via slog.SetDefault. The handler fans log records out to:

  • a stderr text/json handler (always), for local visibility and crashes.
  • the OTel logs bridge (when Endpoint is set), for centralized collection.

Traces and metrics providers are also registered as OTel globals when the endpoint is set. All three signals share one resource.

type SkipFunc

type SkipFunc func(*http.Request) bool

SkipFunc returns true when the request should be excluded from access logging. Returns false (or nil SkipFunc) to log all requests.

Jump to

Keyboard shortcuts

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