diff --git a/go.mod b/go.mod index da7fdaac..588bd3f3 100644 --- a/go.mod +++ b/go.mod @@ -21,6 +21,7 @@ require ( github.com/miekg/dns v1.1.54 github.com/mitchellh/mapstructure v1.5.0 github.com/prometheus/client_golang v1.15.1 + github.com/prometheus/client_model v0.4.0 // indirect github.com/segmentio/ksuid v1.0.4 github.com/spaolacci/murmur3 v1.1.0 // indirect github.com/spf13/cast v1.5.1 diff --git a/go.sum b/go.sum index 0078248c..c1cdc91b 100644 --- a/go.sum +++ b/go.sum @@ -1463,6 +1463,8 @@ github.com/prometheus/client_model v0.1.0/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6T github.com/prometheus/client_model v0.2.0/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= github.com/prometheus/client_model v0.3.0 h1:UBgGFHqYdG/TPFD1B1ogZywDqEkwp3fBMvqdiQ7Xew4= github.com/prometheus/client_model v0.3.0/go.mod h1:LDGWKZIo7rky3hgvBe+caln+Dr3dPggB5dvjtD7w9+w= +github.com/prometheus/client_model v0.4.0 h1:5lQXD3cAg1OXBf4Wq03gTrXHeaV0TQvGfUooCfx1yqY= +github.com/prometheus/client_model v0.4.0/go.mod h1:oMQmHW1/JoDwqLtg57MGgP/Fb1CJEYF2imWWhWtMkYU= github.com/prometheus/common v0.0.0-20181113130724-41aa239b4cce/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro= github.com/prometheus/common v0.0.0-20181126121408-4724e9255275/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro= github.com/prometheus/common v0.2.0/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4= diff --git a/xmetrics/registry.go b/xmetrics/registry.go index 225a8989..61cabbcb 100644 --- a/xmetrics/registry.go +++ b/xmetrics/registry.go @@ -35,6 +35,9 @@ type Registry interface { PrometheusProvider provider.Provider prometheus.Gatherer + + NewPrometheusGaugeEx(namespace, subsystem, name string) prometheus.Gauge + NewPrometheusGauge(name string) prometheus.Gauge } // registry is the internal Registry implementation @@ -97,7 +100,7 @@ func (r *registry) NewGaugeVecEx(namespace, subsystem, name string) *prometheus. return gaugeVec } - panic(fmt.Errorf("The preregistered metric %s is not a gauge", key)) + panic(fmt.Errorf("the preregistered metric %s is not a gauge", key)) } gaugeVec := prometheus.NewGaugeVec( @@ -125,6 +128,30 @@ func (r *registry) NewGauge(name string) metrics.Gauge { return gokitprometheus.NewGauge(r.NewGaugeVec(name)) } +func (r *registry) NewPrometheusGauge(name string) prometheus.Gauge { + return r.NewPrometheusGaugeEx(r.namespace, r.subsystem, name) +} + +func (r *registry) NewPrometheusGaugeEx(namespace, subsystem, name string) prometheus.Gauge { + + gauge := prometheus.NewGauge(prometheus.GaugeOpts{ + Namespace: namespace, + Subsystem: subsystem, + Name: name, + Help: name, + }) + + if err := r.Register(gauge); err != nil { + if already, ok := err.(prometheus.AlreadyRegisteredError); ok { + return already.ExistingCollector.(prometheus.Gauge) + } else { + panic(err) + } + } + + return gauge +} + func (r *registry) NewHistogramVec(name string) *prometheus.HistogramVec { return r.NewHistogramVecEx(r.namespace, r.subsystem, name) }