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

Fix: size of labels must be const #40

Merged
merged 4 commits into from
Aug 3, 2024
Merged
Show file tree
Hide file tree
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
20 changes: 4 additions & 16 deletions metrics/registry.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package metrics

import (
"sort"
"strings"
"sync"

Expand Down Expand Up @@ -38,7 +37,7 @@ func (r *registry) sanitizeMetricName(name string) string {
return strings.ReplaceAll(name, "-", "_")
}

// Inc increments a counter for the given Series, allowing additional custom labels.
// Inc increments a counter for the given Series.
func (r *registry) Inc(name string, labels prometheus.Labels) {
r.metricsMu.Lock()
defer r.metricsMu.Unlock()
Expand All @@ -50,14 +49,14 @@ func (r *registry) Inc(name string, labels prometheus.Labels) {
Subsystem: r.Subsystem,
Namespace: r.Namespace,
Name: sanitized,
}, labelKeys(labels))
}, []string{"series_type", "sub_type", "operation", "status", "error_code"})
r.PromRegistry.MustRegister(counter)
r.counters[sanitized] = counter
}
counter.With(labels).Inc()
}

// RecordDuration records a duration for the given Series, allowing additional custom labels.
// RecordDuration records a duration for the given Series.
func (r *registry) RecordDuration(name string, labels prometheus.Labels, duration float64) {
r.metricsMu.Lock()
defer r.metricsMu.Unlock()
Expand All @@ -70,7 +69,7 @@ func (r *registry) RecordDuration(name string, labels prometheus.Labels, duratio
Namespace: r.Namespace,
Name: sanitized,
Buckets: prometheus.DefBuckets,
}, labelKeys(labels))
}, []string{"series_type", "sub_type", "operation"})
r.PromRegistry.MustRegister(histogram)
r.histograms[sanitized] = histogram
}
Expand All @@ -89,14 +88,3 @@ func registerMetrics(registry *registry) {
collectors.WithGoCollectorRuntimeMetrics(collectors.MetricsScheduler),
))
}

// labelKeys returns the keys of the labels in a deterministic order.
func labelKeys(labels prometheus.Labels) []string {
keys := make([]string, 0, len(labels))
for k := range labels {
keys = append(keys, k)
}
// Sort keys to ensure consistent label ordering
sort.Strings(keys)
return keys
}
30 changes: 17 additions & 13 deletions metrics/series.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
seriesType SeriesType
subType string
operation string
status string

Check failure on line 16 in metrics/series.go

View workflow job for this annotation

GitHub Actions / Lint and Test Go Code

field `status` is unused (unused)
labels prometheus.Labels
}

Expand Down Expand Up @@ -93,19 +93,20 @@
}

const (
seriesTypeInfo = "info"
seriesTypeSuccess = "success"
seriesTypeError = "error"
seriesTypeInfo = "info"
seriesTypeSuccess = "success"
seriesTypeError = "error"
seriesTypeDuration = "duration"
)

// Info returns the metric name and labels for an informational event.
func (s Series) Info(message string) (string, prometheus.Labels) {
labels := prometheus.Labels{
"series_type": s.seriesType.String(),
"sub_type": s.subType,
"operation": s.operation,
"status": seriesTypeInfo,
"info_message": message,
"series_type": s.seriesType.String(),
"sub_type": s.subType,
"operation": s.operation,
"status": seriesTypeInfo,
"message": message,
}
return "operation_count", mergeLabels(labels, s.labels)
}
Expand All @@ -117,18 +118,19 @@
"sub_type": s.subType,
"operation": s.operation,
"status": seriesTypeSuccess,
"message": "",
}
return "operation_count", mergeLabels(labels, s.labels)
}

// Error returns the metric name and labels for an error event.
func (s Series) Error(message string) (string, prometheus.Labels) {
labels := prometheus.Labels{
"series_type": s.seriesType.String(),
"sub_type": s.subType,
"operation": s.operation,
"status": seriesTypeError,
"error_message": message,
"series_type": s.seriesType.String(),
"sub_type": s.subType,
"operation": s.operation,
"status": seriesTypeError,
"message": message,
}
return "operation_count", mergeLabels(labels, s.labels)
}
Expand All @@ -139,6 +141,8 @@
"series_type": s.seriesType.String(),
"sub_type": s.subType,
"operation": s.operation,
"status": seriesTypeDuration,
"message": "",
}
return "operation_duration_seconds", mergeLabels(labels, s.labels)
}
Expand Down
Loading