It is common to have an ASP.NET Core application that utilizes auto-instrumentation, specifically enabling OpenTelemetry.Instrumentation.AspNetCore and OpenTelemetry.Instrumentation.Http. The API can expose status endpoints (e.g. /status/live) that are invoked by something like a Kubernetes liveness probe. Often times, the /status/live endpoint may make external http calls to validate dependencies. It is sometimes desirable to suppress all spans that are created from calling /status/live.
Filtering the root span via OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreInstrumentationOptions.Filter is possible but that causes orphaned auto instrumented http spans. .OpenTelemetry.Instrumentation.HttpHttpClientInstrumentationOptions also provides a Filter method but there is not an obvious way how to determine if they are children of /status/live and not some other desired endpoint.
Custom Samplers execute before the ASP.NET auto instrumentation and the span name is always of the form Microsoft.AspNetCore.Hosting.HttpRequestIn with no way to distinguish from any other endpoints.
This repository provides an example for suppressing all spans related to a specific endpoint. Filtering happens twice because OpenTelemetry.Instrumentation.AspNetCore is still executing after the SpanSuppressionMiddleware returns.
- As part of the
OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreInstrumentationOptions.Filter. This is needed to suppress the span created byOpenTelemetry.Instrumentation.AspNetCore.- Unfortunately
OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreInstrumentationOptions.Filterexecutes afterSpanSuppressionMiddleware
- Unfortunately
- As part of a custom middleware
SpanSuppressionMiddlewarewhich usesHttpContextHelperto determine which endpoints to filter. The middleware suppresses all descendants of the span created byOpenTelemetry.Instrumentation.AspNetCoreby usingOpenTelemetry.SuppressInstrumentationScope. A custom middleware is used to provide the simplest mechanism for managing theIDisposablecreated byOpenTelemetry.SuppressInstrumentationScope.
It would be ideal if this type of advanced suppression had first class support via the OpenTelemetry specification.