Skip to content
Open
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
5 changes: 4 additions & 1 deletion pkg/mapper/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ var defaultQuantiles = []MetricObjective{
func (m *MetricMapper) InitFromYAMLString(fileContents string) error {
var n MetricMapper

if err := yaml.Unmarshal([]byte(fileContents), &n); err != nil {
// Use strict decoding so unknown or misspelled keys in a mapping
// config surface as an error instead of being silently dropped
// (see https://github.com/prometheus/statsd_exporter/issues/546).
if err := yaml.UnmarshalStrict([]byte(fileContents), &n); err != nil {
return err
}

Expand Down
26 changes: 26 additions & 0 deletions pkg/mapper/mapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package mapper

import (
"strings"
"testing"
"time"

Expand Down Expand Up @@ -1760,3 +1761,28 @@ mappings:
}
}
}

// TestStrictConfigUnknownField covers the fix for
// https://github.com/prometheus/statsd_exporter/issues/546: mapping
// configs with keys the struct tags don't know about must fail to load
// rather than silently drop the value.
func TestStrictConfigUnknownField(t *testing.T) {
// `nmae` (misspelled `name`) is a field that MetricMapping doesn't
// expose; with permissive YAML it would be dropped, leaving the
// mapping without a metric name and producing a confusing runtime
// error much later (or none at all).
const badConfig = `---
mappings:
- match: aa.bb.*.*
nmae: "aa_bb_${1}_total"
`

mapper := MetricMapper{}
err := mapper.InitFromYAMLString(badConfig)
if err == nil {
t.Fatal("expected InitFromYAMLString to reject unknown field `nmae`, got nil")
}
if !strings.Contains(err.Error(), "not found") && !strings.Contains(err.Error(), "field nmae") {
t.Fatalf("expected error to mention the unknown field, got: %v", err)
}
}