2017-08-23 21:46:03 +03:00
package metrics
import (
2020-03-05 17:10:07 +03:00
"errors"
"time"
2017-08-23 21:46:03 +03:00
"github.com/go-kit/kit/metrics"
"github.com/go-kit/kit/metrics/multi"
)
2021-10-06 18:34:07 +03:00
const defaultMetricsPrefix = "traefik"
2017-08-23 21:46:03 +03:00
// Registry has to implemented by any system that wants to monitor and expose metrics.
type Registry interface {
2019-07-18 22:36:05 +03:00
// IsEpEnabled shows whether metrics instrumentation is enabled on entry points.
IsEpEnabled ( ) bool
2021-04-30 11:22:04 +03:00
// IsRouterEnabled shows whether metrics instrumentation is enabled on routers.
IsRouterEnabled ( ) bool
2019-07-18 22:36:05 +03:00
// IsSvcEnabled shows whether metrics instrumentation is enabled on services.
IsSvcEnabled ( ) bool
2018-07-03 11:02:03 +03:00
2018-01-26 13:58:03 +03:00
// server metrics
2022-08-31 09:24:08 +03:00
2018-01-26 13:58:03 +03:00
ConfigReloadsCounter ( ) metrics . Counter
ConfigReloadsFailureCounter ( ) metrics . Counter
LastConfigReloadSuccessGauge ( ) metrics . Gauge
LastConfigReloadFailureGauge ( ) metrics . Gauge
2020-12-18 20:44:03 +03:00
// TLS
2022-08-31 09:24:08 +03:00
2020-12-18 20:44:03 +03:00
TLSCertsNotAfterTimestampGauge ( ) metrics . Gauge
2018-01-26 13:58:03 +03:00
// entry point metrics
2022-08-31 09:24:08 +03:00
2023-03-20 20:06:07 +03:00
EntryPointReqsCounter ( ) CounterWithHeaders
2020-03-05 15:30:05 +03:00
EntryPointReqsTLSCounter ( ) metrics . Counter
2020-03-05 17:10:07 +03:00
EntryPointReqDurationHistogram ( ) ScalableHistogram
2019-07-18 22:36:05 +03:00
EntryPointOpenConnsGauge ( ) metrics . Gauge
2022-09-12 18:10:09 +03:00
EntryPointReqsBytesCounter ( ) metrics . Counter
EntryPointRespsBytesCounter ( ) metrics . Counter
2018-01-26 13:58:03 +03:00
2021-04-30 11:22:04 +03:00
// router metrics
2022-08-31 09:24:08 +03:00
2023-03-20 20:06:07 +03:00
RouterReqsCounter ( ) CounterWithHeaders
2021-04-30 11:22:04 +03:00
RouterReqsTLSCounter ( ) metrics . Counter
RouterReqDurationHistogram ( ) ScalableHistogram
RouterOpenConnsGauge ( ) metrics . Gauge
2022-09-12 18:10:09 +03:00
RouterReqsBytesCounter ( ) metrics . Counter
RouterRespsBytesCounter ( ) metrics . Counter
2021-04-30 11:22:04 +03:00
2019-07-18 22:36:05 +03:00
// service metrics
2022-08-31 09:24:08 +03:00
2023-03-20 20:06:07 +03:00
ServiceReqsCounter ( ) CounterWithHeaders
2020-03-05 15:30:05 +03:00
ServiceReqsTLSCounter ( ) metrics . Counter
2020-03-05 17:10:07 +03:00
ServiceReqDurationHistogram ( ) ScalableHistogram
2019-07-18 22:36:05 +03:00
ServiceOpenConnsGauge ( ) metrics . Gauge
ServiceRetriesCounter ( ) metrics . Counter
ServiceServerUpGauge ( ) metrics . Gauge
2022-09-12 18:10:09 +03:00
ServiceReqsBytesCounter ( ) metrics . Counter
ServiceRespsBytesCounter ( ) metrics . Counter
2018-01-26 13:58:03 +03:00
}
// NewVoidRegistry is a noop implementation of metrics.Registry.
// It is used to avoid nil checking in components that do metric collections.
func NewVoidRegistry ( ) Registry {
return NewMultiRegistry ( [ ] Registry { } )
2017-08-23 21:46:03 +03:00
}
2018-01-26 13:58:03 +03:00
// NewMultiRegistry is an implementation of metrics.Registry that wraps multiple registries.
// It handles the case when a registry hasn't registered some metric and returns nil.
2022-09-12 18:10:09 +03:00
// This allows for feature disparity between the different metric implementations.
2017-08-23 21:46:03 +03:00
func NewMultiRegistry ( registries [ ] Registry ) Registry {
2018-07-03 11:02:03 +03:00
var configReloadsCounter [ ] metrics . Counter
var configReloadsFailureCounter [ ] metrics . Counter
var lastConfigReloadSuccessGauge [ ] metrics . Gauge
var lastConfigReloadFailureGauge [ ] metrics . Gauge
2020-12-18 20:44:03 +03:00
var tlsCertsNotAfterTimestampGauge [ ] metrics . Gauge
2023-03-20 20:06:07 +03:00
var entryPointReqsCounter [ ] CounterWithHeaders
2020-03-05 15:30:05 +03:00
var entryPointReqsTLSCounter [ ] metrics . Counter
2020-03-05 17:10:07 +03:00
var entryPointReqDurationHistogram [ ] ScalableHistogram
2019-07-18 22:36:05 +03:00
var entryPointOpenConnsGauge [ ] metrics . Gauge
2022-09-12 18:10:09 +03:00
var entryPointReqsBytesCounter [ ] metrics . Counter
var entryPointRespsBytesCounter [ ] metrics . Counter
2023-03-20 20:06:07 +03:00
var routerReqsCounter [ ] CounterWithHeaders
2021-04-30 11:22:04 +03:00
var routerReqsTLSCounter [ ] metrics . Counter
var routerReqDurationHistogram [ ] ScalableHistogram
var routerOpenConnsGauge [ ] metrics . Gauge
2022-09-12 18:10:09 +03:00
var routerReqsBytesCounter [ ] metrics . Counter
var routerRespsBytesCounter [ ] metrics . Counter
2023-03-20 20:06:07 +03:00
var serviceReqsCounter [ ] CounterWithHeaders
2020-03-05 15:30:05 +03:00
var serviceReqsTLSCounter [ ] metrics . Counter
2020-03-05 17:10:07 +03:00
var serviceReqDurationHistogram [ ] ScalableHistogram
2019-07-18 22:36:05 +03:00
var serviceOpenConnsGauge [ ] metrics . Gauge
var serviceRetriesCounter [ ] metrics . Counter
var serviceServerUpGauge [ ] metrics . Gauge
2022-09-12 18:10:09 +03:00
var serviceReqsBytesCounter [ ] metrics . Counter
var serviceRespsBytesCounter [ ] metrics . Counter
2017-08-23 21:46:03 +03:00
for _ , r := range registries {
2018-01-26 13:58:03 +03:00
if r . ConfigReloadsCounter ( ) != nil {
configReloadsCounter = append ( configReloadsCounter , r . ConfigReloadsCounter ( ) )
}
if r . ConfigReloadsFailureCounter ( ) != nil {
configReloadsFailureCounter = append ( configReloadsFailureCounter , r . ConfigReloadsFailureCounter ( ) )
}
if r . LastConfigReloadSuccessGauge ( ) != nil {
lastConfigReloadSuccessGauge = append ( lastConfigReloadSuccessGauge , r . LastConfigReloadSuccessGauge ( ) )
}
if r . LastConfigReloadFailureGauge ( ) != nil {
lastConfigReloadFailureGauge = append ( lastConfigReloadFailureGauge , r . LastConfigReloadFailureGauge ( ) )
}
2020-12-18 20:44:03 +03:00
if r . TLSCertsNotAfterTimestampGauge ( ) != nil {
tlsCertsNotAfterTimestampGauge = append ( tlsCertsNotAfterTimestampGauge , r . TLSCertsNotAfterTimestampGauge ( ) )
}
2019-07-18 22:36:05 +03:00
if r . EntryPointReqsCounter ( ) != nil {
entryPointReqsCounter = append ( entryPointReqsCounter , r . EntryPointReqsCounter ( ) )
2018-01-26 13:58:03 +03:00
}
2020-03-05 15:30:05 +03:00
if r . EntryPointReqsTLSCounter ( ) != nil {
entryPointReqsTLSCounter = append ( entryPointReqsTLSCounter , r . EntryPointReqsTLSCounter ( ) )
}
2019-07-18 22:36:05 +03:00
if r . EntryPointReqDurationHistogram ( ) != nil {
entryPointReqDurationHistogram = append ( entryPointReqDurationHistogram , r . EntryPointReqDurationHistogram ( ) )
2018-01-26 13:58:03 +03:00
}
2019-07-18 22:36:05 +03:00
if r . EntryPointOpenConnsGauge ( ) != nil {
entryPointOpenConnsGauge = append ( entryPointOpenConnsGauge , r . EntryPointOpenConnsGauge ( ) )
2018-01-26 13:58:03 +03:00
}
2022-09-12 18:10:09 +03:00
if r . EntryPointReqsBytesCounter ( ) != nil {
entryPointReqsBytesCounter = append ( entryPointReqsBytesCounter , r . EntryPointReqsBytesCounter ( ) )
}
if r . EntryPointRespsBytesCounter ( ) != nil {
entryPointRespsBytesCounter = append ( entryPointRespsBytesCounter , r . EntryPointRespsBytesCounter ( ) )
}
2021-04-30 11:22:04 +03:00
if r . RouterReqsCounter ( ) != nil {
routerReqsCounter = append ( routerReqsCounter , r . RouterReqsCounter ( ) )
}
if r . RouterReqsTLSCounter ( ) != nil {
routerReqsTLSCounter = append ( routerReqsTLSCounter , r . RouterReqsTLSCounter ( ) )
}
if r . RouterReqDurationHistogram ( ) != nil {
routerReqDurationHistogram = append ( routerReqDurationHistogram , r . RouterReqDurationHistogram ( ) )
}
if r . RouterOpenConnsGauge ( ) != nil {
routerOpenConnsGauge = append ( routerOpenConnsGauge , r . RouterOpenConnsGauge ( ) )
}
2022-09-12 18:10:09 +03:00
if r . RouterReqsBytesCounter ( ) != nil {
routerReqsBytesCounter = append ( routerReqsBytesCounter , r . RouterReqsBytesCounter ( ) )
}
if r . RouterRespsBytesCounter ( ) != nil {
routerRespsBytesCounter = append ( routerRespsBytesCounter , r . RouterRespsBytesCounter ( ) )
}
2019-07-18 22:36:05 +03:00
if r . ServiceReqsCounter ( ) != nil {
serviceReqsCounter = append ( serviceReqsCounter , r . ServiceReqsCounter ( ) )
2018-01-26 13:58:03 +03:00
}
2020-03-05 15:30:05 +03:00
if r . ServiceReqsTLSCounter ( ) != nil {
serviceReqsTLSCounter = append ( serviceReqsTLSCounter , r . ServiceReqsTLSCounter ( ) )
}
2019-07-18 22:36:05 +03:00
if r . ServiceReqDurationHistogram ( ) != nil {
serviceReqDurationHistogram = append ( serviceReqDurationHistogram , r . ServiceReqDurationHistogram ( ) )
2018-01-26 13:58:03 +03:00
}
2019-07-18 22:36:05 +03:00
if r . ServiceOpenConnsGauge ( ) != nil {
serviceOpenConnsGauge = append ( serviceOpenConnsGauge , r . ServiceOpenConnsGauge ( ) )
2018-01-26 13:58:03 +03:00
}
2019-07-18 22:36:05 +03:00
if r . ServiceRetriesCounter ( ) != nil {
serviceRetriesCounter = append ( serviceRetriesCounter , r . ServiceRetriesCounter ( ) )
2018-01-26 13:58:03 +03:00
}
2019-07-18 22:36:05 +03:00
if r . ServiceServerUpGauge ( ) != nil {
serviceServerUpGauge = append ( serviceServerUpGauge , r . ServiceServerUpGauge ( ) )
2018-01-26 13:58:03 +03:00
}
2022-09-12 18:10:09 +03:00
if r . ServiceReqsBytesCounter ( ) != nil {
serviceReqsBytesCounter = append ( serviceReqsBytesCounter , r . ServiceReqsBytesCounter ( ) )
}
if r . ServiceRespsBytesCounter ( ) != nil {
serviceRespsBytesCounter = append ( serviceRespsBytesCounter , r . ServiceRespsBytesCounter ( ) )
}
2017-08-23 21:46:03 +03:00
}
return & standardRegistry {
2019-07-18 22:36:05 +03:00
epEnabled : len ( entryPointReqsCounter ) > 0 || len ( entryPointReqDurationHistogram ) > 0 || len ( entryPointOpenConnsGauge ) > 0 ,
svcEnabled : len ( serviceReqsCounter ) > 0 || len ( serviceReqDurationHistogram ) > 0 || len ( serviceOpenConnsGauge ) > 0 || len ( serviceRetriesCounter ) > 0 || len ( serviceServerUpGauge ) > 0 ,
2021-04-30 11:22:04 +03:00
routerEnabled : len ( routerReqsCounter ) > 0 || len ( routerReqDurationHistogram ) > 0 || len ( routerOpenConnsGauge ) > 0 ,
2018-01-26 13:58:03 +03:00
configReloadsCounter : multi . NewCounter ( configReloadsCounter ... ) ,
configReloadsFailureCounter : multi . NewCounter ( configReloadsFailureCounter ... ) ,
lastConfigReloadSuccessGauge : multi . NewGauge ( lastConfigReloadSuccessGauge ... ) ,
lastConfigReloadFailureGauge : multi . NewGauge ( lastConfigReloadFailureGauge ... ) ,
2020-12-18 20:44:03 +03:00
tlsCertsNotAfterTimestampGauge : multi . NewGauge ( tlsCertsNotAfterTimestampGauge ... ) ,
2023-03-20 20:06:07 +03:00
entryPointReqsCounter : NewMultiCounterWithHeaders ( entryPointReqsCounter ... ) ,
2020-03-05 15:30:05 +03:00
entryPointReqsTLSCounter : multi . NewCounter ( entryPointReqsTLSCounter ... ) ,
2022-09-12 18:10:09 +03:00
entryPointReqDurationHistogram : MultiHistogram ( entryPointReqDurationHistogram ) ,
2019-07-18 22:36:05 +03:00
entryPointOpenConnsGauge : multi . NewGauge ( entryPointOpenConnsGauge ... ) ,
2022-09-12 18:10:09 +03:00
entryPointReqsBytesCounter : multi . NewCounter ( entryPointReqsBytesCounter ... ) ,
entryPointRespsBytesCounter : multi . NewCounter ( entryPointRespsBytesCounter ... ) ,
2023-03-20 20:06:07 +03:00
routerReqsCounter : NewMultiCounterWithHeaders ( routerReqsCounter ... ) ,
2021-04-30 11:22:04 +03:00
routerReqsTLSCounter : multi . NewCounter ( routerReqsTLSCounter ... ) ,
2022-09-12 18:10:09 +03:00
routerReqDurationHistogram : MultiHistogram ( routerReqDurationHistogram ) ,
2021-04-30 11:22:04 +03:00
routerOpenConnsGauge : multi . NewGauge ( routerOpenConnsGauge ... ) ,
2022-09-12 18:10:09 +03:00
routerReqsBytesCounter : multi . NewCounter ( routerReqsBytesCounter ... ) ,
routerRespsBytesCounter : multi . NewCounter ( routerRespsBytesCounter ... ) ,
2023-03-20 20:06:07 +03:00
serviceReqsCounter : NewMultiCounterWithHeaders ( serviceReqsCounter ... ) ,
2020-03-05 15:30:05 +03:00
serviceReqsTLSCounter : multi . NewCounter ( serviceReqsTLSCounter ... ) ,
2022-09-12 18:10:09 +03:00
serviceReqDurationHistogram : MultiHistogram ( serviceReqDurationHistogram ) ,
2019-07-18 22:36:05 +03:00
serviceOpenConnsGauge : multi . NewGauge ( serviceOpenConnsGauge ... ) ,
serviceRetriesCounter : multi . NewCounter ( serviceRetriesCounter ... ) ,
serviceServerUpGauge : multi . NewGauge ( serviceServerUpGauge ... ) ,
2022-09-12 18:10:09 +03:00
serviceReqsBytesCounter : multi . NewCounter ( serviceReqsBytesCounter ... ) ,
serviceRespsBytesCounter : multi . NewCounter ( serviceRespsBytesCounter ... ) ,
2017-08-23 21:46:03 +03:00
}
}
type standardRegistry struct {
2019-07-18 22:36:05 +03:00
epEnabled bool
2021-04-30 11:22:04 +03:00
routerEnabled bool
2019-07-18 22:36:05 +03:00
svcEnabled bool
2018-01-26 13:58:03 +03:00
configReloadsCounter metrics . Counter
configReloadsFailureCounter metrics . Counter
lastConfigReloadSuccessGauge metrics . Gauge
lastConfigReloadFailureGauge metrics . Gauge
2020-12-18 20:44:03 +03:00
tlsCertsNotAfterTimestampGauge metrics . Gauge
2023-03-20 20:06:07 +03:00
entryPointReqsCounter CounterWithHeaders
2020-03-05 15:30:05 +03:00
entryPointReqsTLSCounter metrics . Counter
2020-03-05 17:10:07 +03:00
entryPointReqDurationHistogram ScalableHistogram
2019-07-18 22:36:05 +03:00
entryPointOpenConnsGauge metrics . Gauge
2022-09-12 18:10:09 +03:00
entryPointReqsBytesCounter metrics . Counter
entryPointRespsBytesCounter metrics . Counter
2023-03-20 20:06:07 +03:00
routerReqsCounter CounterWithHeaders
2021-04-30 11:22:04 +03:00
routerReqsTLSCounter metrics . Counter
routerReqDurationHistogram ScalableHistogram
routerOpenConnsGauge metrics . Gauge
2022-09-12 18:10:09 +03:00
routerReqsBytesCounter metrics . Counter
routerRespsBytesCounter metrics . Counter
2023-03-20 20:06:07 +03:00
serviceReqsCounter CounterWithHeaders
2020-03-05 15:30:05 +03:00
serviceReqsTLSCounter metrics . Counter
2020-03-05 17:10:07 +03:00
serviceReqDurationHistogram ScalableHistogram
2019-07-18 22:36:05 +03:00
serviceOpenConnsGauge metrics . Gauge
serviceRetriesCounter metrics . Counter
serviceServerUpGauge metrics . Gauge
2022-09-12 18:10:09 +03:00
serviceReqsBytesCounter metrics . Counter
serviceRespsBytesCounter metrics . Counter
2017-08-23 21:46:03 +03:00
}
2019-07-18 22:36:05 +03:00
func ( r * standardRegistry ) IsEpEnabled ( ) bool {
return r . epEnabled
}
2021-04-30 11:22:04 +03:00
func ( r * standardRegistry ) IsRouterEnabled ( ) bool {
return r . routerEnabled
}
2019-07-18 22:36:05 +03:00
func ( r * standardRegistry ) IsSvcEnabled ( ) bool {
return r . svcEnabled
2017-08-23 21:46:03 +03:00
}
2018-01-26 13:58:03 +03:00
func ( r * standardRegistry ) ConfigReloadsCounter ( ) metrics . Counter {
return r . configReloadsCounter
2017-08-23 21:46:03 +03:00
}
2018-01-26 13:58:03 +03:00
func ( r * standardRegistry ) ConfigReloadsFailureCounter ( ) metrics . Counter {
return r . configReloadsFailureCounter
2017-08-23 21:46:03 +03:00
}
2018-01-26 13:58:03 +03:00
func ( r * standardRegistry ) LastConfigReloadSuccessGauge ( ) metrics . Gauge {
return r . lastConfigReloadSuccessGauge
2017-08-23 21:46:03 +03:00
}
2018-01-26 13:58:03 +03:00
func ( r * standardRegistry ) LastConfigReloadFailureGauge ( ) metrics . Gauge {
return r . lastConfigReloadFailureGauge
2017-08-23 21:46:03 +03:00
}
2020-12-18 20:44:03 +03:00
func ( r * standardRegistry ) TLSCertsNotAfterTimestampGauge ( ) metrics . Gauge {
return r . tlsCertsNotAfterTimestampGauge
}
2023-03-20 20:06:07 +03:00
func ( r * standardRegistry ) EntryPointReqsCounter ( ) CounterWithHeaders {
2019-07-18 22:36:05 +03:00
return r . entryPointReqsCounter
2018-01-26 13:58:03 +03:00
}
2020-03-05 15:30:05 +03:00
func ( r * standardRegistry ) EntryPointReqsTLSCounter ( ) metrics . Counter {
return r . entryPointReqsTLSCounter
}
2020-03-05 17:10:07 +03:00
func ( r * standardRegistry ) EntryPointReqDurationHistogram ( ) ScalableHistogram {
2019-07-18 22:36:05 +03:00
return r . entryPointReqDurationHistogram
2018-01-26 13:58:03 +03:00
}
2019-07-18 22:36:05 +03:00
func ( r * standardRegistry ) EntryPointOpenConnsGauge ( ) metrics . Gauge {
return r . entryPointOpenConnsGauge
2018-01-26 13:58:03 +03:00
}
2022-09-12 18:10:09 +03:00
func ( r * standardRegistry ) EntryPointReqsBytesCounter ( ) metrics . Counter {
return r . entryPointReqsBytesCounter
}
func ( r * standardRegistry ) EntryPointRespsBytesCounter ( ) metrics . Counter {
return r . entryPointRespsBytesCounter
}
2023-03-20 20:06:07 +03:00
func ( r * standardRegistry ) RouterReqsCounter ( ) CounterWithHeaders {
2021-04-30 11:22:04 +03:00
return r . routerReqsCounter
}
func ( r * standardRegistry ) RouterReqsTLSCounter ( ) metrics . Counter {
return r . routerReqsTLSCounter
}
func ( r * standardRegistry ) RouterReqDurationHistogram ( ) ScalableHistogram {
return r . routerReqDurationHistogram
}
func ( r * standardRegistry ) RouterOpenConnsGauge ( ) metrics . Gauge {
return r . routerOpenConnsGauge
}
2022-09-12 18:10:09 +03:00
func ( r * standardRegistry ) RouterReqsBytesCounter ( ) metrics . Counter {
return r . routerReqsBytesCounter
}
func ( r * standardRegistry ) RouterRespsBytesCounter ( ) metrics . Counter {
return r . routerRespsBytesCounter
}
2023-03-20 20:06:07 +03:00
func ( r * standardRegistry ) ServiceReqsCounter ( ) CounterWithHeaders {
2019-07-18 22:36:05 +03:00
return r . serviceReqsCounter
2018-01-26 13:58:03 +03:00
}
2020-03-05 15:30:05 +03:00
func ( r * standardRegistry ) ServiceReqsTLSCounter ( ) metrics . Counter {
return r . serviceReqsTLSCounter
}
2020-03-05 17:10:07 +03:00
func ( r * standardRegistry ) ServiceReqDurationHistogram ( ) ScalableHistogram {
2019-07-18 22:36:05 +03:00
return r . serviceReqDurationHistogram
2018-01-26 13:58:03 +03:00
}
2017-08-23 21:46:03 +03:00
2019-07-18 22:36:05 +03:00
func ( r * standardRegistry ) ServiceOpenConnsGauge ( ) metrics . Gauge {
return r . serviceOpenConnsGauge
2018-01-26 13:58:03 +03:00
}
2017-08-23 21:46:03 +03:00
2019-07-18 22:36:05 +03:00
func ( r * standardRegistry ) ServiceRetriesCounter ( ) metrics . Counter {
return r . serviceRetriesCounter
2018-01-26 13:58:03 +03:00
}
2017-08-23 21:46:03 +03:00
2019-07-18 22:36:05 +03:00
func ( r * standardRegistry ) ServiceServerUpGauge ( ) metrics . Gauge {
return r . serviceServerUpGauge
2018-01-26 13:58:03 +03:00
}
2020-03-05 17:10:07 +03:00
2022-09-12 18:10:09 +03:00
func ( r * standardRegistry ) ServiceReqsBytesCounter ( ) metrics . Counter {
return r . serviceReqsBytesCounter
}
func ( r * standardRegistry ) ServiceRespsBytesCounter ( ) metrics . Counter {
return r . serviceRespsBytesCounter
}
2020-03-05 17:10:07 +03:00
// ScalableHistogram is a Histogram with a predefined time unit,
// used when producing observations without explicitly setting the observed value.
type ScalableHistogram interface {
With ( labelValues ... string ) ScalableHistogram
Observe ( v float64 )
2020-03-19 15:48:04 +03:00
ObserveFromStart ( start time . Time )
2020-03-05 17:10:07 +03:00
}
// HistogramWithScale is a histogram that will convert its observed value to the specified unit.
type HistogramWithScale struct {
histogram metrics . Histogram
unit time . Duration
}
// With implements ScalableHistogram.
func ( s * HistogramWithScale ) With ( labelValues ... string ) ScalableHistogram {
2020-03-19 15:48:04 +03:00
h , _ := NewHistogramWithScale ( s . histogram . With ( labelValues ... ) , s . unit )
return h
2020-03-05 17:10:07 +03:00
}
2020-03-19 15:48:04 +03:00
// ObserveFromStart implements ScalableHistogram.
func ( s * HistogramWithScale ) ObserveFromStart ( start time . Time ) {
2020-03-05 17:10:07 +03:00
if s . unit <= 0 {
return
}
2020-03-19 15:48:04 +03:00
d := float64 ( time . Since ( start ) . Nanoseconds ( ) ) / float64 ( s . unit )
2020-03-05 17:10:07 +03:00
if d < 0 {
d = 0
}
s . histogram . Observe ( d )
}
// Observe implements ScalableHistogram.
func ( s * HistogramWithScale ) Observe ( v float64 ) {
s . histogram . Observe ( v )
}
// NewHistogramWithScale returns a ScalableHistogram. It returns an error if the given unit is <= 0.
func NewHistogramWithScale ( histogram metrics . Histogram , unit time . Duration ) ( ScalableHistogram , error ) {
if unit <= 0 {
return nil , errors . New ( "invalid time unit" )
}
return & HistogramWithScale {
histogram : histogram ,
unit : unit ,
} , nil
}
// MultiHistogram collects multiple individual histograms and treats them as a unit.
type MultiHistogram [ ] ScalableHistogram
2020-03-19 15:48:04 +03:00
// ObserveFromStart implements ScalableHistogram.
func ( h MultiHistogram ) ObserveFromStart ( start time . Time ) {
2020-03-05 17:10:07 +03:00
for _ , histogram := range h {
2020-03-19 15:48:04 +03:00
histogram . ObserveFromStart ( start )
2020-03-05 17:10:07 +03:00
}
}
// Observe implements ScalableHistogram.
func ( h MultiHistogram ) Observe ( v float64 ) {
for _ , histogram := range h {
histogram . Observe ( v )
}
}
// With implements ScalableHistogram.
func ( h MultiHistogram ) With ( labelValues ... string ) ScalableHistogram {
next := make ( MultiHistogram , len ( h ) )
for i := range h {
next [ i ] = h [ i ] . With ( labelValues ... )
}
return next
}