dotnet / dotnet/aspnetcore

Add support for retries with Polly

Open
#43,942 2 comments 1 reaction 0 assignees View on GitHub
area-networking feature-httpclientfactory
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 6h
Merged PRs (30d)
290

Description

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Is your feature request related to a problem? Please describe the problem.

_No response_

### Describe the solution you'd like

Currently, if you configure retrays using [AddTransientHttpErrorPolicy](https://github.com/App-vNext/Polly/wiki/Polly-and-HttpClientFactory#using-addtransienthttperrorpolicy), there will be no traces if the request will retray.
Even if you add the [OpenTelemetry.Instrumentation.Http](https://github.com/open-telemetry/opentelemetry-dotnet/tree/main/src/OpenTelemetry.Instrumentation.Http), only the first request will be visible in the traces, and subsequent attempts will not be displayed in any way.

This is because [Polly](https://github.com/App-vNext/Polly) is [configured](https://github.com/dotnet/aspnetcore/blob/49599d2691bdfb3d0b5beb746573b2a521e8557a/src/HttpClientFactory/Polly/src/PolicyHttpMessageHandler.cs#L131) with an extension to retry the `SendCoreAsync` method in the [`PolicyHttpMessageHandler`](https://github.com/dotnet/aspnetcore/blob/main/src/HttpClientFactory/Polly/src/PolicyHttpMessageHandler.cs) class.

```csharp
esponse = await policy.ExecuteAsync((c, ct) => SendCoreAsync(request, c, ct), context, cancellationToken).ConfigureAwait(false);
```

Whereas right now activities are created at a higher level in the [HttpClient](https://github.com/dotnet/runtime/blob/main/src/libraries/System.Net.Http/src/System/Net/Http/HttpClient.cs#L526) class.

### Additional context

I propose to extend the PolicyHttpMessageHandler class: add the creation of a span to it when a request is made. And also add the `RetryAttempt` property to the [Polly.Context](https://github.com/App-vNext/Polly/blob/master/src/Polly/Context.cs) class so that we can fill in the tags according to the OpenTelemetry [convention](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/http.md#http-request-retries-and-redirects).

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.