Skip to content

Commit

Permalink
lxd/metrics: Simplify TYPE handling (#14922)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomponline authored Feb 5, 2025
2 parents c071f4a + fd54e79 commit b71e599
Showing 1 changed file with 5 additions and 16 deletions.
21 changes: 5 additions & 16 deletions lxd/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,25 +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 ""
}

metricTypeName := ""

// 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 = "gauge"
metricTypeNameSuffix := " counter\n"
if shared.ValueInSlice(metricType, gaugeMetrics) || strings.HasSuffix(MetricNames[metricType], "_bytes") {
metricTypeNameSuffix = " gauge\n"
}

// 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] + metricTypeNameSuffix)
if err != nil {
return ""
}
Expand Down

0 comments on commit b71e599

Please sign in to comment.