Configure fiberprometheus middleware
masterThe fiberprometheus middleware provides default Prometheus metrics for your Fiber application.
Default Metrics
http_requests_totalhttp_request_duration_secondshttp_requests_in_progress_total
Initialization Options
You can initialize the middleware using several methods to define service identity and labels:
- Basic Service Name:
fiberprometheus.New("service-name")sets the service name as a label. - With Hierarchy:
fiberprometheus.NewWith(servicename, namespace, subsystem)allows for structured labeling. - With Custom Labels:
fiberprometheus.NewWithLabels(labels, namespace, subsystem)wherelabelsis amap[string]string.
Middleware Configuration
Once initialized, you can customize the behavior before applying it to the app:
RegisterAt(app, path): Registers the metrics endpoint (e.g.,/metrics) on your Fiber application.SetSkipPaths([]string{...}): Excludes specific paths from being recorded in metrics.SetIgnoreStatusCodes([]int{...}): Excludes specific HTTP status codes from being recorded in metrics.
After configuration, apply the middleware using app.Use(prometheus.Middleware).
package main
import (
"github.com/ansrivas/fiberprometheus/v2"
"github.com/gofiber/fiber/v2"
)
func main() {
app := fiber.New()
// Initialize with a service name
prometheus := fiberprometheus.New("my-service-name")
// Register the /metrics endpoint
prometheus.RegisterAt(app, "/metrics")
// Optional: Exclude specific paths
prometheus.SetSkipPaths([]string{"/ping"})
// Optional: Exclude specific status codes
prometheus.SetIgnoreStatusCodes([]int{401, 403, 404})
// Apply the middleware
app.Use(prometheus.Middleware)
app.Get("/", func(c *fiber.Ctx) error {
return c.SendString("Hello World")
})
app.Listen(":3000")
}