rpchttp

package
v1.21.102 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2026 License: Apache-2.0 Imports: 9 Imported by: 7 modules, 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 rpchttp provides request-scoped HTTP server timing that both the RPC executor path and hand-written handlers can feed, emitted once as a Server-Timing response header by TimingMiddleware.

The package deliberately imports only the standard library (not its parent rpc package) so business logic can call AddTiming without pulling in rpc's cobra/openapi dependencies and without creating an import cycle.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddTiming

func AddTiming(ctx context.Context, metric TimingMetric)

AddTiming aggregates a metric into the current request. It is a no-op when no accumulator is present, such as on the CLI path.

func MaxRSSBytes added in v1.21.83

func MaxRSSBytes(maxrss int64) int64

MaxRSSBytes converts getrusage's ru_maxrss to bytes for a max_rss_bytes counter: Linux reports KiB, Darwin and the BSDs bytes.

func ObserveTimings added in v1.21.102

func ObserveTimings(observer TimingsObserver) (release func())

ObserveTimings registers a process-wide observer. Delivery is synchronous on the timed goroutine, so observer methods must not block.

func ReportTimings added in v1.21.102

func ReportTimings(ctx context.Context, timings *Timings)

ReportTimings hands the accumulated metrics to every observer. It is a no-op when timings is nil or nothing observes.

func TimingMiddleware

func TimingMiddleware(next http.Handler) http.Handler

TimingMiddleware installs a request-scoped Timings accumulator into the request context and stamps a Server-Timing response header. The header is written on the first WriteHeader/Write, or — for handlers that return without writing anything — once the handler returns. It carries a `total` metric plus any phase metrics business logic contributed via AddTiming/Track.

func Track

func Track(ctx context.Context, name string) func()

Track starts a timer for the named phase and returns a stop function that records the elapsed time when called. Use as `defer Track(ctx, "parse")()` or keep the returned func to stop explicitly inside a loop. Observers registered with ObserveTimings see the phase start and stop even without an accumulator.

Types

type Aggregate added in v1.21.83

type Aggregate int

Aggregate combines the contributions of one counter.

const (
	// AggregateSum adds every contribution (counts, CPU time, bytes).
	AggregateSum Aggregate = iota
	// AggregateMax keeps the largest contribution (peak memory).
	AggregateMax
)

type TimingCounter added in v1.21.53

type TimingCounter struct {
	Name  string
	Value int64
	// Aggregate is how contributions combine; the zero value sums them.
	Aggregate Aggregate
}

TimingCounter is a named integer aggregated into a metric's description.

type TimingMetric added in v1.21.53

type TimingMetric struct {
	Name     string
	Duration time.Duration
	Counters []TimingCounter
}

TimingMetric is one contribution to a request-scoped server timing metric.

type Timings

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

Timings is a request-scoped, insertion-ordered accumulator of named metrics.

func TimingsFromContext

func TimingsFromContext(ctx context.Context) (*Timings, bool)

TimingsFromContext returns the accumulator installed by WithTimings, if any.

func WithTimings

func WithTimings(ctx context.Context) (context.Context, *Timings)

WithTimings returns a context carrying a fresh accumulator and the accumulator itself, so a middleware can read it back after the handler returns.

func (*Timings) Counter added in v1.21.54

func (t *Timings) Counter(metric, counter string) (int64, bool)

Counter returns the accumulated value of a named counter on a named metric, and whether that counter was recorded at all.

func (*Timings) Duration added in v1.21.54

func (t *Timings) Duration(metric string) (time.Duration, bool)

Duration returns the accumulated wall time of a named metric, and whether the metric was recorded at all.

func (*Timings) Header

func (t *Timings) Header() string

Header renders the accumulated phases as a Server-Timing value fragment (`find;dur=4.1, parse;dur=6.8`), durations in milliseconds to one decimal. It returns an empty string when no phases were recorded.

func (*Timings) Metrics added in v1.21.54

func (t *Timings) Metrics() []TimingMetric

Metrics returns a snapshot of the accumulated metrics in insertion order, so callers outside the HTTP path (benchmarks, CLI profiling) can read durations and counters without parsing the header string back apart.

type TimingsObserver added in v1.21.102

type TimingsObserver interface {
	// StartPhase is called when Track starts a phase; the returned func is called when the phase stops.
	StartPhase(ctx context.Context, name string) (end func())
	// Report receives the accumulated metrics once a request (TimingMiddleware) or CLI command (StopTimings) finishes.
	Report(ctx context.Context, metrics []TimingMetric)
}

TimingsObserver sees request/command timing phases as they happen.

Directories

Path Synopsis
Package otelspans bridges rpchttp server timings to OpenTelemetry: each Track phase becomes a span and reported metrics become attributes on the enclosing span.
Package otelspans bridges rpchttp server timings to OpenTelemetry: each Track phase becomes a span and reported metrics become attributes on the enclosing span.

Jump to

Keyboard shortcuts

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