Skip to content

Commit b93eef0

Browse files
committed
Retry on failure to flush to the API
1 parent 3bca90c commit b93eef0

File tree

2 files changed

+91
-20
lines changed

2 files changed

+91
-20
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using System;
2+
using System.Net;
3+
using System.Net.Http;
4+
using System.Threading;
5+
using System.Threading.Tasks;
6+
using Xunit;
7+
8+
namespace Datadog.Tracer.Tests
9+
{
10+
public class SetResponseHandler : DelegatingHandler
11+
{
12+
private HttpResponseMessage _response;
13+
public int RequestsCount { get; set; }
14+
15+
public SetResponseHandler(HttpResponseMessage response)
16+
{
17+
_response = response;
18+
}
19+
20+
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
21+
{
22+
RequestsCount++;
23+
return Task.FromResult(_response);
24+
}
25+
}
26+
27+
public class ApiTests
28+
{
29+
private const int _retries = 2;
30+
31+
[Fact]
32+
public async Task SendServiceAsync_200OK_AllGood()
33+
{
34+
var response = new HttpResponseMessage
35+
{
36+
StatusCode = HttpStatusCode.OK
37+
};
38+
var handler = new SetResponseHandler(response);
39+
var api = new Api(new Uri("http://localhost:1234"), handler);
40+
41+
await api.SendServiceAsync(new ServiceInfo());
42+
43+
Assert.Equal(1, handler.RequestsCount);
44+
}
45+
46+
[Fact]
47+
public async Task SendServiceAsync_500_Retry()
48+
{
49+
var response = new HttpResponseMessage
50+
{
51+
StatusCode = HttpStatusCode.InternalServerError
52+
};
53+
var handler = new SetResponseHandler(response);
54+
var api = new Api(new Uri("http://localhost:1234"), handler);
55+
56+
await api.SendServiceAsync(new ServiceInfo());
57+
58+
Assert.Equal(1 + _retries, handler.RequestsCount);
59+
}
60+
}
61+
}

src/Datadog.Tracer/Api.cs

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using MsgPack.Serialization;
22
using System;
33
using System.Collections.Generic;
4-
using System.IO;
4+
using System.Net;
55
using System.Net.Http;
66
using System.Reflection;
77
using System.Runtime.InteropServices;
@@ -54,32 +54,42 @@ public Api(Uri baseEndpoint, DelegatingHandler delegatingHandler = null)
5454
_client.DefaultRequestHeaders.Add("Datadog-Meta-Tracer-Version", Assembly.GetEntryAssembly().GetName().Version.ToString());
5555
}
5656

57-
public async Task SendTracesAsync(IList<List<Span>> traces)
57+
private async Task SendAsync<T>(T value, Uri endpoint)
5858
{
59-
try
60-
{
61-
var content = new MsgPackContent<IList<List<Span>>>(traces, _serializationContext);
62-
var response = await _client.PostAsync(_tracesEndpoint, content);
63-
response.EnsureSuccessStatusCode();
64-
}
65-
catch
59+
const int retries = 2;
60+
for (int i = 0; i < retries + 1; i++)
6661
{
67-
//TODO:bertrand Log exception
62+
try
63+
{
64+
var content = new MsgPackContent<T>(value, _serializationContext);
65+
var response = await _client.PostAsync(endpoint, content);
66+
if(response.StatusCode == HttpStatusCode.OK)
67+
{
68+
return;
69+
}
70+
if((int)response.StatusCode >= 400 && (int)response.StatusCode < 500)
71+
{
72+
// TODO:bertrand log
73+
return;
74+
}
75+
throw new HttpRequestException($"The request to {endpoint} failed with status {response.StatusCode}");
76+
}
77+
catch
78+
{
79+
// TODO:bertrand Log
80+
}
81+
await Task.Delay(TimeSpan.FromSeconds(1));
6882
}
6983
}
7084

85+
public async Task SendTracesAsync(IList<List<Span>> traces)
86+
{
87+
await SendAsync(traces, _tracesEndpoint);
88+
}
89+
7190
public async Task SendServiceAsync(ServiceInfo service)
7291
{
73-
try
74-
{
75-
var content = new MsgPackContent<ServiceInfo>(service, _serializationContext);
76-
var response = await _client.PostAsync(_servicesEndpoint, content);
77-
response.EnsureSuccessStatusCode();
78-
}
79-
catch
80-
{
81-
// TODO:bertrand log exception
82-
}
92+
await SendAsync(service, _servicesEndpoint);
8393
}
8494
}
8595
}

0 commit comments

Comments
 (0)