observe

package
v0.2.10 Latest Latest
Warning

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

Go to latest
Published: Aug 4, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package observe is the ecosystem's request observability: one structured record and one metric observation per served request, applied as a decorator so every serving surface gets the same fields, the same level grading and the same counters rather than each one inventing its own (R17, R18).

It is a decorator rather than a handler feature because the obligation to log belongs to the *server* — the composition root that binds a port — while the handler's job is to route. The destination is always injected: this package owns the observations, never where they go, so a consumer picks slog's handler and any metrics backend without this package depending on either.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Handler

func Handler(inner http.Handler, logger *slog.Logger, metrics Metrics) http.Handler

Handler wraps inner so every request it serves emits one structured record and one metric observation. A nil logger or nil metrics disables that half; wrapping with neither is a no-op decorator, which keeps the seam usable in tests without pretending to observe.

Types

type ByteCount

type ByteCount int64

ByteCount is a response body size in bytes.

type ExpvarMetrics

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

ExpvarMetrics is the zero-dependency Metrics: request counts and total latency per method and status, published through the standard library's expvar registry and readable wherever the process serves /debug/vars.

It is the default rather than a Prometheus client because this package is a library on a public module path: an interface plus a stdlib implementation gives a real, queryable answer today and leaves any backend a consumer prefers a twenty-line adapter away, with nothing added to go.sum.

func NewExpvarMetrics

func NewExpvarMetrics(prefix MetricsPrefix) ExpvarMetrics

NewExpvarMetrics publishes (or adopts, when a prefix repeats) the two maps. The value is copyable: both fields are already pointers into the expvar registry, so every copy counts into the same maps.

func (ExpvarMetrics) RequestServed

func (m ExpvarMetrics) RequestServed(observed Observation)

RequestServed counts the request and adds its latency, keyed by method and status so a reader can see both the shape of the traffic and where the time goes.

type Metrics

type Metrics interface {
	RequestServed(observed Observation)
}

Metrics receives one Observation per served request. Implementations are called from every request goroutine and own their synchronisation; a nil Metrics collects nothing.

type MetricsPrefix

type MetricsPrefix string

MetricsPrefix names an ExpvarMetrics' published variables, so two servers in one process do not share counters.

type Observation

type Observation struct {
	Method   RequestMethod
	Status   StatusCode
	Duration time.Duration
	Bytes    ByteCount
}

Observation is what one served request contributes to metrics: the request's shape and its cost, and nothing drawn from the document itself — an observability surface that carried sheet data would be an exfiltration surface. TestEveryRequestIsLoggedAndMeasured states that by serving a document whose contents it then looks for, and fails on finding.

type RequestMethod

type RequestMethod string

RequestMethod is an HTTP method as observed.

type StatusCode

type StatusCode int

StatusCode is the HTTP status a request was answered with.

Jump to

Keyboard shortcuts

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