fix(storage): make span ingest idempotent for DLQ replay#62
Merged
Conversation
Adds composite uniqueIndex idx_spans_tenant_trace_span on (tenant_id, trace_id, span_id) so a duplicate span ingest — most commonly a DLQ replay after a partial-batch failure — collapses to a no-op rather than producing double-counted spans in the relational DB and downstream GraphRAG. Changes: - Span model: composite uniqueIndex on (tenant_id, trace_id, span_id) - BatchCreateSpans + BatchCreateAll spans path: OnConflict.DoNothing (MySQL takes INSERT IGNORE) — mirrors existing trace idempotency - New migrate_spans.go: dedupes pre-existing duplicate spans BEFORE AutoMigrate adds the unique index, so upgrades from pre-RAN-65 deployments don't fail on legacy duplicates - Test helper: distinct SpanIDs per seeded span so tests still create the expected count - 6 new tests covering: duplicate-insert no-op, cross-tenant key isolation, BatchCreateAll replay idempotency, dedupe migration on pre-existing dupes, no-op on fresh DB, no-op once unique index exists, AutoMigrate creates the unique index Logs remain non-idempotent (OTLP logs lack a stable identifier); called out explicitly in BatchCreateAll's doc comment as separate future work. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
|
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
Closes the deferred P0 from the robustness brainstorm: span ingest is now idempotent on the (tenant_id, trace_id, span_id) key, so DLQ replay after a partial-batch failure no longer double-counts spans in the relational DB or downstream GraphRAG.
Logs remain non-idempotent — OTLP logs lack a stable identifier and the natural composite key would be expensive without a clear win. Called out explicitly in `BatchCreateAll`'s doc comment as separate future work.
Test plan
Out of scope
🤖 Generated with Claude Code