Skip to content

AlleyPin/ginprom

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ginprom

Gin Prometheus metrics exporter inspired by github.com/zsais/go-gin-prometheus

Go Version Go Version Go Report Card Build Status codecov License

Install

Simply run : go get -u github.com/Depado/ginprom

Differences with go-gin-prometheus

  • No support for Prometheus' Push Gateway
  • Options on constructor
  • Adds a path label to get the matched route
  • Ability to ignore routes

Usage

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")
}

Options

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.

Troubleshooting

The instrumentation doesn't seem to work

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.

Packages

No packages published

Languages

  • Go 100.0%