feat(mapper): reject configs with unknown fields#687
Open
SAY-5 wants to merge 1 commit intoprometheus:masterfrom
Open
feat(mapper): reject configs with unknown fields#687SAY-5 wants to merge 1 commit intoprometheus:masterfrom
SAY-5 wants to merge 1 commit intoprometheus:masterfrom
Conversation
InitFromYAMLString now decodes the mapping file with yaml.UnmarshalStrict, so keys the MetricMapper struct doesn't expose (e.g. a misspelled `nmae:` instead of `name:`) return an error instead of being silently dropped -- the failure mode called out in prometheus#546 and issue prometheus#544 before it. This is a user-facing breaking change in the narrow sense: configs that were silently doing nothing now fail loudly. Release notes should call it out. Adds TestStrictConfigUnknownField that exercises the rejection path on a mapping entry with a misspelled `nmae:` key. Closes prometheus#546. Signed-off-by: SAY-5 <SAY-5@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #546.
Change
`MetricMapper.InitFromYAMLString` now parses the mapping config with `yaml.UnmarshalStrict` (already provided by `go.yaml.in/yaml/v2`). Any key that the target struct tags don't expose — a misspelled `nmae:` in place of `name:`, a stale field from an older version, etc. — now fails to load with an error that names the unknown field, rather than being silently dropped.
This is exactly the failure mode the issue calls out (and the one that produced the confusion in #544 before it): the old permissive behavior made misconfigurations impossible to notice until the downstream mapping just didn't fire.
Breaking change disclaimer
As the issue notes, this is "potentially-breaking in the sense that your configuration no longer silently does something you didn't expect". Any config file that was relying on unknown keys being ignored will start returning an error on reload. That's intentional — the whole point is to surface those. Release notes should highlight it, and my PR description here is the natural anchor for that note.
Tests
Added `TestStrictConfigUnknownField` in `pkg/mapper/mapper_test.go` that feeds a mapping entry with a misspelled `nmae:` key and asserts that `InitFromYAMLString` returns an error mentioning the unknown field:
```
$ go test -count=1 -run TestStrictConfigUnknownField -v ./pkg/mapper/...
=== RUN TestStrictConfigUnknownField
--- PASS: TestStrictConfigUnknownField (0.00s)
PASS
```
Full suite (`go test -count=1 ./...`) passes.
Signed-off-by: SAY-5 SAY-5@users.noreply.github.com