From b1685b311e457200e779dbf92fe328b1276b92d7 Mon Sep 17 00:00:00 2001 From: Simon Deziel Date: Tue, 4 Feb 2025 15:58:51 -0500 Subject: [PATCH 1/4] lxd/metrics: `TYPE` must not be `""` so default to counter An empty `TYPE` confuses Prometheus: > invalid metric type "" Signed-off-by: Simon Deziel --- lxd/metrics/metrics.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lxd/metrics/metrics.go b/lxd/metrics/metrics.go index 756c5cec70fe..358d7a28cb9a 100644 --- a/lxd/metrics/metrics.go +++ b/lxd/metrics/metrics.go @@ -105,7 +105,7 @@ func (m *MetricSet) String() string { return "" } - metricTypeName := "" + metricTypeName := "counter" // ProcsTotal is a gauge according to the OpenMetrics spec as its value can decrease. if shared.ValueInSlice(metricType, gaugeMetrics) { From a5bdd9b583d4ae2b8835e6dcd44feae585ff4c87 Mon Sep 17 00:00:00 2001 From: Simon Deziel Date: Tue, 4 Feb 2025 15:59:55 -0500 Subject: [PATCH 2/4] lxd/metrics: simplify `TYPE` handling Signed-off-by: Simon Deziel --- lxd/metrics/metrics.go | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/lxd/metrics/metrics.go b/lxd/metrics/metrics.go index 358d7a28cb9a..168eeaaa059f 100644 --- a/lxd/metrics/metrics.go +++ b/lxd/metrics/metrics.go @@ -105,14 +105,9 @@ func (m *MetricSet) String() string { return "" } - metricTypeName := "counter" - // ProcsTotal is a gauge according to the OpenMetrics spec as its value can decrease. - if shared.ValueInSlice(metricType, gaugeMetrics) { - metricTypeName = "gauge" - } else if strings.HasSuffix(MetricNames[metricType], "_total") || strings.HasSuffix(MetricNames[metricType], "_seconds") { - metricTypeName = "counter" - } else if strings.HasSuffix(MetricNames[metricType], "_bytes") { + metricTypeName := "counter" + if shared.ValueInSlice(metricType, gaugeMetrics) || strings.HasSuffix(MetricNames[metricType], "_bytes") { metricTypeName = "gauge" } From cc4dd23a164fb3c823b11ba0f7b546883f5e1e44 Mon Sep 17 00:00:00 2001 From: Simon Deziel Date: Tue, 4 Feb 2025 16:01:36 -0500 Subject: [PATCH 3/4] lxd/metrics: write `HELP` and `TYPE` messages in one go Signed-off-by: Simon Deziel --- lxd/metrics/metrics.go | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/lxd/metrics/metrics.go b/lxd/metrics/metrics.go index 168eeaaa059f..d6df543ddf91 100644 --- a/lxd/metrics/metrics.go +++ b/lxd/metrics/metrics.go @@ -99,20 +99,14 @@ func (m *MetricSet) String() string { } for _, metricType := range metricTypes { - // Add HELP message as specified by OpenMetrics - _, err := out.WriteString(MetricHeaders[metricType] + "\n") - if err != nil { - return "" - } - // ProcsTotal is a gauge according to the OpenMetrics spec as its value can decrease. metricTypeName := "counter" if shared.ValueInSlice(metricType, gaugeMetrics) || strings.HasSuffix(MetricNames[metricType], "_bytes") { metricTypeName = "gauge" } - // Add TYPE message as specified by OpenMetrics - _, err = out.WriteString("# TYPE " + MetricNames[metricType] + " " + metricTypeName + "\n") + // Add HELP and TYPE messages as specified by OpenMetrics + _, err := out.WriteString(MetricHeaders[metricType] + "\n# TYPE " + MetricNames[metricType] + " " + metricTypeName + "\n") if err != nil { return "" } From fd54e7918e6e183073d1975e3e705545168d22b2 Mon Sep 17 00:00:00 2001 From: Simon Deziel Date: Tue, 4 Feb 2025 16:08:00 -0500 Subject: [PATCH 4/4] lxd/metrics: simplify `TYPE` suffix handling Signed-off-by: Simon Deziel --- lxd/metrics/metrics.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lxd/metrics/metrics.go b/lxd/metrics/metrics.go index d6df543ddf91..62d8b791c99a 100644 --- a/lxd/metrics/metrics.go +++ b/lxd/metrics/metrics.go @@ -100,13 +100,13 @@ func (m *MetricSet) String() string { for _, metricType := range metricTypes { // ProcsTotal is a gauge according to the OpenMetrics spec as its value can decrease. - metricTypeName := "counter" + metricTypeNameSuffix := " counter\n" if shared.ValueInSlice(metricType, gaugeMetrics) || strings.HasSuffix(MetricNames[metricType], "_bytes") { - metricTypeName = "gauge" + metricTypeNameSuffix = " gauge\n" } // Add HELP and TYPE messages as specified by OpenMetrics - _, err := out.WriteString(MetricHeaders[metricType] + "\n# TYPE " + MetricNames[metricType] + " " + metricTypeName + "\n") + _, err := out.WriteString(MetricHeaders[metricType] + "\n# TYPE " + MetricNames[metricType] + metricTypeNameSuffix) if err != nil { return "" }