-
Notifications
You must be signed in to change notification settings - Fork 785
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Otlp Retry Part2 - Introduce transmission handler (#5367)
- Loading branch information
1 parent
c87134d
commit 7e0213d
Showing
11 changed files
with
195 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
...rter.OpenTelemetryProtocol/Implementation/Transmission/OtlpExporterTransmissionHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#nullable enable | ||
|
||
using System.Diagnostics; | ||
using OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation.ExportClient; | ||
using OpenTelemetry.Internal; | ||
|
||
namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation.Transmission; | ||
|
||
internal class OtlpExporterTransmissionHandler<TRequest> | ||
{ | ||
public OtlpExporterTransmissionHandler(IExportClient<TRequest> exportClient) | ||
{ | ||
Guard.ThrowIfNull(exportClient); | ||
|
||
this.ExportClient = exportClient; | ||
} | ||
|
||
protected IExportClient<TRequest> ExportClient { get; } | ||
|
||
/// <summary> | ||
/// Attempts to send an export request to the server. | ||
/// </summary> | ||
/// <param name="request">The request to send to the server.</param> | ||
/// <returns> <see langword="true" /> if the request is sent successfully; otherwise, <see | ||
/// langword="false" />. | ||
/// </returns> | ||
public bool TrySubmitRequest(TRequest request) | ||
{ | ||
try | ||
{ | ||
var response = this.ExportClient.SendExportRequest(request); | ||
if (response.Success) | ||
{ | ||
return true; | ||
} | ||
|
||
return this.OnSubmitRequestFailure(request, response); | ||
} | ||
catch (Exception ex) | ||
{ | ||
OpenTelemetryProtocolExporterEventSource.Log.TrySubmitRequestException(ex); | ||
return false; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Attempts to shutdown the transmission handler, blocks the current thread | ||
/// until shutdown completed or timed out. | ||
/// </summary> | ||
/// <param name="timeoutMilliseconds"> | ||
/// The number (non-negative) of milliseconds to wait, or | ||
/// <c>Timeout.Infinite</c> to wait indefinitely. | ||
/// </param> | ||
/// <returns> | ||
/// Returns <see langword="true" /> if shutdown succeeded; otherwise, <see | ||
/// langword="false" />. | ||
/// </returns> | ||
public bool Shutdown(int timeoutMilliseconds) | ||
{ | ||
Guard.ThrowIfInvalidTimeout(timeoutMilliseconds); | ||
|
||
var sw = timeoutMilliseconds == Timeout.Infinite ? null : Stopwatch.StartNew(); | ||
|
||
this.OnShutdown(timeoutMilliseconds); | ||
|
||
if (sw != null) | ||
{ | ||
var timeout = timeoutMilliseconds - sw.ElapsedMilliseconds; | ||
|
||
return this.ExportClient.Shutdown((int)Math.Max(timeout, 0)); | ||
} | ||
|
||
return this.ExportClient.Shutdown(timeoutMilliseconds); | ||
} | ||
|
||
/// <summary> | ||
/// Fired when the transmission handler is shutdown. | ||
/// </summary> | ||
/// <param name="timeoutMilliseconds"> | ||
/// The number (non-negative) of milliseconds to wait, or | ||
/// <c>Timeout.Infinite</c> to wait indefinitely. | ||
/// </param> | ||
protected virtual void OnShutdown(int timeoutMilliseconds) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Fired when a request could not be submitted. | ||
/// </summary> | ||
/// <param name="request">The request that was attempted to send to the server.</param> | ||
/// <param name="response"><see cref="ExportClientResponse" />.</param> | ||
/// <returns><see langword="true" /> If the request is resubmitted and succeeds; otherwise, <see | ||
/// langword="false" />.</returns> | ||
protected virtual bool OnSubmitRequestFailure(TRequest request, ExportClientResponse response) | ||
{ | ||
return false; | ||
} | ||
|
||
/// <summary> | ||
/// Fired when resending a request to the server. | ||
/// </summary> | ||
/// <param name="request">The request to be resent to the server.</param> | ||
/// <param name="response"><see cref="ExportClientResponse" />.</param> | ||
/// <returns><see langword="true" /> If the retry succeeds; otherwise, <see | ||
/// langword="false" />.</returns> | ||
protected bool TryRetryRequest(TRequest request, out ExportClientResponse response) | ||
{ | ||
response = this.ExportClient.SendExportRequest(request); | ||
if (!response.Success) | ||
{ | ||
OpenTelemetryProtocolExporterEventSource.Log.ExportMethodException(response.Exception, isRetry: true); | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.