diff --git a/cmd/syncthing/main.go b/cmd/syncthing/main.go index 458f0761e..01256fec1 100644 --- a/cmd/syncthing/main.go +++ b/cmd/syncthing/main.go @@ -578,6 +578,7 @@ func syncthingMain(options serveOptions) { os.Exit(svcutil.ExitError.AsInt()) } earlyService.Add(cfgWrapper) + config.RegisterInfoMetrics(cfgWrapper) // Candidate builds should auto upgrade. Make sure the option is set, // unless we are in a build where it's disabled or the STNOUPGRADE diff --git a/lib/config/metrics.go b/lib/config/metrics.go new file mode 100644 index 000000000..fadc83439 --- /dev/null +++ b/lib/config/metrics.go @@ -0,0 +1,62 @@ +// Copyright (C) 2025 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 config + +import ( + "strconv" + + "github.com/prometheus/client_golang/prometheus" +) + +// RegisterInfoMetrics registers Prometheus metrics for the given config +// wrapper. +func RegisterInfoMetrics(cfg Wrapper) { + prometheus.DefaultRegisterer.MustRegister(prometheus.CollectorFunc((&folderInfoMetric{cfg}).Collect)) + prometheus.DefaultRegisterer.MustRegister(prometheus.CollectorFunc((&folderDeviceMetric{cfg}).Collect)) +} + +type folderInfoMetric struct { + cfg Wrapper +} + +var folderInfoMetricDesc = prometheus.NewDesc( + "syncthing_config_folder_info", + "Provides additional information labels on folders", + []string{"folder", "label", "type", "path", "paused"}, + nil, +) + +func (m *folderInfoMetric) Collect(ch chan<- prometheus.Metric) { + for _, folder := range m.cfg.FolderList() { + ch <- prometheus.MustNewConstMetric( + folderInfoMetricDesc, + prometheus.GaugeValue, 1, + folder.ID, folder.Label, folder.Type.String(), folder.Path, strconv.FormatBool(folder.Paused), + ) + } +} + +type folderDeviceMetric struct { + cfg Wrapper +} + +var folderDeviceMetricDesc = prometheus.NewDesc( + "syncthing_config_device_info", + "Provides additional information labels on devices", + []string{"device", "name", "introducer", "paused", "untrusted"}, + nil, +) + +func (m *folderDeviceMetric) Collect(ch chan<- prometheus.Metric) { + for _, device := range m.cfg.DeviceList() { + ch <- prometheus.MustNewConstMetric( + folderDeviceMetricDesc, + prometheus.GaugeValue, 1, + device.DeviceID.String(), device.Name, strconv.FormatBool(device.Introducer), strconv.FormatBool(device.Paused), strconv.FormatBool(device.Untrusted), + ) + } +}