diff --git a/device/drain/mocks_test.go b/device/drain/mocks_test.go index c0799141..177c1db5 100644 --- a/device/drain/mocks_test.go +++ b/device/drain/mocks_test.go @@ -185,3 +185,7 @@ func generateManagerWithDifferentDevices(assert *assert.Assertions, metadataOneC return sm } + +func (sm *stubManager) MaxDevices() int { + return 1 +} diff --git a/device/manager.go b/device/manager.go index 56a4a842..ab41c804 100644 --- a/device/manager.go +++ b/device/manager.go @@ -111,6 +111,7 @@ type Manager interface { Connector Router Registry + MaxDevices() int } // ManagerOption is a configuration option for a manager @@ -630,3 +631,7 @@ func (m *manager) Route(request *Request) (*Response, error) { return nil, ErrorDeviceNotFound } } + +func (m *manager) MaxDevices() int { + return m.devices.limit +} diff --git a/xmetrics/registry.go b/xmetrics/registry.go index 61cabbcb..0d07672b 100644 --- a/xmetrics/registry.go +++ b/xmetrics/registry.go @@ -38,6 +38,7 @@ type Registry interface { NewPrometheusGaugeEx(namespace, subsystem, name string) prometheus.Gauge NewPrometheusGauge(name string) prometheus.Gauge + NewGaugeFunc(name string, f func() float64) prometheus.GaugeFunc } // registry is the internal Registry implementation @@ -128,6 +129,24 @@ func (r *registry) NewGauge(name string) metrics.Gauge { return gokitprometheus.NewGauge(r.NewGaugeVec(name)) } +func (r *registry) NewGaugeFunc(name string, f func() float64) prometheus.GaugeFunc { + gauge := prometheus.NewGaugeFunc(prometheus.GaugeOpts{ + Namespace: r.namespace, + Subsystem: r.subsystem, + Name: name, + Help: name, + }, f) + + if err := r.Register(gauge); err != nil { + if already, ok := err.(prometheus.AlreadyRegisteredError); ok { + return already.ExistingCollector.(prometheus.GaugeFunc) + } else { + panic(err) + } + } + return gauge +} + func (r *registry) NewPrometheusGauge(name string) prometheus.Gauge { return r.NewPrometheusGaugeEx(r.namespace, r.subsystem, name) }