From 8850a4f60ba2718f8068013abb15354fbf783eb3 Mon Sep 17 00:00:00 2001 From: Joachim Bauch Date: Wed, 31 Jul 2024 10:21:01 +0200 Subject: [PATCH] Expose backend session limits through prometheus stats. --- backend_configuration_stats_prometheus.go | 19 +++++++++++++++++++ backend_storage_etcd.go | 4 ++++ backend_storage_static.go | 7 +++++++ docs/prometheus-metrics.md | 1 + 4 files changed, 31 insertions(+) diff --git a/backend_configuration_stats_prometheus.go b/backend_configuration_stats_prometheus.go index 13b7c7d6..d19d7f97 100644 --- a/backend_configuration_stats_prometheus.go +++ b/backend_configuration_stats_prometheus.go @@ -26,6 +26,12 @@ import ( ) var ( + statsBackendLimit = prometheus.NewGaugeVec(prometheus.GaugeOpts{ + Namespace: "signaling", + Subsystem: "backend", + Name: "session_limit", + Help: "The session limit of a backend", + }, []string{"backend"}) statsBackendLimitExceededTotal = prometheus.NewCounterVec(prometheus.CounterOpts{ Namespace: "signaling", Subsystem: "backend", @@ -40,6 +46,7 @@ var ( }) backendConfigurationStats = []prometheus.Collector{ + statsBackendLimit, statsBackendLimitExceededTotal, statsBackendsCurrent, } @@ -48,3 +55,15 @@ var ( func RegisterBackendConfigurationStats() { registerAll(backendConfigurationStats...) } + +func updateBackendStats(backend *Backend) { + if backend.sessionLimit > 0 { + statsBackendLimit.WithLabelValues(backend.id).Set(float64(backend.sessionLimit)) + } else { + statsBackendLimit.DeleteLabelValues(backend.id) + } +} + +func deleteBackendStats(backend *Backend) { + statsBackendLimit.DeleteLabelValues(backend.id) +} diff --git a/backend_storage_etcd.go b/backend_storage_etcd.go index cd669c80..ce82beff 100644 --- a/backend_storage_etcd.go +++ b/backend_storage_etcd.go @@ -202,6 +202,7 @@ func (s *backendStorageEtcd) EtcdKeyUpdated(client *EtcdClient, key string, data // Simple case, first backend for this host log.Printf("Added backend %s (from %s)", info.Url, key) s.backends[host] = []*Backend{backend} + updateBackendStats(backend) statsBackendsCurrent.Inc() s.wakeupForTesting() return @@ -212,6 +213,7 @@ func (s *backendStorageEtcd) EtcdKeyUpdated(client *EtcdClient, key string, data for idx, entry := range entries { if entry.id == key { log.Printf("Updated backend %s (from %s)", info.Url, key) + updateBackendStats(backend) entries[idx] = backend replaced = true break @@ -222,6 +224,7 @@ func (s *backendStorageEtcd) EtcdKeyUpdated(client *EtcdClient, key string, data // New backend, add to list. log.Printf("Added backend %s (from %s)", info.Url, key) s.backends[host] = append(entries, backend) + updateBackendStats(backend) statsBackendsCurrent.Inc() } s.wakeupForTesting() @@ -247,6 +250,7 @@ func (s *backendStorageEtcd) EtcdKeyDeleted(client *EtcdClient, key string, prev newEntries := make([]*Backend, 0, len(entries)-1) for _, entry := range entries { if entry.id == key { + updateBackendStats(entry) statsBackendsCurrent.Dec() continue } diff --git a/backend_storage_static.go b/backend_storage_static.go index d75427bd..4a60c3f3 100644 --- a/backend_storage_static.go +++ b/backend_storage_static.go @@ -64,12 +64,14 @@ func NewBackendStorageStatic(config *goconf.ConfigFile) (BackendStorage, error) if sessionLimit > 0 { log.Printf("Allow a maximum of %d sessions", sessionLimit) } + updateBackendStats(compatBackend) numBackends++ } else if backendIds, _ := config.GetString("backend", "backends"); backendIds != "" { for host, configuredBackends := range getConfiguredHosts(backendIds, config, commonSecret) { backends[host] = append(backends[host], configuredBackends...) for _, be := range configuredBackends { log.Printf("Backend %s added for %s", be.id, be.url) + updateBackendStats(be) } numBackends += len(configuredBackends) } @@ -111,6 +113,7 @@ func NewBackendStorageStatic(config *goconf.ConfigFile) (BackendStorage, error) if sessionLimit > 0 { log.Printf("Allow a maximum of %d sessions", sessionLimit) } + updateBackendStats(compatBackend) numBackends++ } } @@ -138,6 +141,7 @@ func (s *backendStorageStatic) RemoveBackendsForHost(host string) { if oldBackends := s.backends[host]; len(oldBackends) > 0 { for _, backend := range oldBackends { log.Printf("Backend %s removed for %s", backend.id, backend.url) + deleteBackendStats(backend) } statsBackendsCurrent.Sub(float64(len(oldBackends))) } @@ -158,6 +162,7 @@ func (s *backendStorageStatic) UpsertHost(host string, backends []*Backend) { s.backends[host][existingIndex] = newBackend backends = append(backends[:index], backends[index+1:]...) log.Printf("Backend %s updated for %s", newBackend.id, newBackend.url) + updateBackendStats(newBackend) break } index++ @@ -166,6 +171,7 @@ func (s *backendStorageStatic) UpsertHost(host string, backends []*Backend) { removed := s.backends[host][existingIndex] log.Printf("Backend %s removed for %s", removed.id, removed.url) s.backends[host] = append(s.backends[host][:existingIndex], s.backends[host][existingIndex+1:]...) + deleteBackendStats(removed) statsBackendsCurrent.Dec() } } @@ -173,6 +179,7 @@ func (s *backendStorageStatic) UpsertHost(host string, backends []*Backend) { s.backends[host] = append(s.backends[host], backends...) for _, added := range backends { log.Printf("Backend %s added for %s", added.id, added.url) + updateBackendStats(added) } statsBackendsCurrent.Add(float64(len(backends))) } diff --git a/docs/prometheus-metrics.md b/docs/prometheus-metrics.md index 5655f5d7..ebfe8ddb 100644 --- a/docs/prometheus-metrics.md +++ b/docs/prometheus-metrics.md @@ -24,6 +24,7 @@ The following metrics are available: | `signaling_proxy_command_messages_total` | Counter | 0.4.0 | The total number of command messages | `type` | | `signaling_proxy_payload_messages_total` | Counter | 0.4.0 | The total number of payload messages | `type` | | `signaling_proxy_token_errors_total` | Counter | 0.4.0 | The total number of token errors | `reason` | +| `signaling_backend_session_limit` | Gauge | 1.3.3 | The session limit of a backend (if set) | `backend` | | `signaling_backend_session_limit_exceeded_total` | Counter | 0.4.0 | The number of times the session limit exceeded | `backend` | | `signaling_backend_current` | Gauge | 0.4.0 | The current number of configured backends | | | `signaling_client_countries_total` | Counter | 0.4.0 | The total number of connections by country | `country` |