Azure / Azure/azure-functions-host
Capturing Dependency Failures from Code - Dotnet
- Dominant language
- C#
- Stars
- 2k
- Forks
- 482
- Avg merge
- 2d 12h
- Merged PRs (30d)
- 38
Description
#### What language does your question apply to? (e.g. C#, JavaScript, Java, All)
C#
#### Question
Azure Function-EventHub Trigger deployed in dedicated app service plan. As part of the logic I have to call our internal API to send Event hub message received (event hub trigger). Everything is working fine, however intermittently our internal API call is giving 504 - Gateway Timedout Error - which we are working with the API team to figure it out the issue. But the problem is I cannot catch this particular 504 error in my code. I can able to catch the 502,400,404 and everything except this 504 error.
Here is my startup
``` C#
private ILoggerFactory _loggerFactory;
public override void Configure(IFunctionsHostBuilder builder)
{
_loggerFactory = new LoggerFactory();
builder.Services.AddLogging();
builder.Services.AddHttpClient()
.ConfigurePrimaryHttpMessageHandler(x => new HttpClientHandler()
{
MaxConnectionsPerServer = 22,
CheckCertificateRevocationList = true,
ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator
})
.SetHandlerLifetime(TimeSpan.FromMinutes(5))
.AddPolicyHandler(GetRetryPolicy());
}
private IAsyncPolicy GetRetryPolicy()
{
var _logger = _loggerFactory.CreateLogger("Startup");
return HttpPolicyExtensions
.HandleTransientHttpError()
.OrResult(msg => msg.StatusCode == System.Net.HttpStatusCode.NotFound)
.WaitAndRetryAsync(Backoff.DecorrelatedJitterBackoffV2(medianFirstRetryDelay: TimeSpan.FromSeconds(1), retryCount: 3),
onRetry: (dr, ts) => {
_logger.LogInformation($"Retry Delay: {ts}");
});
}
My httpclient service
```C#
public class TestHttpClient : ITestHttpClient
{
private System.Net.Http.HttpClient _client;
public TestHttpClient(HttpClient client)
{
_client = client;
_client.Timeout = TimeSpan.FromSeconds(300);
}
public System.Net.Http.HttpClient Client { get { return _client; } }
}
```
The actual Logic inside my EventHub Trigger
```C#
public class EventHubTriggers
{
private readonly ITestHttpClient _testHttpClient;
public EventHubTriggers(ITestHttpClient testHttpClient)
{
_testHttpClient = testHttpClient;
}
[FunctionName("Trigger00")]
public async Task Run00AppLogs([EventHubTrigger("eventhub-01", Connection = "EventHubConnectionAppSetting")] EventData[] events)
{
....
....
...
...
try
{
....
_testHttpClient.Client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(_bearer, _splunkAuth);
_testHttpClient.Client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json")
);
HttpResponseMessage response = await _testHttpClient.Client.PostAsync("InternalURI", postContent);
response.EnsureSuccessStatusCode();
string responseBody = response.Content.ReadAsStringAsync().Result;
}
Catch(Exception e) // I also tried with HttpRequestException
{
.....
.....
// Not able to Capture 504 GatewayTimeout. How to capture it ?
}
}
}
```
As I connected my function app with app insights I can only see the 504 error on App Insights under the `Dependencies` telemetry. But I want to capture this 504 in my code.
[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/e0MeA.png
Contributor guide
Assessment
This issue has not been assessed yet.