Help us shape the future of Go! Take the 2026 Go Developer Survey

ops

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package ops holds the operational parts of serving a site that are WebThings' own: its metrics in the Prometheus text format, with the access log, and the caching policy of public pages. The HTTP parts, such as compression, ETags, request IDs, the client's address and panic recovery, are go-router's middleware; App (webthings.go) puts them around its routes.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Observe

func Observe(m *Metrics, log *slog.Logger, class func(*http.Request) string) func(http.Handler) http.Handler

Observe counts every request in metrics under the route class class gives it and, when log is not nil, writes an access log line for it: method, path (without the query, which may hold tokens), status, size, duration, route class and request ID. It is go-router's Observe, so a request is recorded also when a panic passes through, such as http.ErrAbortHandler after the response started: as a 500 when no status was written yet. The panic goes on.

func PageCache

func PageCache(policy func(*http.Request) CachePolicy) func(http.Handler) http.Handler

PageCache is go-router's ETag middleware with the Cache-Control of policy: successful HTML responses to GET and HEAD requests, and the site's Atom and RSS feeds, get an ETag, the hash of their body, and requests that name it in If-None-Match get 304 Not Modified. Responses without a Cache-Control of their own get the one of policy. Responses that are private or not to be stored (pages for signed-in editors, forms with a visitor's result) are left as they are, and so are responses that set a cookie, are streamed (flushed) or carry an ETag already.

Inside it, go-router's LastModified answers a request with If-Modified-Since and no If-None-Match with 304 Not Modified when the response has a Last-Modified that is not newer, such as the feeds' (frontend.Feed). Pages send no Last-Modified, so they are answered by their ETag alone.

Types

type CachePolicy

type CachePolicy struct {
	MaxAge int
	Stale  int
}

CachePolicy is how long browsers and caches may keep public pages for visitors, in seconds. MaxAge zero has them ask the site every time, which the ETag makes cheap: an unchanged page is answered with 304 Not Modified. Stale is how long after MaxAge a cache may still show a page while it fetches a fresh one (stale-while-revalidate).

func (CachePolicy) CacheControl

func (p CachePolicy) CacheControl(r *http.Request) string

CacheControl is the Cache-Control header of the policy for a request: "no-cache" when pages must be checked every time, also for addresses with a query, which often show the result of a visitor's action (such as ?comment=sent) or a search; otherwise public with max-age and stale-while-revalidate.

type Exposition

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

Exposition writes metric families in the Prometheus text format.

func (*Exposition) Counter

func (e *Exposition) Counter(name, help string, value float64)

Counter writes a counter without labels.

func (*Exposition) Gauge

func (e *Exposition) Gauge(name, help string, value float64)

Gauge writes a gauge without labels.

type Metrics

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

Metrics counts the site's requests and renders and writes them in the Prometheus text format. It keeps a few counters and histograms per route class and render kind, so it stays small whatever the site's URLs are.

func NewMetrics

func NewMetrics() *Metrics

NewMetrics returns empty metrics.

func (*Metrics) ObserveRequest

func (m *Metrics) ObserveRequest(class, method string, code int, duration time.Duration)

ObserveRequest counts one answered request.

func (*Metrics) Record

func (m *Metrics) Record(record metrics.Record)

Record counts one render of go-partial; Metrics is a sink of its exp/metrics stage.

func (*Metrics) WriteTo

func (m *Metrics) WriteTo(w io.Writer, extra ...func(*Exposition)) error

WriteTo writes the metrics in the Prometheus text format, followed by what extra writes, for example the database's connection pool.

Jump to

Keyboard shortcuts

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