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 bug corrupting Prometheus timeseries __name__ label #277

Merged
merged 1 commit into from
Nov 6, 2023
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
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