Metrics: Use string constants in API endpoint #5355

Signed-off-by: Michael Mayer <michael@photoprism.app>
This commit is contained in:
Michael Mayer
2025-11-30 10:50:26 +01:00
parent dc19035d8f
commit 96fe2c70a7

View File

@@ -22,15 +22,35 @@ import (
const ( const (
metricsNamespace = "photoprism" metricsNamespace = "photoprism"
metricsUsageSubsystem = "usage" metricsUsageSubsystem = "usage"
metricsStatisticsSubsystem = "statistics"
metricsClusterSubsystem = "cluster"
metricsLabelState = "state" metricsLabelState = "state"
metricsLabelStat = "stat"
metricsLabelRole = "role"
metricsLabelUUID = "uuid"
metricsLabelCIDR = "cidr"
metricsLabelEdition = "edition"
metricsLabelGoVer = "goversion"
metricsLabelVersion = "version"
metricFilesBytes = "files_bytes" metricFilesBytes = "files_bytes"
metricFilesRatio = "files_ratio" metricFilesRatio = "files_ratio"
metricAccountsRatio = "accounts_ratio" metricAccountsRatio = "accounts_ratio"
metricAccountsActive = "accounts_active" metricAccountsActive = "accounts_active"
metricMediaCount = "media_count"
metricBuildInfo = "build_info"
metricClusterNodes = "nodes"
metricClusterInfo = "info"
metricsAccountsHelp = "active user and guest accounts on this PhotoPrism instance" metricsAccountsHelp = "active user and guest accounts on this PhotoPrism instance"
metricsFilesBytesHelp = "filesystem usage in bytes for files indexed by this PhotoPrism instance" metricsFilesBytesHelp = "filesystem usage in bytes for files indexed by this PhotoPrism instance"
metricsFilesRatioHelp = "filesystem usage for files indexed by this PhotoPrism instance" metricsFilesRatioHelp = "filesystem usage for files indexed by this PhotoPrism instance"
metricsAccountsRatioHelp = "account quota usage for this PhotoPrism instance" metricsAccountsRatioHelp = "account quota usage for this PhotoPrism instance"
metricsMediaCountHelp = "media statistics for this PhotoPrism instance"
metricsBuildInfoHelp = "information about the photoprism instance"
metricsClusterNodesHelp = "registered cluster nodes grouped by role"
metricsClusterInfoHelp = "cluster metadata for this PhotoPrism portal"
) )
// GetMetrics provides a Prometheus-compatible metrics endpoint for monitoring the instance, including usage details and portal cluster metrics. // GetMetrics provides a Prometheus-compatible metrics endpoint for monitoring the instance, including usage details and portal cluster metrics.
@@ -94,11 +114,11 @@ func GetMetrics(router *gin.RouterGroup) {
func registerCountMetrics(factory promauto.Factory, counts config.ClientCounts) { func registerCountMetrics(factory promauto.Factory, counts config.ClientCounts) {
metric := factory.NewGaugeVec( metric := factory.NewGaugeVec(
prometheus.GaugeOpts{ prometheus.GaugeOpts{
Namespace: "photoprism", Namespace: metricsNamespace,
Subsystem: "statistics", Subsystem: metricsStatisticsSubsystem,
Name: "media_count", Name: metricMediaCount,
Help: "media statistics for this PhotoPrism instance", Help: metricsMediaCountHelp,
}, []string{"stat"}, }, []string{metricsLabelStat},
) )
stats := []struct { stats := []struct {
@@ -140,7 +160,7 @@ func registerCountMetrics(factory promauto.Factory, counts config.ClientCounts)
} }
for _, stat := range stats { for _, stat := range stats {
metric.With(prometheus.Labels{"stat": stat.label}).Set(float64(stat.value)) metric.With(prometheus.Labels{metricsLabelStat: stat.label}).Set(float64(stat.value))
} }
} }
@@ -148,14 +168,14 @@ func registerCountMetrics(factory promauto.Factory, counts config.ClientCounts)
func registerBuildInfoMetric(factory promauto.Factory, conf *config.ClientConfig) { func registerBuildInfoMetric(factory promauto.Factory, conf *config.ClientConfig) {
factory.NewGaugeVec( factory.NewGaugeVec(
prometheus.GaugeOpts{ prometheus.GaugeOpts{
Namespace: "photoprism", Namespace: metricsNamespace,
Name: "build_info", Name: metricBuildInfo,
Help: "information about the photoprism instance", Help: metricsBuildInfoHelp,
}, []string{"edition", "goversion", "version"}, }, []string{metricsLabelEdition, metricsLabelGoVer, metricsLabelVersion},
).With(prometheus.Labels{ ).With(prometheus.Labels{
"edition": conf.Edition, metricsLabelEdition: conf.Edition,
"goversion": runtime.Version(), metricsLabelGoVer: runtime.Version(),
"version": conf.Version, metricsLabelVersion: conf.Version,
}).Set(1.0) }).Set(1.0)
} }
@@ -228,29 +248,29 @@ func registerClusterMetrics(factory promauto.Factory, conf *config.Config) {
nodeMetric := factory.NewGaugeVec( nodeMetric := factory.NewGaugeVec(
prometheus.GaugeOpts{ prometheus.GaugeOpts{
Namespace: "photoprism", Namespace: metricsNamespace,
Subsystem: "cluster", Subsystem: metricsClusterSubsystem,
Name: "nodes", Name: metricClusterNodes,
Help: "registered cluster nodes grouped by role", Help: metricsClusterNodesHelp,
}, []string{"role"}, }, []string{metricsLabelRole},
) )
for role, value := range counts { for role, value := range counts {
nodeMetric.With(prometheus.Labels{"role": role}).Set(float64(value)) nodeMetric.With(prometheus.Labels{metricsLabelRole: role}).Set(float64(value))
} }
infoMetric := factory.NewGaugeVec( infoMetric := factory.NewGaugeVec(
prometheus.GaugeOpts{ prometheus.GaugeOpts{
Namespace: "photoprism", Namespace: metricsNamespace,
Subsystem: "cluster", Subsystem: metricsClusterSubsystem,
Name: "info", Name: metricClusterInfo,
Help: "cluster metadata for this PhotoPrism portal", Help: metricsClusterInfoHelp,
}, []string{"uuid", "cidr"}, }, []string{metricsLabelUUID, metricsLabelCIDR},
) )
infoMetric.With(prometheus.Labels{ infoMetric.With(prometheus.Labels{
"uuid": conf.ClusterUUID(), metricsLabelUUID: conf.ClusterUUID(),
"cidr": conf.ClusterCIDR(), metricsLabelCIDR: conf.ClusterCIDR(),
}).Set(1.0) }).Set(1.0)
} }