Repository: dotnet/aspnetcore
File: src/Components/WebAssembly/WebAssembly/src/Hosting/WebAssemblyHostBuilder.cs (C#)

Code before:

    internal void InitializeDefaultServices()
    {
        Services.AddSingleton<IJSRuntime>(DefaultWebAssemblyJSRuntime.Instance);
        Services.AddSingleton<NavigationManager>(WebAssemblyNavigationManager.Instance);
        Services.AddSingleton<INavigationInterception>(WebAssemblyNavigationInterception.Instance);
        Services.AddSingleton<IScrollToLocationHash>(WebAssemblyScrollToLocationHash.Instance);
        Services.AddSingleton(_jsMethods);
        Services.AddSingleton(new LazyAssemblyLoader(DefaultWebAssemblyJSRuntime.Instance));
        Services.AddSingleton(_ => _rootComponentCache ?? new());
        Services.AddSingleton<ComponentStatePersistenceManager>();
        Services.AddSingleton(sp => sp.GetRequiredService<ComponentStatePersistenceManager>().State);
        Services.AddSupplyValueFromPersistentComponentStateProvider();
        Services.AddSingleton<IErrorBoundaryLogger, WebAssemblyErrorBoundaryLogger>();
        Services.AddSingleton<ResourceCollectionProvider>();
        RegisterPersistentComponentStateServiceCollectionExtensions.AddPersistentServiceRegistration<ResourceCollectionProvider>(Services, RenderMode.InteractiveWebAssembly);
        Services.AddLogging(builder =>
        {
            builder.AddProvider(new WebAssemblyConsoleLoggerProvider(DefaultWebAssemblyJSRuntime.Instance));
        });
        Services.AddSingleton<AntiforgeryStateProvider, DefaultAntiforgeryStateProvider>();
        RegisterPersistentComponentStateServiceCollectionExtensions.AddPersistentServiceRegistration<AntiforgeryStateProvider>(Services, RenderMode.InteractiveWebAssembly);
        Services.AddSupplyValueFromQueryProvider();
        
        // Register metrics and tracing by default (opt-out via feature switch for trimming)
        var isTelemetryDisabled = AppContext.TryGetSwitch("System.Diagnostics.Metrics.Meter.IsSupported", out var switchValue) && switchValue == false;
        if (!isTelemetryDisabled)
        {
            ComponentsMetricsServiceCollectionExtensions.AddComponentsMetrics(Services);
            ComponentsMetricsServiceCollectionExtensions.AddComponentsTracing(Services);
        }
        Services.AddSingleton<HostedServiceExecutor>();
    }
}


Review comment (suggestion):
please rename it to `isTelemetryEnabled` and make it opt-in, not opt out

Code after:

    internal void InitializeDefaultServices()
    {
        Services.AddSingleton<IJSRuntime>(DefaultWebAssemblyJSRuntime.Instance);
        Services.AddSingleton<NavigationManager>(WebAssemblyNavigationManager.Instance);
        Services.AddSingleton<INavigationInterception>(WebAssemblyNavigationInterception.Instance);
        Services.AddSingleton<IScrollToLocationHash>(WebAssemblyScrollToLocationHash.Instance);
        Services.AddSingleton(_jsMethods);
        Services.AddSingleton(new LazyAssemblyLoader(DefaultWebAssemblyJSRuntime.Instance));
        Services.AddSingleton(_ => _rootComponentCache ?? new());
        Services.AddSingleton<ComponentStatePersistenceManager>();
        Services.AddSingleton(sp => sp.GetRequiredService<ComponentStatePersistenceManager>().State);
        Services.AddSupplyValueFromPersistentComponentStateProvider();
        Services.AddSingleton<IErrorBoundaryLogger, WebAssemblyErrorBoundaryLogger>();
        Services.AddSingleton<ResourceCollectionProvider>();
        RegisterPersistentComponentStateServiceCollectionExtensions.AddPersistentServiceRegistration<ResourceCollectionProvider>(Services, RenderMode.InteractiveWebAssembly);
        Services.AddLogging(builder =>
        {
            builder.AddProvider(new WebAssemblyConsoleLoggerProvider(DefaultWebAssemblyJSRuntime.Instance));
        });
        Services.AddSingleton<AntiforgeryStateProvider, DefaultAntiforgeryStateProvider>();
        RegisterPersistentComponentStateServiceCollectionExtensions.AddPersistentServiceRegistration<AntiforgeryStateProvider>(Services, RenderMode.InteractiveWebAssembly);
        Services.AddSupplyValueFromQueryProvider();
        
        // Register metrics and tracing when explicitly enabled (opt-in via feature switch)
        var isTelemetryEnabled = AppContext.TryGetSwitch("System.Diagnostics.Metrics.Meter.IsSupported", out var switchValue) && switchValue == true;
        if (isTelemetryEnabled)
        {
            ComponentsMetricsServiceCollectionExtensions.AddComponentsMetrics(Services);
            ComponentsMetricsServiceCollectionExtensions.AddComponentsTracing(Services);
        }
        Services.AddSingleton<HostedServiceExecutor>();
    }
}