diff options
author | DerRockWolf <50499906+DerRockWolf@users.noreply.github.com> | 2024-05-04 22:31:37 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-05-04 22:31:37 +0200 |
commit | debbe726e0e7f90b96022659b72d7991cf501a18 (patch) | |
tree | 9715e5ea7bdcd956cc677903cb4d18a9583a5044 | |
parent | ec3e474a53201f508e07f14a8fe409fe1879327a (diff) |
lib/connections: Add syncthing_connections_active metric (fixes #9527) (#9528)
### Purpose
Adds a new metric `syncthing_connections_active` which equals to the
amount of active connections per device.
Fixes #9527
<!--
Describe the purpose of this change. If there is an existing issue that
is
resolved by this pull request, ensure that the commit subject is on the
form
`Some short description (fixes #1234)` where 1234 is the issue number.
-->
### Testing
I've manually tested it by running syncthing with these changes locally
and examining the returned metrics from `/metrics`.
I've done the following things:
- Connect & disconnect a device
- Increase & decrease the number of connections and verify that the
value of the metric matches with the amount displayed in the GUI.
### Documentation
https://github.com/syncthing/docs/blob/main/includes/metrics-list.rst
needs to be regenerated with
[find-metrics.go](https://github.com/syncthing/docs/blob/main/_script/find-metrics/find-metrics.go)
## Authorship
Your name and email will be added automatically to the AUTHORS file
based on the commit metadata.
---------
Co-authored-by: Jakob Borg <jakob@kastelo.net>
-rw-r--r-- | lib/connections/metrics.go | 27 | ||||
-rw-r--r-- | lib/connections/service.go | 9 | ||||
-rw-r--r-- | lib/protocol/metrics.go | 1 |
3 files changed, 37 insertions, 0 deletions
diff --git a/lib/connections/metrics.go b/lib/connections/metrics.go new file mode 100644 index 0000000000..e002833915 --- /dev/null +++ b/lib/connections/metrics.go @@ -0,0 +1,27 @@ +// Copyright (C) 2024 The Syncthing Authors. +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this file, +// You can obtain one at https://mozilla.org/MPL/2.0/. + +package connections + +import ( + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" +) + +var ( + metricDeviceActiveConnections = promauto.NewGaugeVec(prometheus.GaugeOpts{ + Namespace: "syncthing", + Subsystem: "connections", + Name: "active", + Help: "Number of currently active connections, per device. If value is 0, the device is disconnected.", + }, []string{"device"}) +) + +func registerDeviceMetrics(deviceID string) { + // Register metrics for this device, so that counters & gauges are present even + // when zero. + metricDeviceActiveConnections.WithLabelValues(deviceID) +} diff --git a/lib/connections/service.go b/lib/connections/service.go index 359dcff4d5..5b69f2f99a 100644 --- a/lib/connections/service.go +++ b/lib/connections/service.go @@ -846,6 +846,7 @@ func (s *service) CommitConfiguration(from, to config.Configuration) bool { newDevices := make(map[protocol.DeviceID]bool, len(to.Devices)) for _, dev := range to.Devices { newDevices[dev.DeviceID] = true + registerDeviceMetrics(dev.DeviceID.String()) } for _, dev := range from.Devices { @@ -853,6 +854,7 @@ func (s *service) CommitConfiguration(from, to config.Configuration) bool { warningLimitersMut.Lock() delete(warningLimiters, dev.DeviceID) warningLimitersMut.Unlock() + metricDeviceActiveConnections.DeleteLabelValues(dev.DeviceID.String()) } } @@ -1378,6 +1380,9 @@ func (c *deviceConnectionTracker) accountAddedConnection(conn protocol.Connectio c.wantConnections[d] = int(h.NumConnections) l.Debugf("Added connection for %s (now %d), they want %d connections", d.Short(), len(c.connections[d]), h.NumConnections) + // Update active connections metric + metricDeviceActiveConnections.WithLabelValues(d.String()).Inc() + // Close any connections we no longer want to retain. c.closeWorsePriorityConnectionsLocked(d, conn.Priority()-upgradeThreshold) } @@ -1399,6 +1404,10 @@ func (c *deviceConnectionTracker) accountRemovedConnection(conn protocol.Connect delete(c.connections, d) delete(c.wantConnections, d) } + + // Update active connections metric + metricDeviceActiveConnections.WithLabelValues(d.String()).Dec() + l.Debugf("Removed connection for %s (now %d)", d.Short(), c.connections[d]) } diff --git a/lib/protocol/metrics.go b/lib/protocol/metrics.go index 1f57e51fd9..d1873b7d16 100644 --- a/lib/protocol/metrics.go +++ b/lib/protocol/metrics.go @@ -58,5 +58,6 @@ func registerDeviceMetrics(deviceID string) { metricDeviceSentUncompressedBytes.WithLabelValues(deviceID) metricDeviceSentMessages.WithLabelValues(deviceID) metricDeviceRecvBytes.WithLabelValues(deviceID) + metricDeviceRecvDecompressedBytes.WithLabelValues(deviceID) metricDeviceRecvMessages.WithLabelValues(deviceID) } |