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 TimingMiddleware ¶
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.
Types ¶
type TimingCounter ¶ added in v1.21.53
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 ¶
TimingsFromContext returns the accumulator installed by WithTimings, if any.
func WithTimings ¶
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
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
Duration returns the accumulated wall time of a named metric, and whether the metric was recorded at all.
func (*Timings) Header ¶
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.