-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Register Components metrics and tracing by default in WebAssembly when metrics support is enabled #64737
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Co-authored-by: javiercn <[email protected]>
| <ItemGroup> | ||
| <Reference Include="Microsoft.AspNetCore.Components.WebAssembly" /> | ||
| <Reference Include="Microsoft.CodeAnalysis.CSharp" /> | ||
| <Reference Include="Microsoft.Extensions.Diagnostics" /> |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this 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
ComponentsMetricsandComponentsActivitySourceinWebAssemblyHostBuilder.InitializeDefaultServices() - Added
Microsoft.Extensions.Diagnosticsdependency to both source and test projects - Added test coverage verifying
IMeterFactoryregistration
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 |
| [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); | ||
| } |
Copilot
AI
Dec 11, 2025
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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>.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)
Co-authored-by: javiercn <[email protected]>
Co-authored-by: javiercn <[email protected]>
Co-authored-by: javiercn <[email protected]>
Register Components metrics and tracing by default in WebAssembly host with opt-out support
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.IsSupportedfeature switch for trimming scenarios.Changes:
Microsoft.Extensions.Diagnosticsreference to WebAssembly project (brought in transitively to test project)ComponentsMetricsandComponentsActivitySourceinWebAssemblyHostBuilder.InitializeDefaultServices()behind feature switch checkSystem.Diagnostics.Metrics.Meter.IsSupportedAppContext switch tofalseIMeterFactoryregistration and documented that both metrics and tracing services are registered togetherThis enables consistent telemetry collection across Blazor hosting modes while supporting IL trimming for size-constrained scenarios through the feature switch mechanism.
Original prompt
💡 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.