rulefmt: error on rule files that contain multiple YAML documents#18182
Open
turanalmammadov wants to merge 1 commit intoprometheus:mainfrom
Open
rulefmt: error on rule files that contain multiple YAML documents#18182turanalmammadov wants to merge 1 commit intoprometheus:mainfrom
turanalmammadov wants to merge 1 commit intoprometheus:mainfrom
Conversation
Prometheus only loads the first YAML document from a rule file. Extra documents separated by '---' are silently ignored, which can confuse operators who split their rule groups with document separators and then wonder why some groups are not active. Add a check in Parse() that uses the same streaming yaml.Decoder to peek for a second document after the first decode. If a second document is present, an error is returned so the file is rejected at load time with a clear message: 'rule file contains multiple YAML documents separated by ---; only the first document is loaded. Remove the document separator or split into separate files.' The fix uses the same decoder that already exists in Parse() so there is no extra I/O; the second Decode() call returns immediately once a second document is found. Tests added: - TestParseMultipleDocuments: verifies the error is returned and the message mentions 'multiple YAML documents'. - TestParseSingleDocument: confirms that a normal single-document file is still parsed without errors. Closes prometheus#15834 Co-authored-by: Cursor <[email protected]>
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 #15834
Problem
prometheusonly loads the first YAML document from a rule file. When afile contains multiple documents separated by
---, the extra groups aresilently ignored. This surprises operators who split rule groups with
document separators, because their groups appear missing without any warning.
Solution
In
model/rulefmt/Parse(), after decoding the first document, attempt asecond
decoder.Decode()on the same streaming decoder. If it succeeds(i.e., a second document exists), we append an explanatory error:
The file is then rejected at load time so the operator knows they have to fix
it, rather than having groups silently not evaluated.
Implementation Details
yaml.Decoderthat already exists inParse— no extra I/O orallocations.
Decodecall returns immediately once a second document is found(the decoder is streaming and lazy).
errsand causesParseto returnnil, errs(sameas other parse errors), so the caller rejects the file.
Tests
Two new tests in
rulefmt_test.go:TestParseMultipleDocumentsTestParseSingleDocumentMade with Cursor