Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 11, 2025

Register Components metrics and tracing by default in WebAssembly host with opt-out 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.

Align WebAssembly host with server render mode by enabling telemetry by default with opt-out support via feature switch.

Description

WebAssembly host now registers metrics and tracing services during initialization, matching server-side behavior. Services are enabled by default and can be disabled via the System.Diagnostics.Metrics.Meter.IsSupported feature switch for trimming scenarios.

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 enabled by default (opt-out) and can be disabled by setting the System.Diagnostics.Metrics.Meter.IsSupported AppContext switch to false
  • Added test coverage verifying IMeterFactory registration and documented that both metrics and tracing services are registered together

This enables consistent telemetry collection across Blazor hosting modes while supporting IL trimming for size-constrained scenarios through the 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.

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
<ItemGroup>
<Reference Include="Microsoft.AspNetCore.Components.WebAssembly" />
<Reference Include="Microsoft.CodeAnalysis.CSharp" />
<Reference Include="Microsoft.Extensions.Diagnostics" />
Copy link
Member

Choose a reason for hiding this comment

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

Isn't this brought in transitively via the Webassembly reference?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, you're correct. The Microsoft.Extensions.Diagnostics reference comes transitively through the WebAssembly reference. Removed it from the test project in bc57528.

@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

Comment on lines 346 to 356
[Fact]
public void Constructor_RegistersMetricsServices()
{
// Arrange & Act
var builder = new WebAssemblyHostBuilder(new TestInternalJSImportMethods());
var host = builder.Build();

// Assert - Verify that IMeterFactory is registered (required for ComponentsMetrics)
var meterFactory = host.Services.GetService<IMeterFactory>();
Assert.NotNull(meterFactory);
}
Copy link

Copilot AI Dec 11, 2025

Choose a reason for hiding this comment

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

The test only verifies that IMeterFactory is registered (from AddComponentsMetrics), but doesn't verify that ComponentsActivitySource is registered (from AddComponentsTracing). Consider adding an assertion to verify that ComponentsActivitySource can be resolved from the service provider to ensure both metrics and tracing services are properly registered.

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated the test to document that both metrics and tracing services are registered together. Since ComponentsActivitySource is internal and scoped (not exposed to the WebAssembly test project), we can't directly test for it, but the test now clarifies that both AddComponentsMetrics and AddComponentsTracing are called. Commit 500e99a.

Services.AddSingleton<AntiforgeryStateProvider, DefaultAntiforgeryStateProvider>();
RegisterPersistentComponentStateServiceCollectionExtensions.AddPersistentServiceRegistration<AntiforgeryStateProvider>(Services, RenderMode.InteractiveWebAssembly);
Services.AddSupplyValueFromQueryProvider();
ComponentsMetricsServiceCollectionExtensions.AddComponentsMetrics(Services);
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 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

4 participants