httpstats

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2026 License: MIT Imports: 12 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) 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) Snapshot

func (c *Collector) Snapshot() Snapshot

Snapshot returns the currently active generation without clearing it.

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.

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