HTTP Middleware
Automatically instrument all your HTTP handlers with comprehensive request metrics.
- http_requests_total
- http_request_duration_seconds
- http_in_flight_requests
- http_request_size_bytes
- http_response_size_bytes
A lightweight and configurable Prometheus instrumentation library for Go web applications. Add standardized HTTP, health, and business-level metrics with minimal code.
go get github.com/peek8/prometric-go/prometrics Features
Comprehensive metrics collection with plug-and-play middleware for net/http and Gin frameworks.
Automatically instrument all your HTTP handlers with comprehensive request metrics.
Monitor your application's health with real-time system metrics and resource usage.
Track business-level operations with utility functions for object lifecycle metrics.
Quick Start
Simple integration with your existing Go web framework of choice.
package main import ( "net/http" "time" "github.com/prometheus/client_golang/prometheus/promhttp" "github.com/peek8/prometric-go/prometrics" ) func createPerson(w http.ResponseWriter, r *http.Request) { // Track the person create operation defer prometrics.TrackCRUD("person", "Create")(time.Now()) prometrics.IncObjectCount("person") // Simulate creation delay time.Sleep(200 * time.Millisecond) w.Write([]byte("Person created")) } func main() { mux := http.NewServeMux() // Wrap your business handler mux.Handle("/person", prometrics.InstrumentHttpHandler("person_handler", http.HandlerFunc(createPerson))) // Use HealthMiddleware at /metrics endpoint mux.Handle("/metrics", prometrics.HealthMiddleware(promhttp.Handler())) http.ListenAndServe(":8080", mux) }
package main import ( "time" "github.com/gin-gonic/gin" "github.com/prometheus/client_golang/prometheus/promhttp" "github.com/peek8/prometric-go/prometrics" ) func main() { r := gin.Default() r.Use(prometrics.GinMiddleware()) r.Use(prometrics.GinHealthMiddleware()) r.GET("/ping", func(c *gin.Context) { c.JSON(200, gin.H{"msg": "pong"}) }) r.GET("/person", func(c *gin.Context) { defer prometrics.TrackCRUD("person", "Get")(time.Now()) c.JSON(200, gin.H{"name": "asraf"}) }) r.POST("/person", func(c *gin.Context) { defer prometrics.TrackCRUD("person", "create")(time.Now()) prometrics.IncObjectCount("person") time.Sleep(200 * time.Millisecond) c.JSON(201, gin.H{"status": "created"}) }) r.GET("/metrics", gin.WrapH(promhttp.Handler())) r.Run(":7080") }
// Collect health metrics at regular intervals // instead of only on endpoint requests ctx, cancel := context.WithCancel(context.Background(), 10) go prometrics.CollectSystemMetricsLoop(ctx) // Cancel when needed: // cancel()
Compatibility
100% compatible with the Prometheus ecosystem and your favorite tools.
Native client library integration
Beautiful dashboards ready
Standard library support
First-class middleware
Get started with prometric-go in under 5 minutes. Full documentation available on pkg.go.dev.