Thanks to visit codestin.com
Credit goes to github.com

Skip to content

rulefmt: error on rule files that contain multiple YAML documents#18182

Open
turanalmammadov wants to merge 1 commit intoprometheus:mainfrom
turanalmammadov:feat/warn-multidoc-rule-files
Open

rulefmt: error on rule files that contain multiple YAML documents#18182
turanalmammadov wants to merge 1 commit intoprometheus:mainfrom
turanalmammadov:feat/warn-multidoc-rule-files

Conversation

@turanalmammadov
Copy link

Closes #15834

Problem

prometheus only loads the first YAML document from a rule file. When a
file contains multiple documents separated by ---, the extra groups are
silently 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 a
second decoder.Decode() on the same streaming decoder. If it succeeds
(i.e., a second document exists), we append an explanatory error:

rule file contains multiple YAML documents separated by '---';
only the first document is loaded. Remove the document separator or
split into separate files.

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

  • Reuses the yaml.Decoder that already exists in Parse — no extra I/O or
    allocations.
  • The second Decode call returns immediately once a second document is found
    (the decoder is streaming and lazy).
  • Error is appended to errs and causes Parse to return nil, errs (same
    as other parse errors), so the caller rejects the file.

Tests

Two new tests in rulefmt_test.go:

Test Validates
TestParseMultipleDocuments Returns an error; message contains "multiple YAML documents"
TestParseSingleDocument No error; first (only) group is correctly parsed

Made with Cursor

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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Warn if multi-documents in a single yaml rule file are encountered

1 participant