Fix ID-targeted state load dropping resources with regex-escaped filter values - #660
Open
michael-richey wants to merge 4 commits into
Open
Fix ID-targeted state load dropping resources with regex-escaped filter values#660michael-richey wants to merge 4 commits into
michael-richey wants to merge 4 commits into
Conversation
ExactMatch filter values are regex-escaped by callers (e.g. via regexp.QuoteMeta or re.escape) so that metacharacters match literally once the value is wrapped in ^...$ and compiled. The --minimize-reads ID-targeted state load then reused the pattern body as a literal storage key without reversing that escaping, so an id such as "svc.request.count" was looked up as "svc\.request\.count" and never matched its stored blob. Those resources were silently dropped from state before diff/apply — no create, no error, no filtered count. Un-escape the ExactMatch body back to the literal id in _unwrap_exact_match_pattern. When the body is not a pure literal (an unescaped metacharacter, i.e. a genuine regex, or a dangling backslash) raise ValueError so extract_exact_id_filters falls back to type-scoped loading, which matches via the compiled regex and stays correct. Add red/green regression tests covering the unwrap helper and an end-to-end escaped-value load through State.get_by_ids. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a --minimize-reads ID-targeted state loading bug where ExactMatch filters containing regex-escaped IDs (e.g., dotted metric IDs) could be treated as literal storage keys and silently fail to load matching resources.
Changes:
- Add
_regex_literal_from_exact_match_bodyand update_unwrap_exact_match_patternto unescape regex-escaped ExactMatch bodies back to literal IDs, with a conservativeValueErrorfallback for non-literal patterns. - Add unit tests covering escaped dots, escaped backslashes, semantic escapes (e.g.
\d), and an end-to-endState.get_by_idsresolution case.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| tests/unit/test_minimize_reads_id_targeted.py | Adds regression/unit tests for escaped ExactMatch ID handling and ID-targeted state load behavior. |
| datadog_sync/utils/configuration.py | Implements safe unescaping of ExactMatch regex bodies to recover literal IDs for ID-targeted loading. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
A prior autofix commit rewrote the comment above this constant but dropped the actual assignment, leaving the name referenced but undefined and breaking ruff (F821).
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.
Summary
Resources with regex-metacharacters in their ids (e.g. dotted log-metric names) could be silently dropped during a
syncrun when both--minimize-readsandOperator=ExactMatchName=idfilters are in use.ExactMatchfilter values are regex-escaped by callers (Goregexp.QuoteMeta, Pythonre.escape, etc.) so they match literally once wrapped in^...$and compiled — this is required, otherwisesvc.request.countwould also matchsvcXrequestYcount. The--minimize-readsID-targeted fast path (extract_exact_id_filters→_unwrap_exact_match_pattern) then reused the escaped pattern body as a literal storage key:logs_metrics.svc.request.count.json, keyed inside bysvc.request.countsvc\.request\.count← never matchesState.get_by_ids/get_singlereturnsNonefor the mismatch and skips it, so the resource never entersstate.source, never reaches the dependency graph, and produces no create, no error, and nofilteredoutcome — it just disappears. Ids with no metacharacters were unaffected, which is why the failure was partial and easy to miss.Fix
_unwrap_exact_match_patternnow un-escapes the^...$body back to the literal id via a new_regex_literal_from_exact_match_bodyhelper.\d, or a dangling backslash — it raisesValueError.extract_exact_id_filtersalready treats that as "not ID-targetable" and falls back to type-scoped loading, which matches via the compiled regex and stays correct.Testing (red/green)
Added
TestExactMatchEscapedValuesintests/unit/test_minimize_reads_id_targeted.py:\dtrigger type-scoped fallback instead of targeting the wrong IDState.get_by_idsresolves an escaped-value ID against the real blob keyThe escaped-ID regression tests fail on
mainand pass with the fix. The semantic-escape regression fails on the initial PR head by extracting literal IDdfrom a pattern that matches digits, then passes after the conservative fallback change.Focused suite:
28 passed.Unit suite excluding the pre-existing
@pytest.mark.experiment_subprocesstests:1100 passed, 8 skipped, 10 deselected.