Skip to content

Commit 4026d23

Browse files
XciDAdrien Carreira
andauthored
Prometheus add host relabel (#53)
* Add relabel fct for host * Revert go.sum Co-authored-by: Adrien Carreira <[email protected]>
1 parent 96966f5 commit 4026d23

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

prometheus/prometheus.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ var standardMetrics = []*Metric{
7777
}
7878

7979
/*
80-
RequestCounterURLLabelMappingFunc is a function which can be supplied to the middleware to control
80+
RequestCounterLabelMappingFunc is a function which can be supplied to the middleware to control
8181
the cardinality of the request counter's "url" label, which might be required in some contexts.
8282
For instance, if for a "/customer/:name" route you don't want to generate a time series for every
8383
possible customer name, you could use this function:
@@ -94,8 +94,9 @@ func(c echo.Context) string {
9494
}
9595
9696
which would map "/customer/alice" and "/customer/bob" to their template "/customer/:name".
97+
It can also be applied for the "Host" label
9798
*/
98-
type RequestCounterURLLabelMappingFunc func(c echo.Context) string
99+
type RequestCounterLabelMappingFunc func(c echo.Context) string
99100

100101
// Metric is a definition for the name, description, type, ID, and
101102
// prometheus.Collector type (i.e. CounterVec, Summary, etc) of each metric
@@ -121,7 +122,8 @@ type Prometheus struct {
121122
Subsystem string
122123
Skipper middleware.Skipper
123124

124-
RequestCounterURLLabelMappingFunc RequestCounterURLLabelMappingFunc
125+
RequestCounterURLLabelMappingFunc RequestCounterLabelMappingFunc
126+
RequestCounterHostLabelMappingFunc RequestCounterLabelMappingFunc
125127

126128
// Context string to use as a prometheus URL label
127129
URLLabelFromContext string
@@ -165,6 +167,9 @@ func NewPrometheus(subsystem string, skipper middleware.Skipper, customMetricsLi
165167
RequestCounterURLLabelMappingFunc: func(c echo.Context) string {
166168
return c.Path() // i.e. by default do nothing, i.e. return URL as is
167169
},
170+
RequestCounterHostLabelMappingFunc: func(c echo.Context) string {
171+
return c.Request().Host
172+
},
168173
}
169174

170175
p.registerMetrics(subsystem)
@@ -390,7 +395,7 @@ func (p *Prometheus) HandlerFunc(next echo.HandlerFunc) echo.HandlerFunc {
390395
url = u.(string)
391396
}
392397

393-
p.reqCnt.WithLabelValues(status, c.Request().Method, c.Request().Host, url).Inc()
398+
p.reqCnt.WithLabelValues(status, c.Request().Method, p.RequestCounterHostLabelMappingFunc(c), url).Inc()
394399
p.reqSz.WithLabelValues(status, c.Request().Method, url).Observe(float64(reqSz))
395400
p.resSz.WithLabelValues(status, c.Request().Method, url).Observe(resSz)
396401

0 commit comments

Comments
 (0)