Skip to content

Commit 0148064

Browse files
JamesNKRick-Andersontdykstra
authored
Add metrics opt-out section to metrics doc (#34057)
Co-authored-by: Rick Anderson <[email protected]> Co-authored-by: Tom Dykstra <[email protected]>
1 parent d47691d commit 0148064

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed

aspnetcore/log-mon/metrics/metrics.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ For more information, see [dotnet-counters](/dotnet/core/diagnostics/dotnet-coun
9090
## Enrich the ASP.NET Core request metric
9191

9292
ASP.NET Core has many built-in metrics. The `http.server.request.duration` metric:
93+
9394
* Records the duration of HTTP requests on the server.
9495
* Captures request information in tags, such as the matched route and response status code.
9596

@@ -106,7 +107,29 @@ The proceeding example:
106107
* The tag allows requests to be categorized by marketing medium type, which could be useful when analyzing web app traffic.
107108

108109
> [!NOTE]
109-
> Follow the [multi-dimensional metrics](/dotnet/core/diagnostics/metrics-instrumentation#multi-dimensional-metrics) best practices when enriching with custom tags. Too many tags, or tags with an unbound range cause a large combination of tags. Collection tools have a limit on how many combinations they support for a counter and may start filtering results out to avoid excessive memory usage.
110+
> Follow the [multi-dimensional metrics](/dotnet/core/diagnostics/metrics-instrumentation#multi-dimensional-metrics) best practices when enriching with custom tags. Tags that are too numerous or have an unbound range create many tag combinations, resulting in high dimensions. Collection tools have limits on supported dimensions for a counter and may filter results to prevent excessive memory use.
111+
112+
:::moniker range=">= aspnetcore-9.0"
113+
114+
## Opt-out of HTTP metrics on certain endpoints and requests
115+
116+
Opting out of recording metrics is beneficial for endpoints frequently called by automated systems, such as health checks. Recording metrics for these requests is generally unnecessary. Unwanted telemetry uses resources to collect and store, and can distort results displayed in a telemetry dashboard.
117+
118+
HTTP requests to an endpoint can be excluded from metrics by adding metadata, with either the [DisableHttpMetrics](xref:Microsoft.AspNetCore.Http.DisableHttpMetricsAttribute) attribute or the [DisableHttpMetrics](xref:Microsoft.AspNetCore.Builder.HttpMetricsEndpointConventionBuilderExtensions.DisableHttpMetrics``1(``0)) method:
119+
120+
* Add the [DisableHttpMetrics](xref:Microsoft.AspNetCore.Http.DisableHttpMetricsAttribute) attribute to the Web API controller, SignalR hub or gRPC service.
121+
* Call [DisableHttpMetrics](xref:Microsoft.AspNetCore.Builder.HttpMetricsEndpointConventionBuilderExtensions.DisableHttpMetrics``1(``0)) when mapping endpoints in app startup:
122+
123+
:::code language="csharp" source="~/log-mon/metrics/metrics/samples/DisableMetrics/Program.cs" id="snippet_1" highlight="5":::
124+
125+
Alternatively, the <xref:Microsoft.AspNetCore.Http.Features.IHttpMetricsTagsFeature.MetricsDisabled?displayProperty=nameWithType> property has been added for:
126+
127+
* Advanced scenarios where a request doesn't map to an endpoint.
128+
* Dynamically disabling metrics collection for specific HTTP requests.
129+
130+
:::code language="csharp" source="~/log-mon/metrics/metrics/samples/DisableMetrics/Program.cs" id="snippet_2":::
131+
132+
:::moniker-end
110133

111134
## Create custom metrics
112135

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net9.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
9+
</Project>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#define SECOND // FIRST SECOND
2+
#if NEVER
3+
#elif FIRST
4+
// <snippet_1>
5+
var builder = WebApplication.CreateBuilder(args);
6+
builder.Services.AddHealthChecks();
7+
8+
var app = builder.Build();
9+
app.MapHealthChecks("/healthz").DisableHttpMetrics();
10+
app.Run();
11+
// </snippet_1>
12+
#elif SECOND
13+
using Microsoft.AspNetCore.Http.Features;
14+
15+
var builder = WebApplication.CreateBuilder(args);
16+
var app = builder.Build();
17+
18+
app.MapGet("/", () => "Hello World!");
19+
20+
// <snippet_2>
21+
// Middleware that conditionally opts-out HTTP requests.
22+
app.Use(async (context, next) =>
23+
{
24+
var metricsFeature = context.Features.Get<IHttpMetricsTagsFeature>();
25+
if (metricsFeature != null &&
26+
context.Request.Headers.ContainsKey("x-disable-metrics"))
27+
{
28+
metricsFeature.MetricsDisabled = true;
29+
}
30+
31+
await next(context);
32+
});
33+
// </snippet_2>
34+
35+
app.Run();
36+
#endif

0 commit comments

Comments
 (0)