middleware

package
v0.0.0-...-392f6ab Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2026 License: GPL-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package middleware provides Echo v5 middleware components.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Prometheus

func Prometheus() echo.MiddlewareFunc

Prometheus returns an Echo v5 middleware that records Prometheus metrics for every HTTP request.

Metrics recorded:

  • http_requests_total: counter by method, path, status
  • http_request_duration_seconds: histogram by method, path, status
  • http_requests_in_flight: gauge of concurrent requests

Registration order

Register Prometheus OUTERMOST, i.e. before Recover, BasicAuth and anything else that can shorten the chain:

e.Use(middleware.Prometheus()) // must be first
e.Use(middleware.RequestLogger())
e.Use(middleware.Recover())
e.Use(middleware.BasicAuth(func(...) (bool, error) { ... }))

Two reasons, both about observing the request the client actually got:

  • A middleware registered OUTSIDE Prometheus that short-circuits without calling next (BasicAuth rejecting a credential, an IP allow-list, a rate limiter, CSRF) never reaches the recording code at all, so that traffic is invisible in the metrics instead of merely mislabelled. Rejected authentication is the most security-relevant traffic there is.
  • A request's status is only final once the response has been committed. For a handler that RETURNS an error (and for any 404/405), that happens in Echo.HTTPErrorHandler, which ServeHTTP runs *after* the whole middleware chain has returned (echo.go: serveHTTP -> e.HTTPErrorHandler). A recorder that reads the status on the way out of next() therefore sees no status at all and must not guess one.

Status reporting

The recorded status is the status the client received:

  • Normal completion: recorded from the context's *echo.Response, which is the writer that talks to the transport, so it holds the code that was actually written - including codes written through another middleware's wrapper (Gzip replaces the response with its own writer once the client sends Accept-Encoding: gzip, and that writer still commits through *echo.Response).
  • Handler-returned errors and panics: the response does not exist yet when the chain returns, so recording is deferred to Response.Before, which Echo fires exactly once from inside Response.WriteHeader with Status already set to the code being sent. Echo's error handling can never render a status that goes unrecorded, because it always commits through that same Response.
  • A panic that escapes every inner Recover (Echo's Recover re-panics http.ErrAbortHandler, and Recover need not be wired at all) is recorded from a deferred recover, then re-panicked, so the request is never lost even though ServeHTTP never reaches HTTPErrorHandler for it.

The one case that is not recorded is an application whose custom HTTPErrorHandler writes no response at all: there is no commit to observe and nothing on the wire to describe.

Types

This section is empty.

Jump to

Keyboard shortcuts

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