feat: Add optional W3C traceparent correlation context support#289
Merged
Conversation
Enables opt-in trace correlation from EssentialCSharp.Web to the
TryDotNet backend when embedded in the EC# web app. All changes are
backward-compatible: absent correlationContext means zero behavior
change for any existing Try consumers.
Changes:
- configuration.ts: add optional correlationContext?: string field
to public SDK configuration interface
- urlHelpers.ts: append correlationContext as query param to editor
URL when present
- ContentGenerator.cs: read correlationContext from session config
JSON and pass it through to the editor HTML page
- factory.ts: include correlationContext in API config forwarded to
the editor internals
- apiService.ts: generate a valid W3C traceparent header
(00-{traceId}-{newSpanId}-01) per request when correlationContext
is present; each request gets a fresh span ID
- apiService.tests.ts (new): regression tests verifying traceparent
header is included when context is present and absent otherwise
- session.creationapi.specs.ts: add test verifying correlationContext
is propagated in iframe URL when passed in configuration
There was a problem hiding this comment.
Pull request overview
Adds optional W3C traceparent propagation so browser-side App Insights traces from EssentialCSharp.Web can be correlated with TryDotNet backend spans. The change is strictly additive: when correlationContext is not supplied, behavior is unchanged.
Changes:
- Adds
correlationContextto the publicConfiguration(SDK) and threads it through the editor iframe URL. - In the editor's
apiService, validates the input (accepting a full traceparent or a 32-hex trace id) and emits a freshtraceparentheader per request with a new random span id. - Backend
ContentGeneratorreadscorrelationContextfrom the query string and forwards it into the rendered editor configuration.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| src/Microsoft.TryDotNet/ContentGenerator.cs | Reads correlationContext from query and includes it in editor configuration (HTML-attribute-encoded as before). |
| src/microsoft-trydotnet/src/configuration.ts | Adds optional correlationContext field to public Configuration type. |
| src/microsoft-trydotnet/src/internals/urlHelpers.ts | Appends correlationContext to editor iframe URL query string when set. |
| src/microsoft-trydotnet/test/session.creationapi.specs.ts | Test verifying correlationContext is propagated to iframe URL. |
| src/microsoft-trydotnet-editor/src/factory.ts | Passes correlationContext from configuration into apiService; adds optional field to IConfiguration. |
| src/microsoft-trydotnet-editor/src/apiService.ts | Adds traceparent header per request via normalizeTraceId + random createSpanId; sampled flag hard-coded to 01. |
| src/microsoft-trydotnet-editor/tests/apiService.tests.ts | New tests for traceparent presence/absence based on correlationContext. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…NTS.md New files added to this fork should not carry the .NET Foundation header. Existing files are left unchanged.
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
Adds optional, backward-compatible W3C traceparent correlation context support so that EssentialCSharp.Web's App Insights browser telemetry can flow through to TryDotNet backend spans.
All changes are strictly additive — if
correlationContextis not provided, behavior is identical to before.Changes
microsoft-trydotnetSDK (public API)configuration.ts: addedcorrelationContext?: stringto theConfigurationtypeurlHelpers.ts: threadscorrelationContextthrough session URL constructionmicrosoft-trydotnet-editor(internal)factory.ts: readscorrelationContextfrom session config; passes toapiServiceapiService.ts: generates a W3Ctraceparentheader per-request whencorrelationContextis presentnormalizeTraceId(): accepts both a full traceparent string and a raw 32-hex traceIdcreateSpanId(): fresh span ID per request viacrypto.getRandomValues()00-{32hex traceId}-{16hex spanId}-01apiService.tests.ts(new): 2 regression tests — traceparent present with context, absent withoutBackend (ASP.NET Core)
ContentGenerator.cs: readscorrelationContextfrom query/config; passes into sessionHow it flows
Backend OTel requirement
The TryDotNet backend should have
AddAspNetCoreInstrumentation()registered (picks uptraceparentautomatically) and ideally share the sameAPPLICATIONINSIGHTS_CONNECTION_STRINGas EssentialCSharp.Web for E2E trace visualization.