← Back to Pull requests

[Backport v1.32] [CONTP-2223] Skip monitor force-sync updates while the remote definition is unchanged #3762

Closed
unknown closed this pull request without merging on Nov 11, 2026 16:06 UTC
View on GitHub
unknown unknown commented on Nov 10, 2026 20:28 UTC
Author

Backport 5b4a186c9a1d7e34225f95ea317c34d03a9d95f1 from #3743.

___

What does this PR do?

Skips the periodic force-sync update of a monitor when nobody changed it in Datadog since the Operator last wrote it. Applies to DatadogMonitor and DatadogGenericResource (monitor).

  • After each successful create or update, the controller stores a hash of the monitor definition returned by Datadog in a new status field, status.remoteDefinitionHash.

  • Hashed fields: name, type, query, message, tags, options, priority, restricted_roles, draft_status, plus assets for DDGR. State, timestamps and other server-managed fields are excluded. The set-like lists tags, restricted_roles, options.notify_by and options.renotify_statuses are sorted before hashing.

  • A force sync hashes the monitor it already fetches. If the hash matches, the validate and update calls are skipped and lastForceSyncTime advances. Otherwise the existing update path runs and stores the new hash.

  • Spec changes are still detected by status.currentHash. The stored hash is cleared before every update attempt, so a failed update never leaves a hash that would skip the retry.

  • DDGR: only the monitor handler opts in, through an optional remoteDefinitionHasher interface. Its force-sync GET adds with_assets=true. It also hashes every top-level jsonSpec key. If one of them is missing from the response, no hash is stored and the monitor updates on every force sync. Specs that set restriction_policy keep updating on every force sync, since the policy is not part of the monitor definition. Other resource types are unchanged.
  • Motivation

    CONTP-2223: unchanged monitors receive a GET + validate + PUT every force sync (hourly by default).

    | Controller | Unchanged monitor, before | After |
    | --- | --- | --- |
    | DatadogMonitor | 1 GET + 1 validate POST + 1 PUT | 1 GET |
    | DDGR monitor | 1 GET + 1 PUT | 1 GET |

    Additional Notes

    This is an alternative to the force-sync part of #3667 and to #3731, which compare the spec's fields with the remote monitor instead of tracking whether the remote monitor changed. Main differences:

    | | #3667 / #3731 (field comparison) | This PR (remote hash) |
    | --- | --- | --- |
    | Question asked | Do the fields set in the spec match the remote monitor? | Did the remote monitor change since the Operator last wrote it? |
    | Out-of-band edits to fields the spec does not set (e.g. options sub-fields) | Not reverted unless another field drifts | Reverted, as today |
    | New fields or defaults returned by Datadog | Ignored | One update per monitor, then a new hash |
    | A field that differs between write responses and reads | n/a | Updates on every force sync (no drift, only lost savings) |
    | First force sync after upgrade | Skipped if matching | One update, then skipped |
    | State and API changes | None (#3667); int64 priority and omitzero set fields (#3731) | Additive status field |
    | DDGR restriction_policy | Compared (#3731) | Always updates |

    Behavior: what a force sync reverts is unchanged. options is replaced as a whole by an update, so edits to its sub-fields are reverted as before. Top-level fields the update does not send are kept as before; editing one costs one update, after which it becomes part of the stored hash.

    Found during live testing:

  • Update responses include monitor_id in each asset while reads do not. It is excluded from the hash.

  • Create responses return restricted_roles in the order sent, while reads return them in another order. Sorting the set-like lists keeps the hash recorded at creation equal to the next read. tags, notify_by and renotify_statuses kept the sent order in all responses and reads, and are sorted for consistency.
  • DatadogMonitor create requests now include spec.restrictedRoles. Previously they were omitted and applied by the first force-sync update about a minute after creation; with that update skipped they would never be applied. An empty list is not sent, since the API rejects it.

    For DDGR, response-only fields left in jsonSpec (such as overall_state or modified) change the hash without an edit and cause updates, at most once per force sync as today; the docs ask to keep them out of jsonSpec. Numbers in fields the SDK does not know are hashed exactly rather than rounded to float64.

    The new status field is optional. Existing resources need no migration; each gets one update on its first force sync after upgrade.

    Minimum Agent Versions

    Not applicable; this only changes the Operator's Datadog API usage.

    Agent: N/A

  • Cluster Agent: N/A
  • Describe your test plan

    Unit tests:

  • MonitorDefinitionHash: state, modified and the order of tags, restricted roles, notify_by and renotify_statuses do not change the hash; message, options sub-fields, priority, restricted roles, draft status, tags and assets do; monitor_id in assets is ignored.

  • DatadogMonitor and DDGR reconcile tests against a fake API that merges updates per top-level field like the real one: an unchanged monitor makes no validate/update call and advances the force-sync time; edits to fields set and not set by the spec are reverted with one update, then skipped again; a missing hash updates once; a failed update clears the hash; a DDGR spec with restriction_policy always updates. Forcing the skip condition to false makes these tests fail.

  • DDGR: drift in a jsonSpec field unknown to the SDK is repaired with one update; a jsonSpec field missing from the response disables the skip; large integers keep distinct hashes.

  • DatadogMonitor guard tests: the hashed field list must equal the fields of the update request buildMonitor builds, and every DatadogMonitorSpec field must map to a hashed field.

  • go test -race ./internal/controller/... ./pkg/controller/... ./api/... and make lint pass.
  • Live test on kind against the real Datadog API, with 1-minute force syncs, two DatadogMonitors and one DDGR monitor with an asset:

  • Unchanged monitors skipped every force sync; their modified timestamp did not change.

  • Edits in the Datadog UI (an option the spec does not set, priority, the DDGR runbook asset) were each reverted with exactly one update at the next force sync, then skipped again.

  • Muting a monitor from the UI (a downtime) caused no update.

  • Restarting the Operator kept skipping; an outdated stored hash cost one update.

  • Deleting a monitor in Datadog recreated it, and the recreated monitor was then skipped.

  • Deleting the CRs removed all test monitors.
  • The kind run covered the changes up to the asset fix. The later changes (sorted set-like lists, restricted roles on create, DDGR jsonSpec fields, guard tests) are covered by unit tests; the first two were also checked with the API directly.

    List ordering, checked with the API directly: four monitors with 30 shuffled tags, 3 restricted roles, 6 notify_by keys and 3 renotify_statuses, each read 12 times over 4 minutes and then updated with every list reshuffled. Only restricted_roles came back in a different order from the create response; reads were consistent with the update response.

    Checklist

  • PR has at least one valid label: bug, enhancement, refactoring, documentation, tooling, and/or dependencies

  • PR has a milestone or the qa/skip-qa label

  • All commits are signed (see: [signing commits][1])
  • [1]: https://docs.github.com/en/authentication/managing-commit-signature-verification/signing-commits

    🤖 Generated with Claude Code

    [CONTP-2223]: https://datadoghq.atlassian.net/browse/CONTP-2223?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: levan-m Review duration: 1178m
    All Vouch review confidence checks passed
    Residual risk 0.21 = Change Risk (0.52) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 650 lines diff across 8 files
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #3762"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52% (MEDIUM) · Higher = larger blast radius
    52%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 20.5% · Safe to merge
    20.5%