mirror of
https://github.com/prometheus/statsd_exporter.git
synced 2025-01-11 14:55:24 +00:00
Fixes #191: crash on empty metric name
Signed-off-by: Ivan Izaguirre <ivanizag@gmail.com>
This commit is contained in:
parent
1036439f69
commit
1c71191aaf
2 changed files with 38 additions and 1 deletions
|
@ -280,7 +280,7 @@ type Exporter struct {
|
||||||
|
|
||||||
func escapeMetricName(metricName string) string {
|
func escapeMetricName(metricName string) string {
|
||||||
// If a metric starts with a digit, prepend an underscore.
|
// If a metric starts with a digit, prepend an underscore.
|
||||||
if metricName[0] >= '0' && metricName[0] <= '9' {
|
if len(metricName) > 0 && metricName[0] >= '0' && metricName[0] <= '9' {
|
||||||
metricName = "_" + metricName
|
metricName = "_" + metricName
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -333,6 +333,10 @@ func (b *Exporter) handleEvent(event Event) {
|
||||||
metricName := ""
|
metricName := ""
|
||||||
prometheusLabels := event.Labels()
|
prometheusLabels := event.Labels()
|
||||||
if present {
|
if present {
|
||||||
|
if mapping.Name == "" {
|
||||||
|
log.Debugf("The mapping for match \"%v\" generates an empty metric name.", mapping.Match)
|
||||||
|
return
|
||||||
|
}
|
||||||
metricName = escapeMetricName(mapping.Name)
|
metricName = escapeMetricName(mapping.Name)
|
||||||
for label, value := range labels {
|
for label, value := range labels {
|
||||||
prometheusLabels[label] = value
|
prometheusLabels[label] = value
|
||||||
|
|
|
@ -56,6 +56,38 @@ func TestNegativeCounter(t *testing.T) {
|
||||||
ex.Listen(events)
|
ex.Listen(events)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestEmptyStringMetric validates when a metric name ends up
|
||||||
|
// being the empty string after applying the match replacements
|
||||||
|
// tha we don't panic the Exporter Listener.
|
||||||
|
func TestEmptyStringMetric(t *testing.T) {
|
||||||
|
events := make(chan Events)
|
||||||
|
go func() {
|
||||||
|
c := Events{
|
||||||
|
&CounterEvent{
|
||||||
|
metricName: "foo_bar",
|
||||||
|
value: 1,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
events <- c
|
||||||
|
close(events)
|
||||||
|
}()
|
||||||
|
|
||||||
|
config := `
|
||||||
|
mappings:
|
||||||
|
- match: .*_bar
|
||||||
|
match_type: regex
|
||||||
|
name: "${1}"
|
||||||
|
`
|
||||||
|
testMapper := &mapper.MetricMapper{}
|
||||||
|
err := testMapper.InitFromYAMLString(config)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Config load error: %s %s", config, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
ex := NewExporter(testMapper)
|
||||||
|
ex.Listen(events)
|
||||||
|
}
|
||||||
|
|
||||||
// TestInvalidUtf8InDatadogTagValue validates robustness of exporter listener
|
// TestInvalidUtf8InDatadogTagValue validates robustness of exporter listener
|
||||||
// against datadog tags with invalid tag values.
|
// against datadog tags with invalid tag values.
|
||||||
// It sends the same tags first with a valid value, then with an invalid one.
|
// It sends the same tags first with a valid value, then with an invalid one.
|
||||||
|
@ -167,6 +199,7 @@ func TestEscapeMetricName(t *testing.T) {
|
||||||
"with😱emoji": "with_emoji",
|
"with😱emoji": "with_emoji",
|
||||||
"with.*.multiple": "with___multiple",
|
"with.*.multiple": "with___multiple",
|
||||||
"test.web-server.foo.bar": "test_web_server_foo_bar",
|
"test.web-server.foo.bar": "test_web_server_foo_bar",
|
||||||
|
"": "",
|
||||||
}
|
}
|
||||||
|
|
||||||
for in, want := range scenarios {
|
for in, want := range scenarios {
|
||||||
|
|
Loading…
Reference in a new issue