Skip to content

Commit

Permalink
Expose backend session limits through prometheus stats.
Browse files Browse the repository at this point in the history
  • Loading branch information
fancycode committed Jul 31, 2024
1 parent c90d7ae commit 8850a4f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
19 changes: 19 additions & 0 deletions backend_configuration_stats_prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -40,6 +46,7 @@ var (
})

backendConfigurationStats = []prometheus.Collector{
statsBackendLimit,
statsBackendLimitExceededTotal,
statsBackendsCurrent,
}
Expand All @@ -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)
}
4 changes: 4 additions & 0 deletions backend_storage_etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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()
Expand All @@ -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
}
Expand Down
7 changes: 7 additions & 0 deletions backend_storage_static.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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++
}
}
Expand Down Expand Up @@ -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)))
}
Expand All @@ -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++
Expand All @@ -166,13 +171,15 @@ 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()
}
}

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)))
}
Expand Down
1 change: 1 addition & 0 deletions docs/prometheus-metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` |
Expand Down

0 comments on commit 8850a4f

Please sign in to comment.