Skip to content

fix(api): deterministic correlationId fallback in audit factory#86

Open
christopherhouse wants to merge 1 commit into
mainfrom
fix/audit-correlation-id-fallback
Open

fix(api): deterministic correlationId fallback in audit factory#86
christopherhouse wants to merge 1 commit into
mainfrom
fix/audit-correlation-id-fallback

Conversation

@christopherhouse

Copy link
Copy Markdown
Contributor

Summary

EndToEndScenariosTests.QuickstartScenarios_AllThreeUserStories_PassInOneRun has been intermittently failing on:

Expected item.GetProperty(\"correlationId\").GetString() not to be <null> or empty, but found \"\".

Tripped CI on this branch (after #85 merge), and previously on PR #80. Genuine flaky test surfacing a real defect, not a test infrastructure problem.

Root cause

RegistryAuditFactory.cs:38 was:

var correlationId = Activity.Current?.TraceId.ToHexString() ?? string.Empty;

In production with the AspNetCore OTel instrumentation auto-wired by UseAzureMonitor (only kicks in when APPLICATIONINSIGHTS_CONNECTION_STRING is set), the ambient Activity always carries a real trace id. In tests there's no connection string, so the auto-instrumentation never wires up and Activity.Current can race to null in WebApplicationFactory request flows. Same race would hit any background-job path that audits without an inbound HTTP request — and silently break trace linkage in production for those events.

Fix

var correlationId = Activity.Current?.TraceId.ToHexString()
    ?? Guid.NewGuid().ToString(\"N\");

Every audit event now has a non-empty correlationId. When an Activity IS present, real cross-system trace linkage continues to work unchanged.

Test plan

  • Targeted test passes locally
  • Full non-integration suite: 524/524 green
  • No more flaky failures of QuickstartScenarios_AllThreeUserStories_PassInOneRun on future CI runs

Risk

None — additive fallback, semantics improve (no more empty correlationId), no schema change.

🤖 Generated with Claude Code

Spec 006 / FR-042 — every audit event needs a non-empty correlationId
to link back to OTel traces in App Insights. The factory was reading
Activity.Current?.TraceId.ToHexString() with a string.Empty fallback.

In production with the AspNetCore OTel instrumentation active (auto-
wired by UseAzureMonitor when APPLICATIONINSIGHTS_CONNECTION_STRING is
set), there is always an ambient Activity carrying a real trace id, so
this code path worked. In test mode (no AI connection string → no auto-
instrumentation) Activity.Current can race to null, leaving audit
events with correlationId = '' and tripping the
EndToEndScenariosTests.QuickstartScenarios_AllThreeUserStories_PassInOneRun
test's NotBeNullOrEmpty assertion every few CI runs. The same race
would emit empty correlationIds from any background-job path that
audits without an inbound HTTP request, breaking trace linkage in
production for those events too.

Fix: fall back to Guid.NewGuid().ToString('N') instead of string.Empty
when no ambient Activity is present. Audit events always have a
non-empty correlationId; real cross-system trace linkage continues to
work when an Activity IS present.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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.

1 participant