Prometheus Monitoring in Fiber v3
This recipe demonstrates production-ready Prometheus metrics exposition in Fiber v3 using the official github.com/gofiber/contrib/v3/prometheus middleware:
- HTTP Metrics Instrumentation: Automatically tracks request duration histograms, request counts by method, path, and status code, and in-flight requests.
- Dedicated
/metricsEndpoint: Serves metrics in the standard Prometheus text exposition format. - Custom Application / Domain Metrics: Registers a custom business metric (
app_orders_processed_totalCounter) with a shared Prometheus registry, next to the Go runtime and process metrics the middleware registers. - Endpoint Exemption (
SkipURIs): Excludes internal health probes (/health) from skewing request latency histograms and count metrics. - Graceful Shutdown: On
SIGINTorSIGTERM,ListenConfig.GracefulContextdrains active requests before the process exits (bounded by Fiber's default 10sShutdownTimeout).
Project Structure
.
├── go.mod
├── go.sum
├── main.go
├── main_test.go
└── README.md
Running the Recipe
Navigate to the recipe directory and run:
cd prometheus
go run main.go
The server starts on port :3000.
1. Process an Order (Increments Business Metric)
curl -i -X POST http://localhost:3000/orders
Returns:
{"message":"Order processed successfully","status":"created"}
2. Inspect Prometheus Metrics
curl -s http://localhost:3000/metrics
Output includes both Fiber HTTP server metrics and custom application metrics:
# HELP app_orders_processed_total Total number of orders successfully processed by the application.
# TYPE app_orders_processed_total counter
app_orders_processed_total 1
# HELP http_server_requests_total Count all http requests by status code, method and path.
# TYPE http_server_requests_total counter
http_server_requests_total{method="POST",path="/orders",service="order-service",status_code="201"} 1
# HELP http_server_request_duration_seconds Duration of all HTTP requests by status code, method and path.
# TYPE http_server_request_duration_seconds histogram
http_server_request_duration_seconds_bucket{method="POST",path="/orders",service="order-service",status_code="201",le="0.005"} 1
...
3. Check Health Probe Exemption
Health checks at /health return 200 OK, but do not pollute the Prometheus latency histograms:
curl -i http://localhost:3000/health
Running Tests
Run the unit test suite:
go test -v ./...