Skip to content

Record OperationCanceledException in HttpClient native instrumentation #116269

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

Merged
merged 9 commits into from
Jun 10, 2025
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,10 @@ await _innerHandler.SendAsync(request, cancellationToken).ConfigureAwait(false)
_innerHandler.Send(request, cancellationToken);
return response;
}
catch (OperationCanceledException)
catch (OperationCanceledException ex)
{
taskStatus = TaskStatus.Canceled;
exception = ex;

// we'll report task status in HttpRequestOut.Stop
throw;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,63 @@ static async Task RunTest(string useVersion, string testAsync, string failureTyp
}
}

[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
public async Task SendAsync_OperationCanceledException_RecordsActivitiesWithCorrectErrorInfo()
{
await RemoteExecutor.Invoke(RunTest, UseVersion.ToString(), TestAsync.ToString()).DisposeAsync();
static async Task RunTest(string useVersion, string testAsync)
{
TaskCompletionSource activityStopTcs = new(TaskCreationOptions.RunContinuationsAsynchronously);
Activity? activity = null;

using ActivityListener listener = new ActivityListener()
{
ShouldListenTo = s => s.Name is "System.Net.Http",
Sample = (ref ActivityCreationOptions<ActivityContext> _) => ActivitySamplingResult.AllData,
ActivityStopped = a =>
{
activity = a;
Assert.Equal(ActivityStatusCode.Error, a.Status);
ActivityAssert.HasTag(a, "error.type", typeof(TaskCanceledException).FullName);
ActivityEvent evt = a.Events.Single(e => e.Name == "exception");
Dictionary<string, object?> tags = evt.Tags.ToDictionary(t => t.Key, t => t.Value);
Assert.Contains("exception.type", tags.Keys);
Assert.Contains("exception.message", tags.Keys);
Assert.Contains("exception.stacktrace", tags.Keys);
Assert.Equal(typeof(TaskCanceledException).FullName, tags["exception.type"]);

activityStopTcs.SetResult();
}
};
ActivitySource.AddActivityListener(listener);

var cts = new CancellationTokenSource();

await GetFactoryForVersion(useVersion).CreateClientAndServerAsync(
async uri =>
{
Version version = Version.Parse(useVersion);
if (version != HttpVersion30)
{
uri = new Uri($"{uri.Scheme}://localhost:{uri.Port}");
}

await Assert.ThrowsAsync<TaskCanceledException>(() => GetAsync(useVersion, testAsync, uri, cts.Token));
},
async server =>
{
await server.AcceptConnectionAsync(async connection =>
{
cts.Cancel();

await activityStopTcs.Task;
});
});

Assert.NotNull(activity);
}
}

[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
public async Task SendAsync_ExpectedDiagnosticSourceActivityLogging_InvalidBaggage()
{
Expand Down
Loading