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 ¶
- func NewRequestLogger(handler http.Handler, granularity time.Duration, logger xlog.KeyValueLogger, ...) http.Handler
- func NewRequestMetrics(h http.Handler) http.Handler
- func ShouldSkip(cfg []LoggerSkipPath, path, userAgent string) bool
- type LoggerSkipPath
- type Option
- type RequestLogger
- type ResponseCapture
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 ¶
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