Prometheus middleware for Fiber v3 based on ansrivas/fiberprometheus.
Following metrics are available by default:
http_requests_total
http_requests_status_class_total
http_request_duration_seconds
http_requests_in_progress
http_request_size_bytes
http_response_size_bytes
http_requests_in_progress exposes both the HTTP method and normalized
route path so you can pinpoint which handlers are currently running.
Note
The middleware requires Go 1.25 or newer and Fiber v3 (currently RC).
go get github.com/gofiber/contrib/v3/prometheuspackage main
import (
fiberprometheus "github.com/gofiber/contrib/v3/prometheus"
"github.com/gofiber/fiber/v3"
)
func main() {
app := fiber.New()
app.Use("/metrics", fiberprometheus.New(fiberprometheus.Config{
Service: "my-service-name",
SkipURIs: []string{"/ping"},
IgnoreStatusCodes: []int{401, 403, 404},
}))
app.Get("/", func(c fiber.Ctx) error {
return c.SendString("Hello World")
})
app.Get("/ping", func(c fiber.Ctx) error {
return c.SendString("pong")
})
app.Post("/some", func(c fiber.Ctx) error {
return c.SendString("Welcome!")
})
app.Listen(":3000")
}The middleware exposes Prometheus collector toggles and HandlerOpts via
Config. By default it creates a private Registerer/Gatherer pair and uses
that for both registration and scraping. When customizing the registry, ensure
that the Registerer and Gatherer refer to the same metrics source (for
example, a *prometheus.Registry). Supplying only one that does not implement
the other interface or providing a mismatched pair will cause initialization to
panic so metrics are not silently dropped.
-
DisableGoCollectordisables the default Go runtime metrics collector when set totrue. -
DisableProcessCollectordisables the default process metrics collector when set totrue. -
EnableOpenMetricsnegotiates the experimental OpenMetrics encoding so exemplars are exported. -
EnableOpenMetricsTextCreatedSamplesadds synthetic_createdsamples when OpenMetrics is enabled. -
DisableCompressiondisables gzip/zstd compression even when clients request it. -
TrackUnmatchedRequestsrecords metrics for requests that miss all registered routes usingUnmatchedRouteLabelas the path label. Defaults tofalse. -
UnmatchedRouteLabelcustomizes the path label applied to unmatched requests when tracking is enabled. Defaults to/__unmatched__. -
RequestDurationBuckets,RequestSizeBuckets, andResponseSizeBucketscustomize the histogram buckets used for latency and payload metrics. They default to:- Duration:
[0.005 0.01 0.025 0.05 0.075 0.1 0.25 0.5 0.75 1 2.5 5 10 15 30 60] - Request size:
[256 512 1024 2048 4096 8192 16384 32768 65536 131072 262144 524288 1048576 2097152 5242880] - Response size:
[256 512 1024 2048 4096 8192 16384 32768 65536 131072 262144 524288 1048576 2097152 5242880]
- Duration:
All of the options default to false and can be enabled or disabled individually as needed.
The metrics endpoint path is derived from how the middleware is mounted. In the
example above, calling app.Use("/metrics", fiberprometheus.New(...)) exposes
the handler at /metrics while the middleware continues to instrument all
routed traffic.
- Hit the default url at http://localhost:3000
- Navigate to http://localhost:3000/metrics
- Metrics are recorded only for routes registered with Fiber unless
TrackUnmatchedRequestsis enabled, in which case unmatched requests are labeled withUnmatchedRouteLabel.