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

lib/connections: Add syncthing_connections_active metric (fixes #9527) #9528

Merged
merged 4 commits into from
May 4, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 27 additions & 0 deletions lib/connections/metrics.go
Original file line number Diff line number Diff line change
@@ -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)
}
9 changes: 9 additions & 0 deletions lib/connections/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -846,13 +846,15 @@ 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 {
if !newDevices[dev.DeviceID] {
warningLimitersMut.Lock()
delete(warningLimiters, dev.DeviceID)
warningLimitersMut.Unlock()
metricDeviceActiveConnections.DeleteLabelValues(dev.DeviceID.String())
}
}

Expand Down Expand Up @@ -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)
}
Expand All @@ -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])
}

Expand Down
1 change: 1 addition & 0 deletions lib/protocol/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,6 @@ func registerDeviceMetrics(deviceID string) {
metricDeviceSentUncompressedBytes.WithLabelValues(deviceID)
metricDeviceSentMessages.WithLabelValues(deviceID)
metricDeviceRecvBytes.WithLabelValues(deviceID)
metricDeviceRecvDecompressedBytes.WithLabelValues(deviceID)
metricDeviceRecvMessages.WithLabelValues(deviceID)
}