Skip to content

Commit

Permalink
Merge pull request #1048 from ChaitanyaSingla/max-device-metric
Browse files Browse the repository at this point in the history
Add support for a gauge function and metric to track the maximum number of devices permitted to connect to a Talaria instance.
  • Loading branch information
denopink authored Oct 22, 2024
2 parents f31adfd + bb99563 commit 5a26f6f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
4 changes: 4 additions & 0 deletions device/drain/mocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,7 @@ func generateManagerWithDifferentDevices(assert *assert.Assertions, metadataOneC

return sm
}

func (sm *stubManager) MaxDevices() int {
return 1
}
5 changes: 5 additions & 0 deletions device/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ type Manager interface {
Connector
Router
Registry
MaxDevices() int
}

// ManagerOption is a configuration option for a manager
Expand Down Expand Up @@ -630,3 +631,7 @@ func (m *manager) Route(request *Request) (*Response, error) {
return nil, ErrorDeviceNotFound
}
}

func (m *manager) MaxDevices() int {
return m.devices.limit
}
19 changes: 19 additions & 0 deletions xmetrics/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 5a26f6f

Please sign in to comment.