Add prometheus metric requests_total with headers

Co-authored-by: Julien Salleyron <julien.salleyron@gmail.com>
This commit is contained in:
Romain 2023-03-20 18:06:07 +01:00 committed by GitHub
parent 4bc2305ed3
commit c823879097
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
20 changed files with 295 additions and 90 deletions

57
pkg/metrics/headers.go Normal file
View file

@ -0,0 +1,57 @@
package metrics
import (
"net/http"
"github.com/go-kit/kit/metrics"
)
// CounterWithHeaders represents a counter that can use http.Header values as label values.
type CounterWithHeaders interface {
Add(delta float64)
With(headers http.Header, labelValues ...string) CounterWithHeaders
}
// MultiCounterWithHeaders collects multiple individual CounterWithHeaders and treats them as a unit.
type MultiCounterWithHeaders []CounterWithHeaders
// NewMultiCounterWithHeaders returns a multi-counter, wrapping the passed CounterWithHeaders.
func NewMultiCounterWithHeaders(c ...CounterWithHeaders) MultiCounterWithHeaders {
return c
}
// Add adds the given delta value to the counter value.
func (c MultiCounterWithHeaders) Add(delta float64) {
for _, counter := range c {
counter.Add(delta)
}
}
// With creates a new counter by appending the given label values and http.Header as labels and returns it.
func (c MultiCounterWithHeaders) With(headers http.Header, labelValues ...string) CounterWithHeaders {
next := make(MultiCounterWithHeaders, len(c))
for i := range c {
next[i] = c[i].With(headers, labelValues...)
}
return next
}
// NewCounterWithNoopHeaders returns a CounterWithNoopHeaders.
func NewCounterWithNoopHeaders(counter metrics.Counter) CounterWithNoopHeaders {
return CounterWithNoopHeaders{counter: counter}
}
// CounterWithNoopHeaders is a counter that satisfies CounterWithHeaders but ignores the given http.Header.
type CounterWithNoopHeaders struct {
counter metrics.Counter
}
// Add adds the given delta value to the counter value.
func (c CounterWithNoopHeaders) Add(delta float64) {
c.counter.Add(delta)
}
// With creates a new counter by appending the given label values and returns it.
func (c CounterWithNoopHeaders) With(_ http.Header, labelValues ...string) CounterWithHeaders {
return NewCounterWithNoopHeaders(c.counter.With(labelValues...))
}