reqstats

package
v0.134.2 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2026 License: Apache-2.0 Imports: 10 Imported by: 1 module, 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 reqstats emits per-request resource stats on every HTTP response: a W3C Server-Timing header (human/devtools) and an X-Request-Stats JSON header (machine/cost-engine). It is wired as a default middleware by go-common/server, so every service gets the universal payload — total_ms + bytes + an approx process-delta block — for free; services enrich with named phases, a render block (chromedp), and upstream nesting via the request-context Tracker.

See docs/adr/0001-per-request-resource-stats.md. The approx block is a best-effort, concurrency-contaminated hint — never the cost basis.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Middleware

func Middleware(svc, ver string, opts ...Option) func(http.Handler) http.Handler

Middleware returns a go-common middleware that emits per-request Server-Timing + X-Request-Stats on every response. svc/ver label the stats. The universal payload (total + bytes + approx CPU) needs no per-handler code; handlers enrich via reqstats.From(r.Context()).

func NewContext

func NewContext(ctx context.Context, t *Tracker) context.Context

NewContext stores the tracker on the context (the middleware does this).

Types

type Approx

type Approx struct {
	ProcCPUms      int64  `json:"proc_cpu_ms"`
	HeapAllocDelta int64  `json:"heap_alloc_delta,omitempty"`
	Note           string `json:"note"`
}

Approx is the best-effort, debug-only process-delta block (never billable).

type Bytes

type Bytes struct {
	In       int64 `json:"in"`
	Out      int64 `json:"out"`
	Upstream int64 `json:"upstream,omitempty"`
}

Bytes counts request/response/upstream payload sizes.

type Option

type Option func(*mwConfig)

Option configures the middleware.

func WithHeapDelta

func WithHeapDelta() Option

WithHeapDelta also records a per-request heap-alloc delta (adds a runtime.ReadMemStats stop-the-world per request). Opt-in: use on services that want a RAM hint and can afford it; leave off for high-QPS services.

type Render

type Render struct {
	ScriptMs      float64 `json:"script_ms,omitempty"`
	TaskMs        float64 `json:"task_ms,omitempty"`
	LayoutMs      float64 `json:"layout_ms,omitempty"`
	RecalcStyleMs float64 `json:"recalc_style_ms,omitempty"`
	JSHeapUsed    int64   `json:"js_heap_used,omitempty"`
	JSHeapTotal   int64   `json:"js_heap_total,omitempty"`
	Nodes         int64   `json:"nodes,omitempty"`
	Documents     int64   `json:"documents,omitempty"`
	LayoutCount   int64   `json:"layout_count,omitempty"`
	Frames        int64   `json:"frames,omitempty"`
}

Render is the per-tab render-engine block. chromedp services fill it from CDP Performance.getMetrics; go-common never imports chromedp — callers pass plain numbers. Durations are milliseconds.

type Stats

type Stats struct {
	Svc      string           `json:"svc"`
	Ver      string           `json:"ver"`
	OK       bool             `json:"ok"`
	TotalMs  int64            `json:"total_ms"`
	Bytes    Bytes            `json:"bytes"`
	Approx   *Approx          `json:"approx,omitempty"`
	Phase    map[string]int64 `json:"phase,omitempty"`
	Render   *Render          `json:"render,omitempty"`
	Upstream json.RawMessage  `json:"upstream,omitempty"`
}

Stats is the canonical X-Request-Stats envelope.

type Tracker

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

Tracker accumulates per-request stats. Enrichment methods are safe to call from the handler goroutine; a Tracker is one-per-request, not shared.

func From

func From(ctx context.Context) *Tracker

From returns the request's Tracker, or a detached no-op tracker if absent, so callers can enrich unconditionally without nil checks.

func Start

func Start(svc, ver string) *Tracker

Start begins a tracker labeled with the service id + version.

The phases map is allocated lazily on the first Mark/Phase call — most requests record no phases, so this avoids a per-request map allocation on the universal server path.

func (*Tracker) AddBytesIn

func (t *Tracker) AddBytesIn(n int64)

func (*Tracker) AddBytesOut

func (t *Tracker) AddBytesOut(n int64)

func (*Tracker) EnableApprox

func (t *Tracker) EnableApprox() *Tracker

EnableApprox turns on the cheap process-CPU delta (getrusage). Default-on in the server middleware.

func (*Tracker) EnableHeapDelta

func (t *Tracker) EnableHeapDelta() *Tracker

EnableHeapDelta also records the per-request heap-alloc delta. This calls runtime.ReadMemStats (a small stop-the-world) per request — opt-in for services that want a RAM hint and can afford it; high-QPS services should leave it off.

func (*Tracker) Mark

func (t *Tracker) Mark(name string, d time.Duration)

Mark records a named duration directly.

func (*Tracker) Phase

func (t *Tracker) Phase(name string) func()

Phase times a named span; call the returned func to record it. Repeated names accumulate.

func (*Tracker) SetBytesOut

func (t *Tracker) SetBytesOut(n int64)

func (*Tracker) SetRender

func (t *Tracker) SetRender(r Render)

SetRender attaches the render-engine block (chromedp services).

func (*Tracker) SetUpstream

func (t *Tracker) SetUpstream(headerVal string)

SetUpstream nests a callee's X-Request-Stats header value under "upstream". Invalid/empty JSON is ignored.

func (*Tracker) Stats

func (t *Tracker) Stats() Stats

Stats returns the finalized snapshot (finalizing with ok=true if needed).

Jump to

Keyboard shortcuts

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