Help us shape the future of Go! Take the 2026 Go Developer Survey

ginprom

package module
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 15, 2019 License: MIT Imports: 9 Imported by: 0

README

ginprom

GoDoc Build Status Go Report Card

Ginprom collects metrics in the Gin web framework and exports it to the Prometheus.

By default ginprom collects following metrics:

  • ginprom_request_duration_seconds - histogram of request latencies
  • ginprom_requests_in_flight - current number of requests in flight
  • ginprom_requests_total - total number of processed requests by status code

Ginprom also allows to register and export custom user metrics.

Usage

Set the middleware to collect common metrics:

package main

import (
	"github.com/gin-gonic/gin"
	"github.com/striker2000/ginprom"
)

func main() {
	r := gin.Default()
	p := ginprom.New()

	// Attach the middleware to the router
	r.Use(p.Middleware)

	// Set the handler to export collected metrics
	r.GET("/metrics", p.Handler)

	r.Run()
}

Register your custom metrics:

import "github.com/prometheus/client_golang/prometheus"

pings := prometheus.NewCounter(prometheus.CounterOpts{
	Name: "pings_received",
})

// Registered metric will be exported automatically via p.Handler
p.MustRegister(pings)

r.GET("/ping", func(c *gin.Context) {
	// Use the metric in your code
	pings.Inc()

	c.JSON(200, gin.H{"message": "pong"})
})

Documentation

Overview

Package ginprom collects metrics in the Gin web framework and exports it to the Prometheus.

Usage:

r := gin.Default()
p := ginprom.New()

// Attach the middleware to collect common metrics
r.Use(p.Middleware)

// Set the handler to export collected metrics
r.GET("/metrics", p.Handler)

// Register a custom metric
pings := prometheus.NewCounter(prometheus.CounterOpts{
    Name: "pings_received",
})

p.MustRegister(pings)

r.GET("/ping", func(c *gin.Context) {
    pings.Inc()
    c.JSON(200, gin.H{"message": "pong"})
})

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Prometheus

type Prometheus struct {
	// contains filtered or unexported fields
}

Prometheus provides methods to collect common and custom metrics.

func New

func New() *Prometheus

New creates a new Prometheus instance.

func (*Prometheus) Handler

func (p *Prometheus) Handler(c *gin.Context)

Handler exports collected metrics to the caller.

func (*Prometheus) Middleware

func (p *Prometheus) Middleware(c *gin.Context)

Middleware used to collect common HTTP metrics. Should be attached to the Gin router through Use().

func (*Prometheus) MustRegister

func (p *Prometheus) MustRegister(c prometheus.Collector)

MustRegister registers the custom collector and panics if any error occurs.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL