Skip to content

feat: Add optional W3C traceparent correlation context support#289

Merged
BenjaminMichaelis merged 3 commits into
mainfrom
feat/app-insights-correlation-context
May 16, 2026
Merged

feat: Add optional W3C traceparent correlation context support#289
BenjaminMichaelis merged 3 commits into
mainfrom
feat/app-insights-correlation-context

Conversation

@BenjaminMichaelis
Copy link
Copy Markdown
Member

@BenjaminMichaelis BenjaminMichaelis commented May 16, 2026

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 correlationContext is not provided, behavior is identical to before.

Changes

microsoft-trydotnet SDK (public API)

  • configuration.ts: added correlationContext?: string to the Configuration type
  • urlHelpers.ts: threads correlationContext through session URL construction

microsoft-trydotnet-editor (internal)

  • factory.ts: reads correlationContext from session config; passes to apiService
  • apiService.ts: generates a W3C traceparent header per-request when correlationContext is present
    • normalizeTraceId(): accepts both a full traceparent string and a raw 32-hex traceId
    • createSpanId(): fresh span ID per request via crypto.getRandomValues()
    • Header format: 00-{32hex traceId}-{16hex spanId}-01
  • apiService.tests.ts (new): 2 regression tests — traceparent present with context, absent without

Backend (ASP.NET Core)

  • ContentGenerator.cs: reads correlationContext from query/config; passes into session

How it flows

EssentialCSharp.Web (App Insights JS SDK)
  └─ traceId from appInsights.context.telemetryTrace.traceID
     └─ passed as configuration.correlationContext to TryDotNet SDK
        └─ apiService.ts injects traceparent header on every backend call
           └─ ASP.NET Core OTel picks up automatically → child spans appear in same trace

Backend OTel requirement

The TryDotNet backend should have AddAspNetCoreInstrumentation() registered (picks up traceparent automatically) and ideally share the same APPLICATIONINSIGHTS_CONNECTION_STRING as EssentialCSharp.Web for E2E trace visualization.

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
Copilot AI review requested due to automatic review settings May 16, 2026 17:55
@BenjaminMichaelis BenjaminMichaelis temporarily deployed to BuildAndUploadImage May 16, 2026 17:55 — with GitHub Actions Inactive
Comment thread src/microsoft-trydotnet/test/session.creationapi.specs.ts Fixed
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 correlationContext to the public Configuration (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 fresh traceparent header per request with a new random span id.
  • Backend ContentGenerator reads correlationContext from 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.

@BenjaminMichaelis BenjaminMichaelis temporarily deployed to BuildAndUploadImage May 16, 2026 22:02 — with GitHub Actions Inactive
…NTS.md

New files added to this fork should not carry the .NET Foundation header.
Existing files are left unchanged.
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 8 out of 8 changed files in this pull request and generated no new comments.

@BenjaminMichaelis BenjaminMichaelis merged commit a6d389a into main May 16, 2026
15 checks passed
@BenjaminMichaelis BenjaminMichaelis deleted the feat/app-insights-correlation-context branch May 16, 2026 22:45
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.

2 participants