Prometheus Middleware

Prometheus middleware generates metrics for HTTP requests.

Usage

  1. package main
  2. import (
  3. "github.com/labstack/echo/v4"
  4. "github.com/labstack/echo-contrib/prometheus"
  5. )
  6. func main() {
  7. e := echo.New()
  8. // Enable metrics middleware
  9. p := prometheus.NewPrometheus("echo", nil)
  10. p.Use(e)
  11. e.Logger.Fatal(e.Start(":1323"))
  12. }

Sample Output

  1. curl http://localhost:1323/metrics
  2. # HELP echo_request_duration_seconds The HTTP request latencies in seconds.
  3. # TYPE echo_request_duration_seconds summary
  4. echo_request_duration_seconds_sum 0.41086482
  5. echo_request_duration_seconds_count 1
  6. # HELP echo_request_size_bytes The HTTP request sizes in bytes.
  7. # TYPE echo_request_size_bytes summary
  8. echo_request_size_bytes_sum 56
  9. echo_request_size_bytes_count 1
  10. # HELP echo_requests_total How many HTTP requests processed, partitioned by status code and HTTP method.
  11. # TYPE echo_requests_total counter
  12. echo_requests_total{code="200",host="localhost:8080",method="GET",url="/"} 1
  13. # HELP echo_response_size_bytes The HTTP response sizes in bytes.
  14. # TYPE echo_response_size_bytes summary
  15. echo_response_size_bytes_sum 61
  16. echo_response_size_bytes_count 1
  17. ...

Custom Configuration

Usage

A middleware skipper can be passed to avoid generating metrics to certain URLs:

  1. package main
  2. import (
  3. "github.com/labstack/echo/v4"
  4. "github.com/labstack/echo-contrib/prometheus"
  5. )
  6. // urlSkipper ignores metrics route on some middleware
  7. func urlSkipper(c echo.Context) bool {
  8. if strings.HasPrefix(c.Path(), "/testurl") {
  9. return true
  10. }
  11. return false
  12. }
  13. func main() {
  14. e := echo.New()
  15. // Enable metrics middleware
  16. p := prometheus.NewPrometheus("echo", urlSkipper)
  17. p.Use(e)
  18. e.Logger.Fatal(e.Start(":1323"))
  19. }