Documentation
¶
Index ¶
- func NewMetric(m *Metric, subsystem string) prometheus.Collector
- type Config
- type Metric
- type Prometheus
- func (p *Prometheus) HandlerFunc() gin.HandlerFunc
- func (p *Prometheus) SetListenAddress(address string)
- func (p *Prometheus) SetListenAddressWithRouter(listenAddress string, r *gin.Engine)
- func (p *Prometheus) SetMetricsPath(e *gin.Engine)
- func (p *Prometheus) SetMetricsPathWithAuth(e *gin.Engine, accounts gin.Accounts)
- func (p *Prometheus) SetPushGateway(pushGatewayURL, metricsURL string, pushIntervalSeconds time.Duration)
- func (p *Prometheus) SetPushGatewayJob(j string)
- func (p *Prometheus) Use(e *gin.Engine)
- func (p *Prometheus) UseWithAuth(e *gin.Engine, accounts gin.Accounts)
- type PrometheusPushGateway
- type RequestCounterURLLabelMappingFn
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Config ¶ added in v1.0.0
type Config struct {
// Subsystem is the subsystem name to use for the metrics.
Subsystem string
// MetricsList is an optional list of custom metrics to be exposed.
MetricsList []*Metric
// CustomLabels is a map of custom labels to be added to all metrics.
CustomLabels map[string]string
// DisableBodyReading is a boolean that disables reading the request body.
DisableBodyReading bool
}
Config is a struct for configuring the Prometheus middleware.
type Metric ¶
type Metric struct {
// MetricCollector is the prometheus.Collector that will be used to store the
// metric.
MetricCollector prometheus.Collector
// ID is a unique identifier for the metric.
ID string
// Name is the name of the metric.
Name string
// Description is a short description of the metric.
Description string
// Type is the type of the metric. It can be one of the following:
// counter, counter_vec, gauge, gauge_vec, histogram, histogram_vec, summary,
// summary_vec.
Type string
// Args is a list of labels that can be used to distinguish between different
// dimensions of the same metric.
Args []string
}
Metric defines a prometheus metric. It is used to create a new prometheus collector.
type Prometheus ¶
type Prometheus struct {
// Ppg is the Prometheus Push Gateway configuration.
Ppg PrometheusPushGateway
// MetricsList is a list of custom metrics to be exposed.
MetricsList []*Metric
// MetricsPath is the path where the metrics will be exposed.
MetricsPath string
// ReqCntURLLabelMappingFn is a function that can be used to map the URL
// to a different label value.
ReqCntURLLabelMappingFn RequestCounterURLLabelMappingFn
// URLLabelFromContext is the name of the context key that will be used
// to get the URL label from the context.
URLLabelFromContext string
CustomLabels map[string]string
// DisableBodyReading is a boolean that disables reading the request body.
DisableBodyReading bool
// contains filtered or unexported fields
}
Prometheus is a middleware that exports Prometheus metrics.
func NewPrometheus ¶
func NewPrometheus(subsystem string, customMetricsList ...[]*Metric) *Prometheus
NewPrometheus creates a new Prometheus middleware for backward compatibility. It's recommended to use NewWithConfig for new projects.
func NewWithConfig ¶ added in v1.0.0
func NewWithConfig(cfg Config) *Prometheus
NewWithConfig creates a new Prometheus middleware.
func (*Prometheus) HandlerFunc ¶
func (p *Prometheus) HandlerFunc() gin.HandlerFunc
HandlerFunc returns the gin.HandlerFunc that should be used as a middleware.
func (*Prometheus) SetListenAddress ¶
func (p *Prometheus) SetListenAddress(address string)
SetListenAddress sets the address where the metrics will be exposed.
func (*Prometheus) SetListenAddressWithRouter ¶
func (p *Prometheus) SetListenAddressWithRouter(listenAddress string, r *gin.Engine)
SetListenAddressWithRouter sets the address and a custom router where the metrics will be exposed.
func (*Prometheus) SetMetricsPath ¶
func (p *Prometheus) SetMetricsPath(e *gin.Engine)
SetMetricsPath sets the path where the metrics will be exposed.
func (*Prometheus) SetMetricsPathWithAuth ¶
func (p *Prometheus) SetMetricsPathWithAuth(e *gin.Engine, accounts gin.Accounts)
SetMetricsPathWithAuth sets the path where the metrics will be exposed and protects it with basic authentication.
func (*Prometheus) SetPushGateway ¶
func (p *Prometheus) SetPushGateway(pushGatewayURL, metricsURL string, pushIntervalSeconds time.Duration)
SetPushGateway configures the middleware to push metrics to a Prometheus pushgateway.
pushGatewayURL is the URL of the pushgateway.
metricsURL is the URL where the metrics are exposed.
pushIntervalSeconds is the interval at which metrics will be pushed to the pushgateway.
func (*Prometheus) SetPushGatewayJob ¶
func (p *Prometheus) SetPushGatewayJob(j string)
SetPushGatewayJob sets the job name for the pushgateway.
func (*Prometheus) Use ¶
func (p *Prometheus) Use(e *gin.Engine)
Use adds the middleware to a gin engine.
func (*Prometheus) UseWithAuth ¶
func (p *Prometheus) UseWithAuth(e *gin.Engine, accounts gin.Accounts)
UseWithAuth adds the middleware to a gin engine with basic authentication.
type PrometheusPushGateway ¶
type PrometheusPushGateway struct {
// PushIntervalSeconds is the interval at which metrics will be pushed to the
// pushgateway.
PushIntervalSeconds time.Duration
// PushGatewayURL is the URL of the pushgateway.
PushGatewayURL string
// MetricsURL is the URL where the metrics are exposed.
MetricsURL string
// Job is the job name that will be used when pushing to the pushgateway.
Job string
}
PrometheusPushGateway contains the configuration for pushing to a Prometheus pushgateway.
type RequestCounterURLLabelMappingFn ¶
RequestCounterURLLabelMappingFn is a function which can be supplied to the middleware to control the cardinality of the request counter's "url" label, which might be required in some contexts. For instance, if for a "/customer/:name" route you don't want to generate a time series for every possible customer name, you could use this function:
func(c *gin.Context) string {
url := c.Request.URL.Path
for _, p := range c.Params {
if p.Key == "name" {
url = strings.Replace(url, p.Value, ":name", 1)
break
}
}
return url
}
which would map "/customer/alice" and "/customer/bob" to their template "/customer/:name".