Skip to content

Register Components metrics and tracing in WebAssembly when metrics support is explicitly enabled#64737

Merged
javiercn merged 7 commits intomainfrom
copilot/register-metrics-tracing-default
Jan 21, 2026
Merged

Register Components metrics and tracing in WebAssembly when metrics support is explicitly enabled#64737
javiercn merged 7 commits intomainfrom
copilot/register-metrics-tracing-default

Conversation

Copy link
Contributor

Copilot AI commented Dec 11, 2025

Register Components metrics and tracing in WebAssembly host with opt-in support

  • You've read the Contributor Guide and Code of Conduct.
  • You've included unit or integration tests for your change, where applicable.
  • You've included inline docs for your change, where applicable.
  • There's an open issue for the PR that you are making. If you'd like to propose a new feature or change, please open an issue to discuss the change or find an existing issue.

Enable WebAssembly host to register metrics and tracing services when explicitly enabled via feature switch.

Description

WebAssembly host can now register metrics and tracing services during initialization when the System.Diagnostics.Metrics.Meter.IsSupported feature switch is enabled. Services are disabled by default (opt-in) to keep bundle sizes minimal, and can be enabled by setting the AppContext switch to true.

Changes:

  • Added Microsoft.Extensions.Diagnostics reference to WebAssembly project (brought in transitively to test project)
  • Registered ComponentsMetrics and ComponentsActivitySource in WebAssemblyHostBuilder.InitializeDefaultServices() behind feature switch check
  • Services are disabled by default (opt-in) and can be enabled by setting the System.Diagnostics.Metrics.Meter.IsSupported AppContext switch to true
  • Added test coverage verifying default behavior (telemetry not registered) and enabled behavior (telemetry registered when switch is true)

This allows developers to enable consistent telemetry collection across Blazor hosting modes when needed, while keeping WebAssembly bundle sizes minimal by default through the opt-in feature switch mechanism.

Original prompt

This section details on the original issue you should resolve

<issue_title>[Blazor] Register Components metric and tracing by default on webassembly host.</issue_title>
<issue_description></issue_description>

<agent_instructions>Look at how this is done in the components server render mode host. Make sure that on webassembly we also register the metrics and tracingservices by default</agent_instructions>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: javiercn <6995051+javiercn@users.noreply.github.com>
Copilot AI changed the title [WIP] Register components metric and tracing by default on webassembly host Register Components metrics and tracing by default in WebAssembly host Dec 11, 2025
Copilot AI requested a review from javiercn December 11, 2025 14:50
@javiercn javiercn marked this pull request as ready for review December 11, 2025 15:08
@javiercn javiercn requested a review from a team as a code owner December 11, 2025 15:08
Copilot AI review requested due to automatic review settings December 11, 2025 15:08
Copy link
Contributor

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

This PR aligns WebAssembly host telemetry registration with server-side Blazor by enabling metrics and tracing by default during host initialization. Previously, WebAssembly hosts did not automatically register ComponentsMetrics and ComponentsActivitySource, creating an inconsistency with the server render mode.

Key Changes:

  • Added automatic registration of ComponentsMetrics and ComponentsActivitySource in WebAssemblyHostBuilder.InitializeDefaultServices()
  • Added Microsoft.Extensions.Diagnostics dependency to both source and test projects
  • Added test coverage verifying IMeterFactory registration

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
src/Components/WebAssembly/WebAssembly/src/Microsoft.AspNetCore.Components.WebAssembly.csproj Added Microsoft.Extensions.Diagnostics package reference required for metrics functionality
src/Components/WebAssembly/WebAssembly/src/Hosting/WebAssemblyHostBuilder.cs Registered ComponentsMetrics and ComponentsTracing services in InitializeDefaultServices method
src/Components/WebAssembly/WebAssembly/test/Microsoft.AspNetCore.Components.WebAssembly.Tests.csproj Added Microsoft.Extensions.Diagnostics package reference for test project
src/Components/WebAssembly/WebAssembly/test/Hosting/WebAssemblyHostBuilderTest.cs Added test to verify IMeterFactory registration, confirming metrics services are available

Copy link
Member

Choose a reason for hiding this comment

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

This comes with increased download size. Please quantify the download size difference.
Please also make it opt-in (IL trimable), not always included.
Probably behind existing <MetricsSupport>true</MetricsSupport>.

Copy link
Member

Choose a reason for hiding this comment

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

We have benchmarks that tell us about size regressions. We've never measured the size diff on every change for wasm stuff.

Please also make it opt-in (IL trimable), not always included.

Is there an AppContextSwitch for this?

We will want to make this work by default and be opt-out rather than opt-in since it will otherwise force people to jump through more hoops for something that we want to be easy.

Copy link
Member

@lewing lewing Dec 11, 2025

Choose a reason for hiding this comment

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

System.Diagnostics.Metrics.Meter.IsSupported

Copy link
Member

Choose a reason for hiding this comment

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

@copilot register the services behind System.Diagnostics.Metrics.Meter.IsSupported (don't use the fully qualified name, add the using if needed). Check with the microsoft docs if there is a different check for traces we could use, if there is, use that, if not, Meter.IsSupported is good enough.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Registered metrics and tracing services behind the System.Diagnostics.Metrics.Meter.IsSupported feature switch check using AppContext.TryGetSwitch. Services are now opt-out (enabled by default, can be disabled via the feature switch). Commit bc57528.

Copy link
Member

Choose a reason for hiding this comment

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

We will want to make this work by default and be opt-out rather than opt-in

I disagree
#64575 (comment)

Copilot AI and others added 3 commits December 12, 2025 10:23
Co-authored-by: javiercn <6995051+javiercn@users.noreply.github.com>
Co-authored-by: javiercn <6995051+javiercn@users.noreply.github.com>
Co-authored-by: javiercn <6995051+javiercn@users.noreply.github.com>
Copilot AI changed the title Register Components metrics and tracing by default in WebAssembly host Register Components metrics and tracing by default in WebAssembly host with opt-out support Dec 12, 2025
Copilot AI requested a review from javiercn December 12, 2025 10:29
@javiercn javiercn changed the title Register Components metrics and tracing by default in WebAssembly host with opt-out support Register Components metrics and tracing by default in WebAssembly when metrics support is enabled Dec 12, 2025
@javiercn javiercn enabled auto-merge (squash) January 21, 2026 12:03
…metryEnabled

Co-authored-by: javiercn <6995051+javiercn@users.noreply.github.com>
auto-merge was automatically disabled January 21, 2026 12:18

Head branch was pushed to by a user without write access

Copilot AI changed the title Register Components metrics and tracing by default in WebAssembly when metrics support is enabled Register Components metrics and tracing in WebAssembly when metrics support is explicitly enabled Jan 21, 2026
@pavelsavara
Copy link
Member

Thank you!

@javiercn javiercn enabled auto-merge (squash) January 21, 2026 13:08
@javiercn javiercn merged commit db27498 into main Jan 21, 2026
25 checks passed
@javiercn javiercn deleted the copilot/register-metrics-tracing-default branch January 21, 2026 15:57
@dotnet-policy-service dotnet-policy-service bot added this to the 11.0-preview1 milestone Jan 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-blazor Includes: Blazor, Razor Components Attention: Shared Code Modified pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Blazor] Register Components metric and tracing by default on webassembly host.

5 participants

Comments