httpstats

package
v1.1.0 Latest Latest
Warning

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

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

Documentation

Overview

Package httpstats provides bounded, in-memory HTTP request measurements. It deliberately records URL paths, never query strings.

Index

Constants

View Source
const (
	// DefaultMaxKeys is the maximum number of distinct HTTP identities held by
	// a Collector. New identities past the limit are merged into OverflowPath.
	DefaultMaxKeys = 10000
	// OverflowPath identifies observations merged after the key limit.
	OverflowPath = "(other)"
)

Variables

View Source
var Default = New()

Default is used by the package-level Middleware helper.

Functions

func Middleware

func Middleware(next http.Handler) http.Handler

Middleware instruments next using Default.

Types

type Collector

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

Collector owns HTTP measurements and generation boundaries. Reset swaps in a fresh table and waits for requests already in flight, then returns their completed generation.

func New

func New(opts ...Option) *Collector

New returns an independent Collector.

func (*Collector) Connections added in v0.7.0

func (c *Collector) Connections() ConnSnapshot

Connections reports long-lived connection stats for the current generation.

func (*Collector) Middleware

func (c *Collector) Middleware(next http.Handler) http.Handler

Middleware returns a handler that measures method, normalized path, protocol, status, duration, and response body bytes.

func (*Collector) Reset

func (c *Collector) Reset() Snapshot

Reset atomically starts a new generation, waits for requests that started in the old one to finish, and returns the completed old generation.

func (*Collector) SetRules added in v0.7.0

func (c *Collector) SetRules(rules []Rule)

SetRules replaces the collector's path rules at runtime (e.g. from the ISUTOOLS_PATH_RULES environment variable).

func (*Collector) Snapshot

func (c *Collector) Snapshot() Snapshot

Snapshot returns the currently active generation without clearing it.

type ConnSnapshot added in v0.7.0

type ConnSnapshot struct {
	Total        int64   `json:"total"`
	Active       int64   `json:"active"`
	AvgSeconds   float64 `json:"avg_seconds"`
	P95Seconds   float64 `json:"p95_seconds"`
	MaxSeconds   float64 `json:"max_seconds"`
	BytesRead    int64   `json:"bytes_read"`
	BytesWritten int64   `json:"bytes_written"`
}

ConnSnapshot summarizes long-lived connections (WebSocket upgrades and text/event-stream responses) which are excluded from the latency table so they cannot distort p95/avg. Active spans generations; the rest reset with the generation.

type Entry

type Entry struct {
	Key        string        `json:"key"`
	Method     string        `json:"method"`
	Path       string        `json:"path"`
	Protocol   string        `json:"protocol"`
	Status     int           `json:"status"`
	Count      int64         `json:"count"`
	Total      time.Duration `json:"total_ns"`
	Avg        time.Duration `json:"avg_ns"`
	Max        time.Duration `json:"max_ns"`
	P95        time.Duration `json:"p95_ns"`
	TotalBytes int64         `json:"total_bytes"`
	AvgBytes   int64         `json:"avg_bytes"`
}

Entry is one aggregated HTTP identity. Duration fields are encoded as integer nanoseconds in JSON. P95 is the upper bound of a log2 bucket.

type Option

type Option func(*config)

Option configures a Collector.

func WithMaxKeys

func WithMaxKeys(max int) Option

WithMaxKeys changes the distinct-key limit. A value of zero merges every observation into the overflow entry. Negative values are ignored.

func WithPathRules

func WithPathRules(rules []Rule) Option

WithPathRules replaces the built-in path normalization with rules applied in order. Rules with a nil Pattern are ignored.

type Rule

type Rule struct {
	Pattern     *regexp.Regexp
	Replacement string
}

Rule replaces matching text in a request path. Supplying path rules disables the built-in numeric and UUID segment normalization.

func ParseRules added in v0.7.0

func ParseRules(spec string) ([]Rule, error)

ParseRules parses an ISUTOOLS_PATH_RULES spec: semicolon-separated "regex=replacement" pairs, split on the LAST '=' so regexes may contain '='. Example: "^/@[^/]+$=/@*;^/posts/[0-9]+$=/posts/*".

type Snapshot

type Snapshot []Entry

Snapshot is a point-in-time copy sorted by total duration descending.

Jump to

Keyboard shortcuts

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