fix(spans_metrics): fall back to update on 409 during create - #650
Merged
Conversation
The `spans_metrics` model has `skip_resource_mapping=True`, so `state.destination` is never populated from a live LIST. First-run against a destination that already has a same-named metric (customer-created before sync-cli started managing the org) POSTs, hits HTTP 409 "metric already exists with that name", and loops forever — state stays empty, next dispatch retries the CREATE, same 409. The v2 API keys spans_metrics by name (path param `metric_id` == metric name), so a 409 on POST means the same `_id` is on the destination. On 409, GET the existing record by id, hydrate `state.destination`, and retry as `update_resource` (PATCH). Non-409 errors propagate unchanged. Unit tests cover: happy-path POST, 409 → GET → PATCH fallback, and non-409 reraise.
michael-richey
approved these changes
Jul 29, 2026
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
spans_metricssetsskip_resource_mapping=True, sostate.destination["spans_metrics"]is never populated from a live LIST. First-run against a destination that already has a same-named metric (customer-created before sync-cli started managing the org) POSTs, gets HTTP 409 "metric already exists with that name", and loops forever — state stays empty, next dispatch retries the CREATE, same 409.The v2 API keys spans_metrics by name (URL path param
metric_id== metric name), so a 409 on POST means the same_idis on the destination. This PR makescreate_resourcecatch 409, GET the existing record by id, hydratestate.destination, and delegate toupdate_resource(PATCH). Non-409 errors propagate unchanged.Motivation
Observed in production against a real customer org where 18/18 spans_metrics failed every sync run with HTTP 409. The destination org had all 18 metric names pre-created by the customer (
managed=false); sync-cli had no way to converge because it never saw them.The same class of defect affects any resource type that combines:
skip_resource_mapping=True(no live LIST populating state)create_resourcedoing a bare POST with no 409 fallbackI only fix
spans_metricshere; other candidates (metric_percentiles,metrics_metadata, name-keyed order/config types) should be evaluated individually.Test plan
pytest tests/unit/test_spans_metrics_409_fallback.py -v→ 3 passedtest_create_resource_happy_path— POST succeeds, no fallback triggeredtest_create_resource_409_falls_back_to_get_then_patch— POST 409 → GET-by-id →state.destinationhydrated → PATCH called with id-scoped URLtest_create_resource_non_409_reraises— 500 propagates unchanged, no GET/PATCHspans_metricsbehavior unchanged on the happy path (same return shape, same POST call)Files changed
datadog_sync/model/spans_metrics.py— wrapcreate_resourcePOST in try/except; on 409, GET-by-id, hydrate state, delegate to update_resourcetests/unit/test_spans_metrics_409_fallback.py— new unit tests