Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for a gauge function and metric to track the maximum number of devices permitted to connect to a Talaria instance. #1048

Merged
merged 4 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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