Skip to content

Commit

Permalink
Merge pull request #277 from alibresco/main
Browse files Browse the repository at this point in the history
Fix bug corrupting Prometheus timeseries __name__ label
  • Loading branch information
karimra authored Nov 6, 2023
2 parents f6273a8 + d3b9766 commit f4c7436
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
13 changes: 8 additions & 5 deletions outputs/prometheus_output/prometheus_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
28 changes: 28 additions & 0 deletions outputs/prometheus_output/prometheus_common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit f4c7436

Please sign in to comment.