Skip to content

Commit

Permalink
Added config to honor labels in mappings
Browse files Browse the repository at this point in the history
Signed-off-by: Sebastian Rabenhorst <sebastian.rabenhorst@shopify.com>
  • Loading branch information
rabenhorst committed Nov 2, 2023
1 parent 56fe4f5 commit 4392bea
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
4 changes: 4 additions & 0 deletions pkg/exporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ func (b *Exporter) handleEvent(thisEvent event.Event) {
}
metricName = mapper.EscapeMetricName(mapping.Name)
for label, value := range labels {
if _, ok := prometheusLabels[label]; mapping.HonorLabels && ok {
continue
}

prometheusLabels[label] = value
}
b.EventsActions.WithLabelValues(string(mapping.Action)).Inc()
Expand Down
52 changes: 51 additions & 1 deletion pkg/exporter/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ mappings:
// TestLabelParsing verifies that labels getting parsed out of metric
// names are being properly created.
func TestLabelParsing(t *testing.T) {
codes := [2]string{"200", "300"}
codes := [3]string{"200", "300", "400"}

events := make(chan event.Events)
go func() {
Expand All @@ -292,6 +292,11 @@ func TestLabelParsing(t *testing.T) {
CValue: 1,
CLabels: make(map[string]string),
},
&event.CounterEvent{
CMetricName: "counter.test.400",
CValue: 1,
CLabels: map[string]string{"code": "should be overwritten"},
},
}
events <- c
close(events)
Expand Down Expand Up @@ -329,6 +334,51 @@ mappings:
}
}

func TestHonorLabels(t *testing.T) {
metricName := "some_counter"
events := make(chan event.Events)
go func() {
c := event.Events{
&event.CounterEvent{
CMetricName: metricName,
CValue: 1,
CLabels: map[string]string{"some_label": "bar"},
},
}
events <- c
close(events)
}()

config := `
mappings:
- match: .*
match_type: regex
name: $0
labels:
some_label: foo
honor_labels: true
`
testMapper := &mapper.MetricMapper{
Logger: log.NewNopLogger(),
}
err := testMapper.InitFromYAMLString(config)
if err != nil {
t.Fatalf("Config load error: %s %s", config, err)
}

ex := NewExporter(prometheus.DefaultRegisterer, testMapper, log.NewNopLogger(), eventsActions, eventsUnmapped, errorEventStats, eventStats, conflictingEventStats, metricsCount)
ex.Listen(events)

metrics, err := prometheus.DefaultGatherer.Gather()
if err != nil {
t.Fatalf("Cannot gather from DefaultGatherer: %v", err)
}

if getFloat64(metrics, metricName, map[string]string{"some_label": "bar"}) == nil {
t.Fatalf("Could not find metrics for %s with label set", metricName)
}
}

// TestConflictingMetrics validates that the exporter will not register metrics
// of different types that have overlapping names.
func TestConflictingMetrics(t *testing.T) {
Expand Down
2 changes: 2 additions & 0 deletions pkg/mapper/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type MetricMapping struct {
nameFormatter *fsm.TemplateFormatter
regex *regexp.Regexp
Labels prometheus.Labels `yaml:"labels"`
HonorLabels bool `yaml:"honor_labels"`
labelKeys []string
labelFormatters []*fsm.TemplateFormatter
ObserverType ObserverType `yaml:"observer_type"`
Expand Down Expand Up @@ -57,6 +58,7 @@ func (m *MetricMapping) UnmarshalYAML(unmarshal func(interface{}) error) error {
m.Match = tmp.Match
m.Name = tmp.Name
m.Labels = tmp.Labels
m.HonorLabels = tmp.HonorLabels
m.ObserverType = tmp.ObserverType
m.LegacyBuckets = tmp.LegacyBuckets
m.LegacyQuantiles = tmp.LegacyQuantiles
Expand Down

0 comments on commit 4392bea

Please sign in to comment.