telemetry

package
v0.41.418 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2026 License: MIT Imports: 8 Imported by: 1 module, 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 telemetry provides http.Handler middleware for request logging and request metrics, plus the ResponseCapture writer they rely on to observe the status code and body size of a response.

h := telemetry.NewRequestLogger(next, time.Millisecond, logger,
	telemetry.WithLoggerSkipPaths([]telemetry.LoggerSkipPath{{Path: "/healthz", Agent: "*"}}))
h = telemetry.NewRequestMetrics(h)

Metrics are emitted through porto/metricskey (HTTPReqPerf, HTTPReqByRole) keyed by method, status and URL path. LoggerSkipPath entries are also reused by restserver/authz to suppress access logs; see ShouldSkip.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewRequestLogger

func NewRequestLogger(
	handler http.Handler,
	granularity time.Duration,
	logger xlog.KeyValueLogger,
	opts ...Option) http.Handler

NewRequestLogger creates a RequestLogger that chains to handler. The logged duration is expressed in units of granularity (e.g. time.Millisecond), which must be greater than zero. It panics if handler is nil and returns handler unchanged (no logging) if logger is nil. The remote address logged is identity.ClientIPFromRequest, which honours X-Forwarded-For.

func NewRequestMetrics

func NewRequestMetrics(h http.Handler) http.Handler

NewRequestMetrics wraps h so that every request records metricskey.HTTPReqPerf (latency) and metricskey.HTTPReqByRole (count) labelled by method, status code, URL path and caller role (from identity.FromRequest). Responses with status 404 are counted only under a fixed "unknown" path to limit cardinality from scanners.

func ShouldSkip added in v0.24.0

func ShouldSkip(cfg []LoggerSkipPath, path, userAgent string) bool

ShouldSkip reports whether a request for path with the given User-Agent matches any entry in cfg and should therefore not be logged.

Types

type LoggerSkipPath

type LoggerSkipPath struct {
	Path  string `json:"path,omitempty" yaml:"path,omitempty"`
	Agent string `json:"agent,omitempty" yaml:"agent,omitempty"`
}

LoggerSkipPath describes requests to exclude from logging by Path and User-Agent. Path is compared exactly ("*" matches every path); Agent is a substring match ("*" matches every agent). Both must match.

type Option

type Option option

Option configures NewRequestLogger; see WithLoggerSkipPaths.

func WithLoggerSkipPaths

func WithLoggerSkipPaths(value []LoggerSkipPath) Option

WithLoggerSkipPaths returns an Option that suppresses log lines for requests matching any of the given LoggerSkipPath entries.

type RequestLogger

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

RequestLogger is a http.Handler that forwards requests to the wrapped handler and then logs one INFO line per request (method, path, status, bytes, duration, remote IP, agent) using the request context for correlation fields.

func (*RequestLogger) ServeHTTP

func (l *RequestLogger) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements the http.Handler interface. We wrap the call to the real handler to collect info about the response, and then write out the log line

type ResponseCapture

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

ResponseCapture is a net/http.ResponseWriter that delegates everything to the contained delegate, but captures the status code and number of bytes written. The status defaults to 200 until WriteHeader is called. It implements http.Flusher but not http.Hijacker or Unwrap, so handlers that hijack connections (WebSocket) cannot run behind it.

func NewResponseCapture

func NewResponseCapture(w http.ResponseWriter) *ResponseCapture

NewResponseCapture returns a new ResponseCapture instance that delegates writes to the supplied ResponseWriter

func (*ResponseCapture) BodySize

func (r *ResponseCapture) BodySize() uint64

BodySize returns in bytes the total number of bytes written to the response body so far.

func (*ResponseCapture) Flush

func (r *ResponseCapture) Flush()

Flush sends any buffered data to the client when the delegate implements http.Flusher; otherwise it is a no-op.

func (*ResponseCapture) Header

func (r *ResponseCapture) Header() http.Header

Header returns the underlying writers Header instance

func (*ResponseCapture) StatusCode

func (r *ResponseCapture) StatusCode() int

StatusCode returns the http status set by the handler.

func (*ResponseCapture) Write

func (r *ResponseCapture) Write(data []byte) (int, error)

Write the supplied data to the response (tracking the number of bytes written as we go)

func (*ResponseCapture) WriteHeader

func (r *ResponseCapture) WriteHeader(sc int)

WriteHeader sets the HTTP status code of the response

Jump to

Keyboard shortcuts

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