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 ¶
- func Middleware(svc, ver string, opts ...Option) func(http.Handler) http.Handler
- func NewContext(ctx context.Context, t *Tracker) context.Context
- type Approx
- type Bytes
- type Option
- type Render
- type Stats
- type Tracker
- func (t *Tracker) AddBytesIn(n int64)
- func (t *Tracker) AddBytesOut(n int64)
- func (t *Tracker) EnableApprox() *Tracker
- func (t *Tracker) EnableHeapDelta() *Tracker
- func (t *Tracker) Mark(name string, d time.Duration)
- func (t *Tracker) Phase(name string) func()
- func (t *Tracker) SetBytesOut(n int64)
- func (t *Tracker) SetRender(r Render)
- func (t *Tracker) SetUpstream(headerVal string)
- func (t *Tracker) Stats() Stats
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Middleware ¶
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()).
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 ¶
From returns the request's Tracker, or a detached no-op tracker if absent, so callers can enrich unconditionally without nil checks.
func Start ¶
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 (*Tracker) AddBytesOut ¶
func (*Tracker) EnableApprox ¶
EnableApprox turns on the cheap process-CPU delta (getrusage). Default-on in the server middleware.
func (*Tracker) EnableHeapDelta ¶
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) Phase ¶
Phase times a named span; call the returned func to record it. Repeated names accumulate.
func (*Tracker) SetBytesOut ¶
func (*Tracker) SetUpstream ¶
SetUpstream nests a callee's X-Request-Stats header value under "upstream". Invalid/empty JSON is ignored.