Skip to content

[Instrumentation.AspNetCore] Handle cases where the HttpContext is a property of the payload (ASP.NET Core 2.x support) #2724

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/OpenTelemetry.Instrumentation.AspNetCore/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

## Unreleased

* Fixed issue
[#5019](https://github.com/open-telemetry/opentelemetry-dotnet/issues/5019)
where metrics and traces were not exported for incoming requests when using
ASP.NET Core 2.x.
([#2724](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/2724))

## 1.11.1

Released 2025-Mar-05
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ internal class HttpInListener : ListenerHandler
};

private static readonly PropertyFetcher<Exception> ExceptionPropertyFetcher = new("Exception");
private static readonly PropertyFetcher<HttpContext> HttpContextPropertyFetcher = new("HttpContext");

private readonly AspNetCoreTraceInstrumentationOptions options;

Expand Down Expand Up @@ -102,8 +103,15 @@ public void OnStartActivity(Activity activity, object? payload)

if (payload is not HttpContext context)
{
AspNetCoreInstrumentationEventSource.Log.NullPayload(nameof(HttpInListener), nameof(this.OnStartActivity), activity.OperationName);
return;
if (TryFetchHttpContext(payload, out var innerContext))
{
context = innerContext;
}
else
{
AspNetCoreInstrumentationEventSource.Log.NullPayload(nameof(HttpInListener), nameof(this.OnStartActivity), activity.OperationName);
return;
}
}

// Ensure context extraction irrespective of sampling decision
Expand Down Expand Up @@ -237,8 +245,15 @@ public void OnStopActivity(Activity activity, object? payload)
{
if (payload is not HttpContext context)
{
AspNetCoreInstrumentationEventSource.Log.NullPayload(nameof(HttpInListener), nameof(this.OnStopActivity), activity.OperationName);
return;
if (TryFetchHttpContext(payload, out var innerContext))
{
context = innerContext;
}
else
{
AspNetCoreInstrumentationEventSource.Log.NullPayload(nameof(HttpInListener), nameof(this.OnStopActivity), activity.OperationName);
return;
}
}

var response = context.Response;
Expand Down Expand Up @@ -347,6 +362,12 @@ static bool TryFetchException(object? payload, [NotNullWhen(true)] out Exception
}
}

#if NET
[UnconditionalSuppressMessage("Trimming", "IL2026", Justification = "The ASP.NET Core framework guarantees that top level properties are preserved")]
#endif
private static bool TryFetchHttpContext(object? payload, [NotNullWhen(true)] out HttpContext? context)
=> HttpContextPropertyFetcher.TryFetch(payload, out context) && context is not null;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool TryGetGrpcMethod(Activity activity, [NotNullWhen(true)] out string? grpcMethod)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,21 @@ static bool TryFetchException(object? payload, [NotNullWhen(true)] out Exception
{
return ExceptionPropertyFetcher.TryFetch(payload, out exc) && exc != null;
}
#if NET
[UnconditionalSuppressMessage("Trimming", "IL2026", Justification = "The ASP.NET Core framework guarantees that top level properties are preserved")]
#endif
static bool TryFetchHttpContext(object? payload, [NotNullWhen(true)] out HttpContext? ctx)
{
return HttpContextPropertyFetcher.TryFetch(payload, out ctx) && ctx != null;
}
}

public static void OnStopEventWritten(string name, object? payload)
{
if (payload is not HttpContext context)
{
AspNetCoreInstrumentationEventSource.Log.NullPayload(nameof(HttpInMetricsListener), nameof(OnStopEventWritten), HttpServerRequestDurationMetricName);
return;
if (TryFetchHttpContext(payload, out var innerContext))
{
context = innerContext;
}
else
{
AspNetCoreInstrumentationEventSource.Log.NullPayload(nameof(HttpInMetricsListener), nameof(OnStopEventWritten), HttpServerRequestDurationMetricName);
return;
}
}

TagList tags = default;
Expand Down Expand Up @@ -133,4 +133,10 @@ public override void OnEventWritten(string name, object? payload)
break;
}
}

#if NET
[UnconditionalSuppressMessage("Trimming", "IL2026", Justification = "The ASP.NET Core framework guarantees that top level properties are preserved")]
#endif
private static bool TryFetchHttpContext(object? payload, [NotNullWhen(true)] out HttpContext? context)
=> HttpContextPropertyFetcher.TryFetch(payload, out context) && context is not null;
}