Skip to content

Commit d0e6ce8

Browse files
disable/trim MetricsHandler when System.Diagnostics.Metrics.Meter.IsSupported is false (#114326)
Co-authored-by: Anton Firszov <[email protected]>
1 parent c893f68 commit d0e6ce8

19 files changed

+196
-64
lines changed

eng/testing/linker/project.csproj.template

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
<MicrosoftNetCoreAppRuntimePackDir>{MicrosoftNetCoreAppRuntimePackDir}</MicrosoftNetCoreAppRuntimePackDir>
5454

5555
<RepositoryEngineeringDir>{RepositoryEngineeringDir}</RepositoryEngineeringDir>
56-
<_ExtraTrimmerArgs>{ExtraTrimmerArgs} $(_ExtraTrimmerArgs)</_ExtraTrimmerArgs>
56+
<_ExtraTrimmerArgs>{ExtraTrimmerArgs} $(_ExtraTrimmerArgs) --dump-dependencies</_ExtraTrimmerArgs>
5757
{AdditionalProperties}
5858

5959
<!-- Needed for PublishAot -->

src/libraries/System.Net.Http/src/ILLink/ILLink.Substitutions.xml

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/libraries/System.Net.Http/src/System.Net.Http.csproj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,6 @@
2424
<EmitCompilerGeneratedFiles Condition="'$(Configuration)' == 'Debug' and '$(TargetPlatformIdentifier)' == 'browser'">true</EmitCompilerGeneratedFiles>
2525
</PropertyGroup>
2626

27-
<ItemGroup>
28-
<ILLinkSubstitutionsXmls Include="$(ILLinkDirectory)ILLink.Substitutions.xml" />
29-
</ItemGroup>
30-
3127
<ItemGroup Condition="'$(TargetPlatformIdentifier)' != ''">
3228
<Compile Include="System\Net\Http\ByteArrayContent.cs" />
3329
<Compile Include="System\Net\Http\CancellationHelper.cs" />

src/libraries/System.Net.Http/src/System/Net/Http/DiagnosticsHandler.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ internal sealed class DiagnosticsHandler : HttpMessageHandlerStage
2424

2525
public DiagnosticsHandler(HttpMessageHandler innerHandler, DistributedContextPropagator propagator, bool autoRedirect = false)
2626
{
27-
Debug.Assert(IsGloballyEnabled());
27+
Debug.Assert(GlobalHttpSettings.DiagnosticsHandler.EnableActivityPropagation);
2828
Debug.Assert(innerHandler is not null && propagator is not null);
2929

3030
_innerHandler = innerHandler;
@@ -71,8 +71,6 @@ private static bool IsEnabled()
7171
return activity;
7272
}
7373

74-
internal static bool IsGloballyEnabled() => GlobalHttpSettings.DiagnosticsHandler.EnableActivityPropagation;
75-
7674
internal override ValueTask<HttpResponseMessage> SendAsync(HttpRequestMessage request, bool async, CancellationToken cancellationToken)
7775
{
7876
if (IsEnabled())
@@ -89,7 +87,7 @@ internal override ValueTask<HttpResponseMessage> SendAsync(HttpRequestMessage re
8987

9088
private async ValueTask<HttpResponseMessage> SendAsyncCore(HttpRequestMessage request, bool async, CancellationToken cancellationToken)
9189
{
92-
// HttpClientHandler is responsible to call static DiagnosticsHandler.IsEnabled() before forwarding request here.
90+
// HttpClientHandler is responsible to call static GlobalHttpSettings.DiagnosticsHandler.IsEnabled before forwarding request here.
9391
// It will check if propagation is on (because parent Activity exists or there is a listener) or off (forcibly disabled)
9492
// This code won't be called unless consumer unsubscribes from DiagnosticListener right after the check.
9593
// So some requests happening right after subscription starts might not be instrumented. Similarly,

src/libraries/System.Net.Http/src/System/Net/Http/GlobalHttpSettings.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System;
5+
using System.Diagnostics.CodeAnalysis;
6+
47
namespace System.Net.Http
58
{
69
/// <summary>
@@ -10,12 +13,21 @@ internal static class GlobalHttpSettings
1013
{
1114
internal static class DiagnosticsHandler
1215
{
16+
[FeatureSwitchDefinition("System.Net.Http.EnableActivityPropagation")]
1317
public static bool EnableActivityPropagation { get; } = RuntimeSettingParser.QueryRuntimeSettingSwitch(
1418
"System.Net.Http.EnableActivityPropagation",
1519
"DOTNET_SYSTEM_NET_HTTP_ENABLEACTIVITYPROPAGATION",
1620
true);
1721
}
1822

23+
internal static class MetricsHandler
24+
{
25+
[FeatureSwitchDefinition("System.Diagnostics.Metrics.Meter.IsSupported")]
26+
public static bool IsGloballyEnabled { get; } = RuntimeSettingParser.QueryRuntimeSettingSwitch(
27+
"System.Diagnostics.Metrics.Meter.IsSupported",
28+
true);
29+
}
30+
1931
internal static class SocketsHttpHandler
2032
{
2133
#if !TARGET_BROWSER && !TARGET_WASI

src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.AnyMobile.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,11 @@ private HttpMessageHandler Handler
4545

4646
// MetricsHandler should be descendant of DiagnosticsHandler in the handler chain to make sure the 'http.request.duration'
4747
// metric is recorded before stopping the request Activity. This is needed to make sure that our telemetry supports Exemplars.
48-
handler = new MetricsHandler(handler, _nativeMeterFactory, out _);
49-
if (DiagnosticsHandler.IsGloballyEnabled())
48+
if (GlobalHttpSettings.MetricsHandler.IsGloballyEnabled)
49+
{
50+
handler = new MetricsHandler(handler, _nativeMeterFactory, out _);
51+
}
52+
if (GlobalHttpSettings.DiagnosticsHandler.EnableActivityPropagation)
5053
{
5154
handler = new DiagnosticsHandler(handler, DistributedContextPropagator.Current);
5255
}

src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,11 @@ private HttpMessageHandler Handler
4545

4646
// MetricsHandler should be descendant of DiagnosticsHandler in the handler chain to make sure the 'http.request.duration'
4747
// metric is recorded before stopping the request Activity. This is needed to make sure that our telemetry supports Exemplars.
48-
handler = new MetricsHandler(handler, _meterFactory, out _);
49-
if (DiagnosticsHandler.IsGloballyEnabled())
48+
if (GlobalHttpSettings.MetricsHandler.IsGloballyEnabled)
49+
{
50+
handler = new MetricsHandler(handler, _meterFactory, out _);
51+
}
52+
if (GlobalHttpSettings.DiagnosticsHandler.EnableActivityPropagation)
5053
{
5154
handler = new DiagnosticsHandler(handler, DistributedContextPropagator.Current);
5255
}

src/libraries/System.Net.Http/src/System/Net/Http/Metrics/MetricsHandler.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System.Collections.Generic;
55
using System.Diagnostics;
6+
using System.Diagnostics.CodeAnalysis;
67
using System.Diagnostics.Metrics;
78
using System.Threading;
89
using System.Threading.Tasks;
@@ -17,6 +18,8 @@ internal sealed class MetricsHandler : HttpMessageHandlerStage
1718

1819
public MetricsHandler(HttpMessageHandler innerHandler, IMeterFactory? meterFactory, out Meter meter)
1920
{
21+
Debug.Assert(GlobalHttpSettings.MetricsHandler.IsGloballyEnabled);
22+
2023
_innerHandler = innerHandler;
2124

2225
meter = meterFactory?.Create("System.Net.Http") ?? SharedMeter.Instance;
@@ -49,6 +52,8 @@ internal override ValueTask<HttpResponseMessage> SendAsync(HttpRequestMessage re
4952

5053
private async ValueTask<HttpResponseMessage> SendAsyncWithMetrics(HttpRequestMessage request, bool async, CancellationToken cancellationToken)
5154
{
55+
Debug.Assert(GlobalHttpSettings.MetricsHandler.IsGloballyEnabled);
56+
5257
(long startTimestamp, bool recordCurrentRequests) = RequestStart(request);
5358
HttpResponseMessage? response = null;
5459
Exception? exception = null;

src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/ConnectionPool/ConnectionSetupDistributedTracing.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public static void AddConnectionLinkToRequestActivity(Activity connectionSetupAc
8585
Debug.Assert(connectionSetupActivity is not null);
8686

8787
// We only support links for request activities created by the "System.Net.Http" ActivitySource.
88-
if (DiagnosticsHandler.s_activitySource.HasListeners())
88+
if (GlobalHttpSettings.DiagnosticsHandler.EnableActivityPropagation && DiagnosticsHandler.s_activitySource.HasListeners())
8989
{
9090
Activity? requestActivity = Activity.Current;
9191
if (requestActivity?.Source == DiagnosticsHandler.s_activitySource)

src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/ConnectionPool/HttpConnectionPool.Http3.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,9 @@ internal sealed partial class HttpConnectionPool
8787
ThrowGetVersionException(request, 3, reasonException);
8888
}
8989

90-
long queueStartingTimestamp = HttpTelemetry.Log.IsEnabled() || Settings._metrics!.RequestsQueueDuration.Enabled ? Stopwatch.GetTimestamp() : 0;
90+
long queueStartingTimestamp = HttpTelemetry.Log.IsEnabled() || (GlobalHttpSettings.MetricsHandler.IsGloballyEnabled && Settings._metrics!.RequestsQueueDuration.Enabled)
91+
? Stopwatch.GetTimestamp()
92+
: 0;
9193
Activity? waitForConnectionActivity = ConnectionSetupDistributedTracing.StartWaitForConnectionActivity(authority);
9294

9395
if (!TryGetPooledHttp3Connection(request, out Http3Connection? connection, out http3ConnectionWaiter))

0 commit comments

Comments
 (0)