Skip to content

Commit fb39490

Browse files
authored
Added options support to SendSumOfCountersPerUser() (#1794)
* Added options support to SendSumOfCountersPerUser() Signed-off-by: Marco Pracucci <[email protected]> * Renamed SkipZeroValueMetrics() to WithSkipZeroValueMetrics() Signed-off-by: Marco Pracucci <[email protected]>
1 parent 9302ea6 commit fb39490

File tree

4 files changed

+32
-33
lines changed

4 files changed

+32
-33
lines changed

pkg/alertmanager/alertmanager_metrics.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -296,10 +296,10 @@ func (m *alertmanagerMetrics) Collect(out chan<- prometheus.Metric) {
296296
data.SendSumOfCountersPerUser(out, m.alertsReceived, "alertmanager_alerts_received_total")
297297
data.SendSumOfCountersPerUser(out, m.alertsInvalid, "alertmanager_alerts_invalid_total")
298298

299-
data.SendSumOfCountersPerUserWithLabelsAndOptions(out, m.numNotifications, "alertmanager_notifications_total", []string{"integration"}, util.SkipZeroValueMetrics)
300-
data.SendSumOfCountersPerUserWithLabelsAndOptions(out, m.numFailedNotifications, "alertmanager_notifications_failed_total", []string{"integration"}, util.SkipZeroValueMetrics)
301-
data.SendSumOfCountersPerUserWithLabelsAndOptions(out, m.numNotificationRequestsTotal, "alertmanager_notification_requests_total", []string{"integration"}, util.SkipZeroValueMetrics)
302-
data.SendSumOfCountersPerUserWithLabelsAndOptions(out, m.numNotificationRequestsFailedTotal, "alertmanager_notification_requests_failed_total", []string{"integration"}, util.SkipZeroValueMetrics)
299+
data.SendSumOfCountersPerUser(out, m.numNotifications, "alertmanager_notifications_total", util.WithLabels("integration"), util.WithSkipZeroValueMetrics)
300+
data.SendSumOfCountersPerUser(out, m.numFailedNotifications, "alertmanager_notifications_failed_total", util.WithLabels("integration"), util.WithSkipZeroValueMetrics)
301+
data.SendSumOfCountersPerUser(out, m.numNotificationRequestsTotal, "alertmanager_notification_requests_total", util.WithLabels("integration"), util.WithSkipZeroValueMetrics)
302+
data.SendSumOfCountersPerUser(out, m.numNotificationRequestsFailedTotal, "alertmanager_notification_requests_failed_total", util.WithLabels("integration"), util.WithSkipZeroValueMetrics)
303303
data.SendSumOfHistograms(out, m.notificationLatencySeconds, "alertmanager_notification_latency_seconds")
304304
data.SendSumOfGaugesPerUserWithLabels(out, m.markerAlerts, "alertmanager_alerts", "state")
305305

@@ -334,7 +334,7 @@ func (m *alertmanagerMetrics) Collect(out chan<- prometheus.Metric) {
334334
data.SendSumOfCounters(out, m.persistTotal, "alertmanager_state_persist_total")
335335
data.SendSumOfCounters(out, m.persistFailed, "alertmanager_state_persist_failed_total")
336336

337-
data.SendSumOfCountersPerUserWithLabelsAndOptions(out, m.notificationRateLimited, "alertmanager_notification_rate_limited_total", []string{"integration"}, util.SkipZeroValueMetrics)
337+
data.SendSumOfCountersPerUser(out, m.notificationRateLimited, "alertmanager_notification_rate_limited_total", util.WithLabels("integration"), util.WithSkipZeroValueMetrics)
338338
data.SendSumOfCountersPerUser(out, m.dispatcherAggregationGroupsLimitReached, "alertmanager_dispatcher_aggregation_group_limit_reached_total")
339339
data.SendSumOfCountersPerUser(out, m.insertAlertFailures, "alertmanager_alerts_insert_limited_total")
340340
data.SendSumOfGaugesPerUser(out, m.alertsLimiterAlertsCount, "alertmanager_alerts_limiter_current_alerts")

pkg/ruler/manager_metrics.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,10 @@ func (m *ManagerMetrics) Collect(out chan<- prometheus.Metric) {
139139
data.SendSumOfSummariesPerUser(out, m.EvalDuration, "prometheus_rule_evaluation_duration_seconds")
140140
data.SendSumOfSummariesPerUser(out, m.IterationDuration, "prometheus_rule_group_duration_seconds")
141141

142-
data.SendSumOfCountersPerUserWithLabels(out, m.IterationsMissed, "prometheus_rule_group_iterations_missed_total", "rule_group")
143-
data.SendSumOfCountersPerUserWithLabels(out, m.IterationsScheduled, "prometheus_rule_group_iterations_total", "rule_group")
144-
data.SendSumOfCountersPerUserWithLabels(out, m.EvalTotal, "prometheus_rule_evaluations_total", "rule_group")
145-
data.SendSumOfCountersPerUserWithLabels(out, m.EvalFailures, "prometheus_rule_evaluation_failures_total", "rule_group")
142+
data.SendSumOfCountersPerUser(out, m.IterationsMissed, "prometheus_rule_group_iterations_missed_total", util.WithLabels("rule_group"))
143+
data.SendSumOfCountersPerUser(out, m.IterationsScheduled, "prometheus_rule_group_iterations_total", util.WithLabels("rule_group"))
144+
data.SendSumOfCountersPerUser(out, m.EvalTotal, "prometheus_rule_evaluations_total", util.WithLabels("rule_group"))
145+
data.SendSumOfCountersPerUser(out, m.EvalFailures, "prometheus_rule_evaluation_failures_total", util.WithLabels("rule_group"))
146146
data.SendSumOfGaugesPerUserWithLabels(out, m.GroupInterval, "prometheus_rule_group_interval_seconds", "rule_group")
147147
data.SendSumOfGaugesPerUserWithLabels(out, m.GroupLastEvalTime, "prometheus_rule_group_last_evaluation_timestamp_seconds", "rule_group")
148148
data.SendSumOfGaugesPerUserWithLabels(out, m.GroupLastDuration, "prometheus_rule_group_last_duration_seconds", "rule_group")

pkg/util/metrics_helper.go

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -162,27 +162,18 @@ func (d MetricFamiliesPerUser) SendSumOfCountersWithLabels(out chan<- prometheus
162162
d.sumOfSingleValuesWithLabels(counter, counterValue, labelNames, false).WriteToMetricChannel(out, desc, prometheus.CounterValue)
163163
}
164164

165-
func (d MetricFamiliesPerUser) SendSumOfCountersPerUser(out chan<- prometheus.Metric, desc *prometheus.Desc, counter string) {
166-
d.SendSumOfCountersPerUserWithLabels(out, desc, counter)
167-
}
168-
169-
// SendSumOfCountersPerUserWithLabels provides metrics with the provided label names on a per-user basis. This function assumes that `user` is the
170-
// first label on the provided metric Desc
171-
func (d MetricFamiliesPerUser) SendSumOfCountersPerUserWithLabels(out chan<- prometheus.Metric, desc *prometheus.Desc, metric string, labelNames ...string) {
172-
d.SendSumOfCountersPerUserWithLabelsAndOptions(out, desc, metric, labelNames)
173-
}
165+
// SendSumOfCountersPerUser provides metrics on a per-user basis, with additional and optional label names.
166+
// This function assumes that `user` is the first label on the provided metric Desc.
167+
func (d MetricFamiliesPerUser) SendSumOfCountersPerUser(out chan<- prometheus.Metric, desc *prometheus.Desc, metric string, options ...MetricOption) {
168+
opts := applyMetricOptions(options...)
174169

175-
// SendSumOfCountersPerUserWithLabelsAndOptions provides metrics with the provided label names on a per-user basis. This function assumes that `user` is the
176-
// first label on the provided metric Desc
177-
func (d MetricFamiliesPerUser) SendSumOfCountersPerUserWithLabelsAndOptions(out chan<- prometheus.Metric, desc *prometheus.Desc, metric string, labelNames []string, options ...MetricOption) {
178170
for _, userEntry := range d {
179171
if userEntry.user == "" {
180172
continue
181173
}
182174

183175
result := singleValueWithLabelsMap{}
184-
opts := applyMetricOptions(options...)
185-
userEntry.metrics.sumOfSingleValuesWithLabels(metric, labelNames, counterValue, result.aggregateFn, opts.skipZeroValueMetrics)
176+
userEntry.metrics.sumOfSingleValuesWithLabels(metric, opts.labelNames, counterValue, result.aggregateFn, opts.skipZeroValueMetrics)
186177
result.prependUserLabelValue(userEntry.user)
187178
result.WriteToMetricChannel(out, desc, prometheus.CounterValue)
188179
}
@@ -825,11 +816,18 @@ type CollectorVec interface {
825816
// MetricOption defines a functional-style option for metrics aggregation.
826817
type MetricOption func(options *metricOptions)
827818

828-
// SkipZeroValueMetrics controls whether metrics aggregation should skip zero value metrics.
829-
func SkipZeroValueMetrics(options *metricOptions) {
819+
// WithSkipZeroValueMetrics controls whether metrics aggregation should skip zero value metrics.
820+
func WithSkipZeroValueMetrics(options *metricOptions) {
830821
options.skipZeroValueMetrics = true
831822
}
832823

824+
// WithLabels set labels to use for aggregations.
825+
func WithLabels(labelNames ...string) MetricOption {
826+
return func(options *metricOptions) {
827+
options.labelNames = labelNames
828+
}
829+
}
830+
833831
// applyMetricOptions returns a metricOptions with all the input options applied.
834832
func applyMetricOptions(options ...MetricOption) *metricOptions {
835833
actual := &metricOptions{}
@@ -842,4 +840,5 @@ func applyMetricOptions(options ...MetricOption) *metricOptions {
842840

843841
type metricOptions struct {
844842
skipZeroValueMetrics bool
843+
labelNames []string
845844
}

pkg/util/metrics_helper_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -322,9 +322,9 @@ func TestSendSumOfHistogramsWithLabels(t *testing.T) {
322322
}
323323
}
324324

325-
// TestSendSumOfCountersPerUserWithLabels tests to ensure multiple metrics for the same user with a matching label are
325+
// TestSendSumOfCountersPerUser_WithLabels tests to ensure multiple metrics for the same user with a matching label are
326326
// summed correctly
327-
func TestSendSumOfCountersPerUserWithLabels(t *testing.T) {
327+
func TestSendSumOfCountersPerUser_WithLabels(t *testing.T) {
328328
user1Metric := prometheus.NewCounterVec(prometheus.CounterOpts{Name: "test_metric"}, []string{"label_one", "label_two"})
329329
user2Metric := prometheus.NewCounterVec(prometheus.CounterOpts{Name: "test_metric"}, []string{"label_one", "label_two"})
330330
user3Metric := prometheus.NewCounterVec(prometheus.CounterOpts{Name: "test_metric"}, []string{"label_one", "label_two"})
@@ -351,7 +351,7 @@ func TestSendSumOfCountersPerUserWithLabels(t *testing.T) {
351351
t.Run("group metrics by user and label_one", func(t *testing.T) {
352352
desc := prometheus.NewDesc("test_metric", "", []string{"user", "label_one"}, nil)
353353
actual := collectMetrics(t, func(out chan prometheus.Metric) {
354-
mf.SendSumOfCountersPerUserWithLabels(out, desc, "test_metric", "label_one")
354+
mf.SendSumOfCountersPerUser(out, desc, "test_metric", WithLabels("label_one"))
355355
})
356356
expected := []*dto.Metric{
357357
{Label: makeLabels("label_one", "a", "user", "user-1"), Counter: &dto.Counter{Value: proto.Float64(180)}},
@@ -364,7 +364,7 @@ func TestSendSumOfCountersPerUserWithLabels(t *testing.T) {
364364
t.Run("group metrics by user and label_one, and skip zero value metrics", func(t *testing.T) {
365365
desc := prometheus.NewDesc("test_metric", "", []string{"user", "label_one"}, nil)
366366
actual := collectMetrics(t, func(out chan prometheus.Metric) {
367-
mf.SendSumOfCountersPerUserWithLabelsAndOptions(out, desc, "test_metric", []string{"label_one"}, SkipZeroValueMetrics)
367+
mf.SendSumOfCountersPerUser(out, desc, "test_metric", WithLabels("label_one"), WithSkipZeroValueMetrics)
368368
})
369369
expected := []*dto.Metric{
370370
{Label: makeLabels("label_one", "a", "user", "user-1"), Counter: &dto.Counter{Value: proto.Float64(180)}},
@@ -376,7 +376,7 @@ func TestSendSumOfCountersPerUserWithLabels(t *testing.T) {
376376
t.Run("group metrics by user and label_two", func(t *testing.T) {
377377
desc := prometheus.NewDesc("test_metric", "", []string{"user", "label_two"}, nil)
378378
actual := collectMetrics(t, func(out chan prometheus.Metric) {
379-
mf.SendSumOfCountersPerUserWithLabels(out, desc, "test_metric", "label_two")
379+
mf.SendSumOfCountersPerUser(out, desc, "test_metric", WithLabels("label_two"))
380380
})
381381
expected := []*dto.Metric{
382382
{Label: makeLabels("label_two", "b", "user", "user-1"), Counter: &dto.Counter{Value: proto.Float64(100)}},
@@ -392,7 +392,7 @@ func TestSendSumOfCountersPerUserWithLabels(t *testing.T) {
392392
t.Run("group metrics by user and label_two, and skip zero value metrics", func(t *testing.T) {
393393
desc := prometheus.NewDesc("test_metric", "", []string{"user", "label_two"}, nil)
394394
actual := collectMetrics(t, func(out chan prometheus.Metric) {
395-
mf.SendSumOfCountersPerUserWithLabelsAndOptions(out, desc, "test_metric", []string{"label_two"}, SkipZeroValueMetrics)
395+
mf.SendSumOfCountersPerUser(out, desc, "test_metric", WithLabels("label_two"), WithSkipZeroValueMetrics)
396396
})
397397
expected := []*dto.Metric{
398398
{Label: makeLabels("label_two", "b", "user", "user-1"), Counter: &dto.Counter{Value: proto.Float64(100)}},
@@ -405,7 +405,7 @@ func TestSendSumOfCountersPerUserWithLabels(t *testing.T) {
405405
t.Run("group metrics by user, label_one and label_two", func(t *testing.T) {
406406
desc := prometheus.NewDesc("test_metric", "", []string{"user", "label_one", "label_two"}, nil)
407407
actual := collectMetrics(t, func(out chan prometheus.Metric) {
408-
mf.SendSumOfCountersPerUserWithLabels(out, desc, "test_metric", "label_one", "label_two")
408+
mf.SendSumOfCountersPerUser(out, desc, "test_metric", WithLabels("label_one", "label_two"))
409409
})
410410
expected := []*dto.Metric{
411411
{Label: makeLabels("label_one", "a", "label_two", "b", "user", "user-1"), Counter: &dto.Counter{Value: proto.Float64(100)}},
@@ -421,7 +421,7 @@ func TestSendSumOfCountersPerUserWithLabels(t *testing.T) {
421421
t.Run("group metrics by user, label_one and label_two, and skip zero value metrics", func(t *testing.T) {
422422
desc := prometheus.NewDesc("test_metric", "", []string{"user", "label_one", "label_two"}, nil)
423423
actual := collectMetrics(t, func(out chan prometheus.Metric) {
424-
mf.SendSumOfCountersPerUserWithLabelsAndOptions(out, desc, "test_metric", []string{"label_one", "label_two"}, SkipZeroValueMetrics)
424+
mf.SendSumOfCountersPerUser(out, desc, "test_metric", WithLabels("label_one", "label_two"), WithSkipZeroValueMetrics)
425425
})
426426
expected := []*dto.Metric{
427427
{Label: makeLabels("label_one", "a", "label_two", "b", "user", "user-1"), Counter: &dto.Counter{Value: proto.Float64(100)}},

0 commit comments

Comments
 (0)