Gin Prometheus metrics exporter inspired by github.com/zsais/go-gin-prometheus
Simply run :
go get -u github.com/Depado/ginprom
- No support for Prometheus' Push Gateway
- Options on constructor
- Adds a
path
label to get the matched route - Ability to ignore routes
package main
import (
"github.com/Depado/ginprom"
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
p := ginprom.New(
ginprom.Engine(r),
ginprom.Subsystem("gin"),
ginprom.Path("/metrics"),
)
r.Use(p.Instrument())
r.GET("/hello/:id", func(c *gin.Context) {})
r.GET("/world/:id", func(c *gin.Context) {})
r.Run("127.0.0.1:8080")
}
Path(path string)
Specify the path on which the metrics are accessed
Default : "/metrics"
Namespace(ns string)
Specify the namespace
Default : "gin"
Subsystem(sub string)
Specify the subsystem
Default : "go"
Engine(e *gin.Engine)
Specify the Gin engine directly when initializing.
Saves a call to Use(e *gin.Engine)
Default : nil
Ignore(paths ...string)
Specify which paths should not be taken into account by the middleware.
Make sure you have set the gin.Engine
in the ginprom
middleware, either when
initializing it using ginprom.New(ginprom.Engine(r))
or using the Use
function after the initialization like this :
p := ginprom.New(
ginprom.Namespace("gin"),
ginprom.Subsystem("gonic"),
ginprom.Path("/metrics"),
)
p.Use(r)
r.Use(p.Instrument())
By design, if the middleware was to panic, it would do so when a route is called. That's why it just silently fails when no engine has been set.