metrics.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Copyright (C) 2025 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package config
  7. import (
  8. "strconv"
  9. "github.com/prometheus/client_golang/prometheus"
  10. )
  11. // RegisterInfoMetrics registers Prometheus metrics for the given config
  12. // wrapper.
  13. func RegisterInfoMetrics(cfg Wrapper) {
  14. prometheus.DefaultRegisterer.MustRegister(prometheus.CollectorFunc((&folderInfoMetric{cfg}).Collect))
  15. prometheus.DefaultRegisterer.MustRegister(prometheus.CollectorFunc((&folderDeviceMetric{cfg}).Collect))
  16. }
  17. type folderInfoMetric struct {
  18. cfg Wrapper
  19. }
  20. var folderInfoMetricDesc = prometheus.NewDesc(
  21. "syncthing_config_folder_info",
  22. "Provides additional information labels on folders",
  23. []string{"folder", "label", "type", "path", "paused"},
  24. nil,
  25. )
  26. func (m *folderInfoMetric) Collect(ch chan<- prometheus.Metric) {
  27. for _, folder := range m.cfg.FolderList() {
  28. ch <- prometheus.MustNewConstMetric(
  29. folderInfoMetricDesc,
  30. prometheus.GaugeValue, 1,
  31. folder.ID, folder.Label, folder.Type.String(), folder.Path, strconv.FormatBool(folder.Paused),
  32. )
  33. }
  34. }
  35. type folderDeviceMetric struct {
  36. cfg Wrapper
  37. }
  38. var folderDeviceMetricDesc = prometheus.NewDesc(
  39. "syncthing_config_device_info",
  40. "Provides additional information labels on devices",
  41. []string{"device", "name", "introducer", "paused", "untrusted"},
  42. nil,
  43. )
  44. func (m *folderDeviceMetric) Collect(ch chan<- prometheus.Metric) {
  45. for _, device := range m.cfg.DeviceList() {
  46. ch <- prometheus.MustNewConstMetric(
  47. folderDeviceMetricDesc,
  48. prometheus.GaugeValue, 1,
  49. device.DeviceID.String(), device.Name, strconv.FormatBool(device.Introducer), strconv.FormatBool(device.Paused), strconv.FormatBool(device.Untrusted),
  50. )
  51. }
  52. }