Skip to content

Commit 5849d3a

Browse files
Otlp Retry Part1 - Refactor ExportClients (#5335)
1 parent e4b08ac commit 5849d3a

15 files changed

+121
-102
lines changed

src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/ExportClient/BaseOtlpGrpcExportClient.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation.ExportClie
1313
/// <typeparam name="TRequest">Type of export request.</typeparam>
1414
internal abstract class BaseOtlpGrpcExportClient<TRequest> : IExportClient<TRequest>
1515
{
16+
protected static readonly ExportClientGrpcResponse SuccessExportResponse = new ExportClientGrpcResponse(success: true, deadlineUtc: null, exception: null);
17+
1618
protected BaseOtlpGrpcExportClient(OtlpExporterOptions options)
1719
{
1820
Guard.ThrowIfNull(options);
@@ -38,7 +40,7 @@ protected BaseOtlpGrpcExportClient(OtlpExporterOptions options)
3840
internal int TimeoutMilliseconds { get; }
3941

4042
/// <inheritdoc/>
41-
public abstract bool SendExportRequest(TRequest request, CancellationToken cancellationToken = default);
43+
public abstract ExportClientResponse SendExportRequest(TRequest request, CancellationToken cancellationToken = default);
4244

4345
/// <inheritdoc/>
4446
public virtual bool Shutdown(int timeoutMilliseconds)

src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/ExportClient/BaseOtlpHttpExportClient.cs

+16-5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation.ExportClie
1212
/// <typeparam name="TRequest">Type of export request.</typeparam>
1313
internal abstract class BaseOtlpHttpExportClient<TRequest> : IExportClient<TRequest>
1414
{
15+
private static readonly ExportClientHttpResponse SuccessExportResponse = new ExportClientHttpResponse(success: true, deadlineUtc: null, response: null, exception: null);
16+
1517
protected BaseOtlpHttpExportClient(OtlpExporterOptions options, HttpClient httpClient, string signalPath)
1618
{
1719
Guard.ThrowIfNull(options);
@@ -34,24 +36,33 @@ protected BaseOtlpHttpExportClient(OtlpExporterOptions options, HttpClient httpC
3436
internal IReadOnlyDictionary<string, string> Headers { get; }
3537

3638
/// <inheritdoc/>
37-
public bool SendExportRequest(TRequest request, CancellationToken cancellationToken = default)
39+
public ExportClientResponse SendExportRequest(TRequest request, CancellationToken cancellationToken = default)
3840
{
41+
DateTime deadline = DateTime.UtcNow.AddMilliseconds(this.HttpClient.Timeout.TotalMilliseconds);
3942
try
4043
{
4144
using var httpRequest = this.CreateHttpRequest(request);
4245

4346
using var httpResponse = this.SendHttpRequest(httpRequest, cancellationToken);
4447

45-
httpResponse?.EnsureSuccessStatusCode();
48+
try
49+
{
50+
httpResponse.EnsureSuccessStatusCode();
51+
}
52+
catch (HttpRequestException ex)
53+
{
54+
return new ExportClientHttpResponse(success: false, deadlineUtc: deadline, response: httpResponse, ex);
55+
}
56+
57+
// We do not need to return back response and deadline for successful response so using cached value.
58+
return SuccessExportResponse;
4659
}
4760
catch (HttpRequestException ex)
4861
{
4962
OpenTelemetryProtocolExporterEventSource.Log.FailedToReachCollector(this.Endpoint, ex);
5063

51-
return false;
64+
return new ExportClientHttpResponse(success: false, deadlineUtc: deadline, response: null, exception: ex);
5265
}
53-
54-
return true;
5566
}
5667

5768
/// <inheritdoc/>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
#nullable enable
5+
6+
namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation.ExportClient;
7+
8+
internal sealed class ExportClientGrpcResponse : ExportClientResponse
9+
{
10+
public ExportClientGrpcResponse(
11+
bool success,
12+
DateTime? deadlineUtc,
13+
Exception? exception)
14+
: base(success, deadlineUtc, exception)
15+
{
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
#nullable enable
5+
6+
using System.Net;
7+
#if NETFRAMEWORK
8+
using System.Net.Http;
9+
#endif
10+
using System.Net.Http.Headers;
11+
12+
namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation.ExportClient;
13+
14+
internal sealed class ExportClientHttpResponse : ExportClientResponse
15+
{
16+
public ExportClientHttpResponse(
17+
bool success,
18+
DateTime? deadlineUtc,
19+
HttpResponseMessage? response,
20+
Exception? exception)
21+
: base(success, deadlineUtc, exception)
22+
{
23+
this.Headers = response?.Headers;
24+
this.StatusCode = response?.StatusCode;
25+
}
26+
27+
public HttpResponseHeaders? Headers { get; }
28+
29+
public HttpStatusCode? StatusCode { get; }
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
#nullable enable
5+
6+
namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation.ExportClient;
7+
8+
internal abstract class ExportClientResponse
9+
{
10+
protected ExportClientResponse(bool success, DateTime? deadlineUtc, Exception? exception)
11+
{
12+
this.Success = success;
13+
this.Exception = exception;
14+
this.DeadlineUtc = deadlineUtc;
15+
}
16+
17+
public bool Success { get; }
18+
19+
public Exception? Exception { get; }
20+
21+
public DateTime? DeadlineUtc { get; }
22+
}

src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/ExportClient/IExportClient.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright The OpenTelemetry Authors
22
// SPDX-License-Identifier: Apache-2.0
33

4+
#nullable enable
5+
46
namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation.ExportClient;
57

68
/// <summary>Export client interface.</summary>
@@ -12,8 +14,8 @@ internal interface IExportClient<in TRequest>
1214
/// </summary>
1315
/// <param name="request">The request to send to the server.</param>
1416
/// <param name="cancellationToken">An optional token for canceling the call.</param>
15-
/// <returns>True if the request has been sent successfully, otherwise false.</returns>
16-
bool SendExportRequest(TRequest request, CancellationToken cancellationToken = default);
17+
/// <returns><see cref="ExportClientResponse"/>.</returns>
18+
ExportClientResponse SendExportRequest(TRequest request, CancellationToken cancellationToken = default);
1719

1820
/// <summary>
1921
/// Method for shutting down the export client.

src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/ExportClient/OtlpGrpcLogExportClient.cs

+5-4
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,22 @@ public OtlpGrpcLogExportClient(OtlpExporterOptions options, OtlpCollector.LogsSe
2626
}
2727

2828
/// <inheritdoc/>
29-
public override bool SendExportRequest(OtlpCollector.ExportLogsServiceRequest request, CancellationToken cancellationToken = default)
29+
public override ExportClientResponse SendExportRequest(OtlpCollector.ExportLogsServiceRequest request, CancellationToken cancellationToken = default)
3030
{
3131
var deadline = DateTime.UtcNow.AddMilliseconds(this.TimeoutMilliseconds);
3232

3333
try
3434
{
3535
this.logsClient.Export(request, headers: this.Headers, deadline: deadline, cancellationToken: cancellationToken);
36+
37+
// We do not need to return back response and deadline for successful response so using cached value.
38+
return SuccessExportResponse;
3639
}
3740
catch (RpcException ex)
3841
{
3942
OpenTelemetryProtocolExporterEventSource.Log.FailedToReachCollector(this.Endpoint, ex);
4043

41-
return false;
44+
return new ExportClientGrpcResponse(success: false, deadlineUtc: deadline, exception: ex);
4245
}
43-
44-
return true;
4546
}
4647
}

src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/ExportClient/OtlpGrpcMetricsExportClient.cs

+5-4
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,22 @@ public OtlpGrpcMetricsExportClient(OtlpExporterOptions options, OtlpCollector.Me
2626
}
2727

2828
/// <inheritdoc/>
29-
public override bool SendExportRequest(OtlpCollector.ExportMetricsServiceRequest request, CancellationToken cancellationToken = default)
29+
public override ExportClientResponse SendExportRequest(OtlpCollector.ExportMetricsServiceRequest request, CancellationToken cancellationToken = default)
3030
{
3131
var deadline = DateTime.UtcNow.AddMilliseconds(this.TimeoutMilliseconds);
3232

3333
try
3434
{
3535
this.metricsClient.Export(request, headers: this.Headers, deadline: deadline, cancellationToken: cancellationToken);
36+
37+
// We do not need to return back response and deadline for successful response so using cached value.
38+
return SuccessExportResponse;
3639
}
3740
catch (RpcException ex)
3841
{
3942
OpenTelemetryProtocolExporterEventSource.Log.FailedToReachCollector(this.Endpoint, ex);
4043

41-
return false;
44+
return new ExportClientGrpcResponse(success: false, deadlineUtc: deadline, exception: ex);
4245
}
43-
44-
return true;
4546
}
4647
}

src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/ExportClient/OtlpGrpcTraceExportClient.cs

+5-4
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,22 @@ public OtlpGrpcTraceExportClient(OtlpExporterOptions options, OtlpCollector.Trac
2626
}
2727

2828
/// <inheritdoc/>
29-
public override bool SendExportRequest(OtlpCollector.ExportTraceServiceRequest request, CancellationToken cancellationToken = default)
29+
public override ExportClientResponse SendExportRequest(OtlpCollector.ExportTraceServiceRequest request, CancellationToken cancellationToken = default)
3030
{
3131
var deadline = DateTime.UtcNow.AddMilliseconds(this.TimeoutMilliseconds);
3232

3333
try
3434
{
3535
this.traceClient.Export(request, headers: this.Headers, deadline: deadline, cancellationToken: cancellationToken);
36+
37+
// We do not need to return back response and deadline for successful response so using cached value.
38+
return SuccessExportResponse;
3639
}
3740
catch (RpcException ex)
3841
{
3942
OpenTelemetryProtocolExporterEventSource.Log.FailedToReachCollector(this.Endpoint, ex);
4043

41-
return false;
44+
return new ExportClientGrpcResponse(success: false, deadlineUtc: deadline, exception: ex);
4245
}
43-
44-
return true;
4546
}
4647
}

src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/Retry/OtlpExporterTransmissionHandler.cs

-76
This file was deleted.

src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpLogExporter.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public override ExportResult Export(in Batch<LogRecord> logRecordBatch)
8989
{
9090
request = this.otlpLogRecordTransformer.BuildExportRequest(this.ProcessResource, logRecordBatch);
9191

92-
if (!this.exportClient.SendExportRequest(request))
92+
if (!this.exportClient.SendExportRequest(request).Success)
9393
{
9494
return ExportResult.Failure;
9595
}

src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpMetricExporter.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public override ExportResult Export(in Batch<Metric> metrics)
7272
{
7373
request.AddMetrics(this.ProcessResource, metrics);
7474

75-
if (!this.exportClient.SendExportRequest(request))
75+
if (!this.exportClient.SendExportRequest(request).Success)
7676
{
7777
return ExportResult.Failure;
7878
}

src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpTraceExporter.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public override ExportResult Export(in Batch<Activity> activityBatch)
8080
{
8181
request.AddBatch(this.sdkLimitOptions, this.ProcessResource, activityBatch);
8282

83-
if (!this.exportClient.SendExportRequest(request))
83+
if (!this.exportClient.SendExportRequest(request).Success)
8484
{
8585
return ExportResult.Failure;
8686
}

test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/Implementation/ExportClient/OtlpHttpTraceExportClientTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ void RunTest(Batch<Activity> batch)
135135
var httpRequest = testHttpHandler.HttpRequestMessage;
136136

137137
// Assert
138-
Assert.True(result);
138+
Assert.True(result.Success);
139139
Assert.NotNull(httpRequest);
140140
Assert.Equal(HttpMethod.Post, httpRequest.Method);
141141
Assert.Equal("http://localhost:4317/", httpRequest.RequestUri.AbsoluteUri);

test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/TestExportClient.cs

+10-2
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,28 @@ internal class TestExportClient<T>(bool throwException = false) : IExportClient<
1313

1414
public bool ThrowException { get; set; } = throwException;
1515

16-
public bool SendExportRequest(T request, CancellationToken cancellationToken = default)
16+
public ExportClientResponse SendExportRequest(T request, CancellationToken cancellationToken = default)
1717
{
1818
if (this.ThrowException)
1919
{
2020
throw new Exception("Exception thrown from SendExportRequest");
2121
}
2222

2323
this.SendExportRequestCalled = true;
24-
return true;
24+
return new TestExportClientResponse(true, null, null);
2525
}
2626

2727
public bool Shutdown(int timeoutMilliseconds)
2828
{
2929
this.ShutdownCalled = true;
3030
return true;
3131
}
32+
33+
private class TestExportClientResponse : ExportClientResponse
34+
{
35+
public TestExportClientResponse(bool success, DateTime? deadline, Exception exception)
36+
: base(success, deadline, exception)
37+
{
38+
}
39+
}
3240
}

0 commit comments

Comments
 (0)