Documentation ¶
Overview ¶
Package ginprom is a library to instrument a gin server and expose a /metrics endpoint for Prometheus to scrape, keeping a low cardinality by preserving the path parameters name in the prometheus label
Index ¶
- Variables
- func BucketSize(b []float64) func(*Prometheus)
- func Engine(e *gin.Engine) func(*Prometheus)
- func Ignore(paths ...string) func(*Prometheus)
- func Namespace(ns string) func(*Prometheus)
- func Path(path string) func(*Prometheus)
- func Registry(r *prometheus.Registry) func(*Prometheus)
- func RequestCounterMetricName(reqCntMetricName string) func(*Prometheus)
- func RequestDurationMetricName(reqDurMetricName string) func(*Prometheus)
- func RequestSizeMetricName(reqSzMetricName string) func(*Prometheus)
- func ResponseSizeMetricName(resDurMetricName string) func(*Prometheus)
- func Subsystem(sub string) func(*Prometheus)
- func Token(token string) func(*Prometheus)
- type Prometheus
- func (p *Prometheus) AddCustomGauge(name, help string, labels []string)
- func (p *Prometheus) AddGaugeValue(name string, labelValues []string, value float64) error
- func (p *Prometheus) DecrementGaugeValue(name string, labelValues []string) error
- func (p *Prometheus) IncrementGaugeValue(name string, labelValues []string) error
- func (p *Prometheus) Instrument() gin.HandlerFunc
- func (p *Prometheus) SetGaugeValue(name string, labelValues []string, value float64) error
- func (p *Prometheus) SubGaugeValue(name string, labelValues []string, value float64) error
- func (p *Prometheus) Use(e *gin.Engine)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrCustomGauge = errors.New("error finding custom gauge")
ErrCustomGauge is returned when the custom gauge can't be found.
var ErrInvalidToken = errors.New("invalid or missing token")
ErrInvalidToken is returned when the provided token is invalid or missing.
Functions ¶
func BucketSize ¶ added in v1.4.0
func BucketSize(b []float64) func(*Prometheus)
BucketSize is used to define the default bucket size when initializing with New.
func Engine ¶
func Engine(e *gin.Engine) func(*Prometheus)
Engine is an option allowing to set the gin engine when intializing with New. Example: r := gin.Default() p := ginprom.New(Engine(r))
func Ignore ¶
func Ignore(paths ...string) func(*Prometheus)
Ignore is used to disable instrumentation on some routes.
func Namespace ¶ added in v1.1.0
func Namespace(ns string) func(*Prometheus)
Namespace is an option allowing to set the namespace when intitializing with New.
func Path ¶
func Path(path string) func(*Prometheus)
Path is an option allowing to set the metrics path when intializing with New.
Example ¶
Set the path (endpoint) where the metrics will be served
r := gin.New() p := New(Engine(r), Path("/metrics")) r.Use(p.Instrument())
Output:
func Registry ¶ added in v1.5.0
func Registry(r *prometheus.Registry) func(*Prometheus)
Registry is an option allowing to set a *prometheus.Registry with New. Use this option if you want to use a custom Registry instead of a global one that prometheus client uses by default Example: r := gin.Default() p := ginprom.New(Registry(r))
func RequestCounterMetricName ¶ added in v1.7.0
func RequestCounterMetricName(reqCntMetricName string) func(*Prometheus)
RequestCounterMetricName is an option allowing to set the request counter metric name.
func RequestDurationMetricName ¶ added in v1.7.0
func RequestDurationMetricName(reqDurMetricName string) func(*Prometheus)
RequestDurationMetricName is an option allowing to set the request duration metric name.
func RequestSizeMetricName ¶ added in v1.7.0
func RequestSizeMetricName(reqSzMetricName string) func(*Prometheus)
RequestSizeMetricName is an option allowing to set the request size metric name.
func ResponseSizeMetricName ¶ added in v1.7.0
func ResponseSizeMetricName(resDurMetricName string) func(*Prometheus)
ResponseSizeMetricName is an option allowing to set the response size metric name.
func Subsystem ¶
func Subsystem(sub string) func(*Prometheus)
Subsystem is an option allowing to set the subsystem when intitializing with New.
func Token ¶ added in v1.2.0
func Token(token string) func(*Prometheus)
Token is an option allowing to set the bearer token in prometheus with New. Example: ginprom.New(ginprom.Token("your_custom_token"))
Example ¶
Set a secret token that is required to access the endpoint
r := gin.New() p := New(Engine(r), Token("supersecrettoken")) r.Use(p.Instrument())
Output:
Types ¶
type Prometheus ¶
type Prometheus struct { MetricsPath string Namespace string Subsystem string Token string Ignored pmapb Engine *gin.Engine BucketsSize []float64 Registry *prometheus.Registry RequestCounterMetricName string RequestDurationMetricName string RequestSizeMetricName string ResponseSizeMetricName string // contains filtered or unexported fields }
Prometheus contains the metrics gathered by the instance and its path.
func New ¶
func New(options ...func(*Prometheus)) *Prometheus
New will initialize a new Prometheus instance with the given options. If no options are passed, sane defaults are used. If a router is passed using the Engine() option, this instance will automatically bind to it.
func (*Prometheus) AddCustomGauge ¶ added in v1.3.0
func (p *Prometheus) AddCustomGauge(name, help string, labels []string)
AddCustomGauge adds a custom gauge and registers it.
func (*Prometheus) AddGaugeValue ¶ added in v1.7.2
func (p *Prometheus) AddGaugeValue(name string, labelValues []string, value float64) error
AddGaugeValue adds gauge to value.
func (*Prometheus) DecrementGaugeValue ¶ added in v1.3.0
func (p *Prometheus) DecrementGaugeValue(name string, labelValues []string) error
DecrementGaugeValue decrements a custom gauge.
func (*Prometheus) IncrementGaugeValue ¶ added in v1.3.0
func (p *Prometheus) IncrementGaugeValue(name string, labelValues []string) error
IncrementGaugeValue increments a custom gauge.
func (*Prometheus) Instrument ¶
func (p *Prometheus) Instrument() gin.HandlerFunc
Instrument is a gin middleware that can be used to generate metrics for a single handler
func (*Prometheus) SetGaugeValue ¶ added in v1.3.0
func (p *Prometheus) SetGaugeValue(name string, labelValues []string, value float64) error
SetGaugeValue sets gauge to value.
func (*Prometheus) SubGaugeValue ¶ added in v1.7.2
func (p *Prometheus) SubGaugeValue(name string, labelValues []string, value float64) error
SubGaugeValue adds gauge to value.
func (*Prometheus) Use ¶
func (p *Prometheus) Use(e *gin.Engine)
Use is a method that should be used if the engine is set after middleware initialization.