Skip to content

Add end-to-end MSBuild telemetry coverage and fix spans - #55752

Merged
JanProvaznik merged 5 commits into
mainfrom
copilot/55749-telemetry-e2e
Aug 13, 2026
Merged

JanProvaznik merged 5 commits into
mainfrom
copilot/55749-telemetry-e2e

Conversation

@JanProvaznik

@JanProvaznik JanProvaznik commented Aug 12, 2026 •

Copy link
Copy Markdown
Member

Summary

This PR adds end-to-end telemetry tests that use Aspire Dashboard as the OTLP collector. It also fixes a lifecycle bug that the tests found in the current .NET 11 SDK.

The test installs Aspire CLI 13.4.6 from the feeds in NuGet.config, starts the dashboard on free loopback ports, runs the SDK under test, and reads the exported events from /api/telemetry/spans.

Why the current SDK loses server build telemetry

MSBuildLogger starts an activity when a build starts. The current code stops that activity when MSBuild raises BuildFinished.

MSBuild sends the final msbuild/build telemetry event after BuildFinished. At that point the server process has no current activity. TelemetryClient uses Activity.Current?.AddEvent(...), so it silently drops the event.

An in-process build does not show the same failure because stopping the MSBuild activity restores the surrounding CLI activity. The late event can attach to that activity.

Fix

Keep the MSBuild activity open until logger Shutdown, after MSBuild has sent the final event.

For a persistent MSBuild server, force-flush telemetry after each build but keep the process-wide provider alive. Shut the provider down only when the server process exits. This allows later builds in the same server process to export telemetry.

End-to-end validation

I verified the problem independently with the current daily SDK 11.0.100-rc.1.26411.119 (7cdb217445) and Aspire Dashboard 13.4.6.

Host dotnet/cli/msbuild/build CLI control events
Cold default MSBuild server 0 Present
Hot default MSBuild server 0 Present
In process (MSBUILDUSESERVER=0) 1 Present

All builds succeeded. The CLI control events reached the same dashboard, so the exporter and connection were working. Only the server-hosted MSBuild telemetry was missing.

The new TelemetryCollectorFixture performs the same path in tests:

  1. Install the pinned Aspire CLI from the repository feeds.
  2. Start Aspire Dashboard on free ports.
  3. Run a real build with the repository SDK.
  4. Export OTLP HTTP/protobuf to Aspire.
  5. Query the dashboard API and assert the received event names and tags.

The fixture matrix passes with the fix for:

  • cold default MSBuild server;
  • hot default MSBuild server;
  • in-process build;
  • server fallback;
  • telemetry opt-out.

I also tested the regression directly. I temporarily restored the old behavior that stops the activity at BuildFinished, rebuilt and deployed dotnet.dll, and reran the fixture matrix. Only the cold and hot server rows failed. Their CLI control events were still present. After restoring this fix, all five rows passed again.

Part of #55749. This PR covers items 1 and 2. The item 3 contract snapshot and contributor gate remain a separate follow-up, as allowed by the issue.

Exercise real OTLP export across server, in-process, fallback, and opt-out hosts. Keep MSBuild activities alive through final telemetry, preserve providers across persistent server builds, and create fallback activities for standalone events.

Co-authored-by: Copilot <[email protected]>

Copilot-Session: 46416155-3020-40a9-b340-40f426b38bc3
Copilot AI lite review requested due to automatic review settings August 12, 2026 17:05
@JanProvaznik
JanProvaznik requested review from a team, lbussell and mthalman as code owners August 12, 2026 17:05
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 2 pipeline(s).
1 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Adds an end-to-end OTLP (HTTP/protobuf) telemetry test harness and adjusts MSBuild logger / telemetry plumbing so MSBuild-server builds reliably export the final build event. This strengthens telemetry correctness across the SDK’s different MSBuild hosting modes (in-proc, server cold/hot, fallback, opt-out).

Changes:

  • Introduces a hermetic OTLP loopback receiver plus a host-matrix test to validate real export for MSBuild build telemetry.
  • Keeps the MSBuild build activity open until MSBuildLogger.Shutdown() and force-flushes providers per logger instance (without shutting down providers in a persistent server).
  • Ensures TelemetryClient can create a fallback activity when tracking events without an ambient Activity.

Reviewed changes

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

Show a summary per file
File Description
test/dotnet.Tests/TelemetryTests/TelemetryHostMatrixTests.cs Adds end-to-end matrix coverage for telemetry export across MSBuild host modes (cold/hot server, in-proc, fallback, opt-out).
test/dotnet.Tests/TelemetryTests/TelemetryCollector.cs Implements an in-proc OTLP HTTP/protobuf receiver and minimal OTLP span-event parser for tests.
test/dotnet.Tests/TelemetryTests/TelemetryClientTests.cs Adds unit coverage for “no ambient activity” event tracking behavior.
test/dotnet.Tests/CommandTests/MSBuild/GivenMSBuildLogger.cs Updates logger tests to assert activity lifetime extends through Shutdown().
src/Cli/dotnet/Telemetry/TelemetryClient.cs Adds provider force-flush + process-exit shutdown registration and creates a fallback activity when needed.
src/Cli/dotnet/Commands/MSBuild/MSBuildLogger.cs Defers activity completion until logger shutdown and switches to force-flush for persistent-server safety.
src/Cli/AGENTS.md Documents the updated MSBuild logger activity lifecycle guidance.
documentation/project-docs/telemetry.md Updates telemetry docs to reflect the new lifecycle and host-matrix E2E tests.

Remove ambient server overrides for default-host rows and tolerate listener disposal races during collector shutdown.

Co-authored-by: Copilot <[email protected]>

Copilot-Session: 46416155-3020-40a9-b340-40f426b38bc3
@JanProvaznik

Copy link
Copy Markdown
Member Author

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
1 pipeline(s) were filtered out due to trigger conditions.

@JanProvaznik
JanProvaznik marked this pull request as draft August 13, 2026 07:49
Replace the custom OTLP receiver with the pinned Aspire dashboard and query its telemetry API. Keep the host matrix sensitive to the original MSBuild activity regression by leaving standalone fallback behavior out of this PR.

Co-authored-by: Copilot <[email protected]>

Copilot-Session: 46416155-3020-40a9-b340-40f426b38bc3
Keep the runtime-acquired Aspire CLI dependency visible in test project configuration and pass it to the fixture through assembly metadata.

Co-authored-by: Copilot <[email protected]>

Copilot-Session: 46416155-3020-40a9-b340-40f426b38bc3
Declare the Aspire CLI pin with the repository's other test dependency versions and keep the test project responsible only for exposing it to the fixture.

Co-authored-by: Copilot <[email protected]>

Copilot-Session: 46416155-3020-40a9-b340-40f426b38bc3
@JanProvaznik
JanProvaznik marked this pull request as ready for review August 13, 2026 11:32
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 2 pipeline(s).
1 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@JanProvaznik
JanProvaznik requested a review from baronfel August 13, 2026 11:37
@JanProvaznik JanProvaznik changed the title Add end-to-end MSBuild telemetry coverage Add end-to-end MSBuild telemetry coverage and fix spans Aug 13, 2026
@JanProvaznik
JanProvaznik marked this pull request as draft August 13, 2026 12:56

namespace Microsoft.DotNet.Tests.TelemetryTests;

[TestClass]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@Evangelink I'd like to come back to these tests after getting guidance/docs from you and see if we can 'manage' an aspire CLI otel instance as a shared resource with nice setup/teardown hooks.

@baronfel
baronfel marked this pull request as ready for review August 13, 2026 14:03
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 2 pipeline(s).
1 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@JanProvaznik
JanProvaznik enabled auto-merge (squash) August 13, 2026 14:43
@JanProvaznik
JanProvaznik merged commit a753c74 into main Aug 13, 2026
27 checks passed
@JanProvaznik
JanProvaznik deleted the copilot/55749-telemetry-e2e branch August 13, 2026 15:45
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-rc1 milestone Aug 15, 2026
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.

3 participants