Health probes hit /health on your ASP.NET Core 10 booking API every few seconds; how do you keep them out of its request metrics and traces?
answer
- metrics and traces are filtered separately
- endpoint metadata since ASP.NET Core 9
- DisableHttpMetrics() or [DisableHttpMetrics]
- IHttpMetricsTagsFeature.MetricsDisabled per request
- Filter on the trace instrumentation options
basics
~10 sChain .DisableHttpMetrics() on the health endpoint (or put [DisableHttpMetrics] on an action) so ASP.NET Core skips http.server.request.duration, and set AspNetCoreTraceInstrumentationOptions.Filter to drop the path from traces. The two switches are independent.
solid answer
~30 sMetrics and traces are filtered in different places. For metrics, ASP.NET Core 9 added `DisableHttpMetrics()`, so `app.MapHealthChecks("/health").DisableHttpMetrics()` attaches metadata that makes the hosting layer skip `http.server.request.duration` for that endpoint; `[DisableHttpMetrics]` does the same on a controller or action. When the decision is per request rather than per endpoint, middleware can set `IHttpMetricsTagsFeature.MetricsDisabled = true`. `http.server.active_requests` still counts those requests. For traces, I configure `AddAspNetCoreInstrumentation(o => o.Filter = ctx => !ctx.Request.Path.StartsWithSegments("/health"))`; returning `false` drops the request span. Neither switch affects the other signal, so a probe-free dashboard needs both.
code
csharp · 19 linesusing Microsoft.AspNetCore.Http.Features;
builder.Services.AddHealthChecks();
builder.Services.AddOpenTelemetry()
.WithTracing(t => t.AddAspNetCoreInstrumentation(o =>
o.Filter = ctx => !ctx.Request.Path.StartsWithSegments("/health")))
.WithMetrics(m => m.AddAspNetCoreInstrumentation())
.UseOtlpExporter();
var app = builder.Build();
app.Use(async (ctx, next) =>
{
if (ctx.Features.Get<IHttpMetricsTagsFeature>() is { } metrics)
metrics.Tags.Add(new("airline.channel", ctx.Request.Headers.ContainsKey("X-Mobile-App") ? "mobile" : "web"));
await next(ctx);
});
app.MapHealthChecks("/health").DisableHttpMetrics();go deeper
Know that probe traffic distorts request metrics and that ASP.NET Core can exclude an endpoint with DisableHttpMetrics.
Show both switches: DisableHttpMetrics on the mapped endpoint for metrics and the Filter option for traces, and explain that each covers only its own signal.
Handle the per-request case with IHttpMetricsTagsFeature, explain why active_requests still counts probes, and keep the Filter cheap and exception-free.
Define which synthetic traffic every service must exclude and how, so fleet-wide latency SLOs are computed on user traffic only.
## Why probe traffic is a problem A load balancer or orchestrator calling `/health` every few seconds on every instance produces a steady stream of tiny, fast, successful requests. Left in: - the **request-duration histogram** is skewed toward near-zero latencies, so percentiles look healthier than real user traffic; - **request rates** are inflated by traffic no user generated; - **traces** fill with thousands of identical one-span traces that cost storage and hide the interesting ones. ASP.NET Core 10 gives one switch for metrics and the OpenTelemetry instrumentation gives another for traces. They are independent. ## Metrics: `DisableHttpMetrics` (ASP.NET Core 9 and later) ASP.NET Core 9 added a way to opt an endpoint out of the built-in request-duration metric: - **Minimal APIs and mapped endpoints:** chain `.DisableHttpMetrics()` on the endpoint builder, for example `app.MapHealthChecks("/health").DisableHttpMetrics()`. `MapHealthChecks` returns an `IEndpointConventionBuilder`, so the call chains like any other convention. - **Controllers and actions:** put `[DisableHttpMetrics]` (namespace `Microsoft.AspNetCore.Http`) on the class or method. Both add `IDisableHttpMetricsMetadata` to the endpoint. When the request ends, the hosting layer checks the matched endpoint's metadata and skips recording **`http.server.request.duration`**. What it does **not** do: 1. It does not touch **`http.server.active_requests`**; that up-down counter is incremented when the request starts, before routing has chosen an endpoint, and decremented at the end as usual. 2. It does not affect **traces** or **logs**. 3. It does not affect other meters, such as Kestrel's connection metrics. ## Metrics, per request: `IHttpMetricsTagsFeature` When the decision depends on the request rather than the endpoint (for example, any request carrying a synthetic-monitoring header), middleware can use the feature that the hosting layer puts on `HttpContext.Features`: - `MetricsDisabled = true` skips the duration measurement for this request, just like the metadata does; - `Tags` lets you **add** tags to the duration measurement, for example `airline.channel = mobile`. The feature is only set when something is listening to the hosting meter, so read it with `Features.Get<IHttpMetricsTagsFeature>()` and a null check. ## Traces: the instrumentation's `Filter` Request spans come from OpenTelemetry's ASP.NET Core instrumentation, so the switch lives in **`AspNetCoreTraceInstrumentationOptions`**: - `Filter` is a `Func<HttpContext, bool>`; returning `true` collects the request, returning `false` drops it. - If the filter **throws**, the request is not collected either, so keep it trivial. - Matching with `PathString.StartsWithSegments("/health")` covers `/health` and `/health/ready` but not `/healthy-flights`. `EnrichWithHttpRequest`, `EnrichWithHttpResponse` and `EnrichWithException` on the same options class add tags to spans; they have no effect on metrics. The meter-side `AddAspNetCoreInstrumentation()` takes no options at all on .NET 8 and later. ## Putting it together for the booking API 1. Register health checks and map them with `app.MapHealthChecks("/health").DisableHttpMetrics()`; the latency histogram now reflects passengers, not probes. 2. Add a trace `Filter` that rejects paths under `/health`; the trace store stops filling with one-span probe traces. 3. If a synthetic checkout monitor runs against real endpoints, recognise it in a small middleware, set `MetricsDisabled` for it, and test the same condition in the trace filter. 4. Optionally tag the remaining traffic with a bounded value such as the sales channel through `IHttpMetricsTagsFeature.Tags`, so dashboards can split latency by channel without a custom meter. ## Summary table | Goal | Mechanism | Signal affected | |---|---|---| | Drop an endpoint from request duration | `.DisableHttpMetrics()` / `[DisableHttpMetrics]` | metrics only | | Drop one request from request duration | `IHttpMetricsTagsFeature.MetricsDisabled` | metrics only | | Add a tag to request duration | `IHttpMetricsTagsFeature.Tags` | metrics only | | Drop requests from traces | `AspNetCoreTraceInstrumentationOptions.Filter` | traces only | | Drop a whole instrument | `AddView(name, MetricStreamConfiguration.Drop)` | that instrument everywhere | A **view** with `MetricStreamConfiguration.Drop` removes an instrument for every endpoint, so it is the wrong tool for a single path. ## Version note `DisableHttpMetrics()`, `[DisableHttpMetrics]` and the `IHttpMetricsTagsFeature.MetricsDisabled` property all arrived in ASP.NET Core 9; on .NET 8 the feature offered only `Tags`, so an endpoint could not opt out of the built-in duration metric. This answer assumes ASP.NET Core 10.
- After adding DisableHttpMetrics, http.server.active_requests still moves on every probe. Is that a bug?No. The active-requests counter is incremented when the request starts, before routing knows the endpoint, and decremented at the end. `DisableHttpMetrics` only suppresses the `http.server.request.duration` measurement. The probes are short, so their effect on the active-requests gauge is small.
- How would you exclude requests from a synthetic monitor that hits normal business endpoints?Endpoint metadata cannot help, because the endpoints are shared with users. Middleware that recognises the monitor, for example by a header, sets `IHttpMetricsTagsFeature.MetricsDisabled = true` for metrics, and the trace `Filter` checks the same condition to drop the span.
- [DisableHttpMetrics] also removes the endpoint's spans from OpenTelemetry traces.
- The trace Filter option also keeps the endpoint out of http.server.request.duration.
- DisableHttpMetrics stops every built-in metric for the endpoint, including active requests.
- A view with MetricStreamConfiguration.Drop is the way to exclude one path.
- On ASP.NET Core 10 a health endpoint can only be excluded from metrics by a custom meter.
Assumes ASP.NET Core 10; DisableHttpMetrics(), [DisableHttpMetrics] and IHttpMetricsTagsFeature.MetricsDisabled were added in ASP.NET Core 9.