Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lxd/metrics: Simplify TYPE handling #14922

Merged
merged 4 commits into from
Feb 5, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading