fix(mobile): explicitly inject traceparent for mobile↔API correlation#173
Merged
Conversation
…tion PR #172 got mobile HttpClient dependency spans emitting with operation_Id, but the correlation join against API requests still returned zero rows: the API saw every incoming request without a traceparent header and started a fresh operation_Id. Root cause: HttpClient's built-in DiagnosticsHandler only injects traceparent automatically when an OTel-style ActivityListener is attached to "System.Net.Http". On MAUI the listener never attaches because OpenTelemetry's TelemetryHostedService — which wires listeners to the TracerProvider — relies on IHostedService, and MauiApp doesn't run hosted services (issue #171). Fix: have ApiActivityHandler explicitly call DistributedContextPropagator.Current.Inject(...) on the outbound request headers after starting its Activity. Guards against double-injection if a caller or a resilience retry already set traceparent. This is the user-space workaround to #171. Framework fix is still desirable but now lower priority. Verification plan: re-run the App Insights correlation join; expect requests | join dependencies on operation_Id to return > 0 rows for the mobile role name. Refs: #165 #166 #172 #171
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
Explicitly inject W3C
traceparentinto outbound HttpClient requests from mobile so the API can correlate its spans against ours.Why
PR #172 added
ApiActivityHandlerwhich creates an Activity per request (so App Insights dependencies now carryoperation_Id). But the correlation join still returned zero rows:Evidence after #172:
Diagnosis:
HttpClient's built-inDiagnosticsHandleronly injectstraceparentautomatically when anActivityListeneris attached to"System.Net.Http". On MAUI that listener never attaches because OpenTelemetry'sTelemetryHostedService— which materializes theTracerProviderand its listeners — depends onIHostedService, andMauiAppdoesn't run hosted services. Tracked in #171.What changed
In
ApiActivityHandler.SendAsync, after the activity null-check and tag-setting, callDistributedContextPropagator.Current.Inject(...)on the request headers. Guarded by!request.Headers.Contains("traceparent")so deliberate upstream callers and resilience retries don't produce duplicate headers.Why this is the right scope
DistributedContextPropagator.Current(W3C by default in .NET 8+), not a captured instance — safe if runtime defaults change.Verification
Refs: #165 #166 #172 #171