From d3b9766b441e3543596c1dceaf55d845f816dd6b Mon Sep 17 00:00:00 2001 From: Adam Libresco Date: Sun, 5 Nov 2023 00:05:51 -0400 Subject: [PATCH] Fix bug corrupting prometheus timeseries --- .../prometheus_output/prometheus_common.go | 13 +++++---- .../prometheus_common_test.go | 28 +++++++++++++++++++ 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/outputs/prometheus_output/prometheus_common.go b/outputs/prometheus_output/prometheus_common.go index 763c966a..d9220e75 100644 --- a/outputs/prometheus_output/prometheus_common.go +++ b/outputs/prometheus_output/prometheus_common.go @@ -143,14 +143,17 @@ func (m *MetricBuilder) TimeSeriesFromEvent(ev *formatters.EventMsg) []*NamedTim fv = 1.0 } tsName := m.MetricName(ev.Name, k) + tsLabelsWithName := make([]prompb.Label, 0, len(tsLabels)+1) + tsLabelsWithName = append(tsLabelsWithName, tsLabels...) + tsLabelsWithName = append(tsLabelsWithName, + prompb.Label{ + Name: labels.MetricName, + Value: tsName, + }) nts := &NamedTimeSeries{ Name: tsName, TS: &prompb.TimeSeries{ - Labels: append(tsLabels, - prompb.Label{ - Name: labels.MetricName, - Value: m.MetricName(ev.Name, k), - }), + Labels: tsLabelsWithName, Samples: []prompb.Sample{ { Value: fv, diff --git a/outputs/prometheus_output/prometheus_common_test.go b/outputs/prometheus_output/prometheus_common_test.go index 292972c9..e333b883 100644 --- a/outputs/prometheus_output/prometheus_common_test.go +++ b/outputs/prometheus_output/prometheus_common_test.go @@ -10,6 +10,9 @@ package prometheus_output import ( "testing" + + "github.com/openconfig/gnmic/formatters" + "github.com/prometheus/prometheus/model/labels" ) var metricNameSet = map[string]struct { @@ -69,6 +72,31 @@ var metricNameSet = map[string]struct { }, } +func TestTimeSeriesFromEvent(t *testing.T) { + metricBuilder := &MetricBuilder{StringsAsLabels: true} + event := &formatters.EventMsg{ + Name: "eventName", + Timestamp: 12345, + Tags: map[string]string{ + "tagName": "tagVal", + }, + Values: map[string]interface{}{ + "strName1": "strVal1", + "strName2": "strVal2", + "intName1": 1, + "intName2": 2, + }, + Deletes: []string{}, + } + for _, nts := range metricBuilder.TimeSeriesFromEvent(event) { + for _, label := range nts.TS.Labels { + if label.Name == labels.MetricName && label.Value != nts.Name { + t.Errorf("__name__ label wrong, expected '%s', got '%s'", nts.Name, label.Value) + } + } + } +} + func TestMetricName(t *testing.T) { for name, tc := range metricNameSet { t.Run(name, func(t *testing.T) {