Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Otlp] Add persistent storage transmission handler #5460

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#nullable enable

using Google.Protobuf;
using OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation.ExportClient;
using OpenTelemetry.PersistentStorage.Abstractions;
using OpenTelemetry.PersistentStorage.FileSystem;
using OpenTelemetry.Proto.Collector.Logs.V1;
using OpenTelemetry.Proto.Collector.Metrics.V1;
using OpenTelemetry.Proto.Collector.Trace.V1;

namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation.Transmission;

internal sealed class OtlpExporterPersistentStorageTransmissionHandler<TRequest> : OtlpExporterTransmissionHandler<TRequest>, IDisposable
{
internal int RetryInterval = 60000;
private readonly ManualResetEvent stopEvent = new(false);
private readonly Thread thread;
private readonly PersistentBlobProvider persistentBlobProvider;
private readonly Func<byte[], TRequest>? requestFactory;
vishweshbankwar marked this conversation as resolved.
Show resolved Hide resolved
private bool disposed;

public OtlpExporterPersistentStorageTransmissionHandler(IExportClient<TRequest> exportClient, double timeoutMilliseconds, Func<byte[], TRequest> requestFactory, string storagePath)
: this(new FileBlobProvider(storagePath), exportClient, timeoutMilliseconds, requestFactory)
{
}

internal OtlpExporterPersistentStorageTransmissionHandler(PersistentBlobProvider persistentBlobProvider, IExportClient<TRequest> exportClient, double timeoutMilliseconds, Func<byte[], TRequest> requestFactory)
: base(exportClient, timeoutMilliseconds)
{
this.persistentBlobProvider = persistentBlobProvider;
this.requestFactory = requestFactory;
vishweshbankwar marked this conversation as resolved.
Show resolved Hide resolved

this.thread = new Thread(this.RetryStoredRequests)
vishweshbankwar marked this conversation as resolved.
Show resolved Hide resolved
{
Name = $"OtlpExporter Persistent Retry Storage - {typeof(TRequest)}",
IsBackground = true,
};

this.thread.Start();
}

protected override bool OnSubmitRequestFailure(TRequest request, ExportClientResponse response)
{
if (RetryHelper.ShouldRetryRequest(request, response, OtlpRetry.InitialBackoffMilliseconds, out _))
{
byte[]? data = null;
if (request is ExportTraceServiceRequest traceRequest)
{
data = traceRequest.ToByteArray();
}
else if (request is ExportMetricsServiceRequest metricsRequest)
{
data = metricsRequest.ToByteArray();
}
else if (request is ExportLogsServiceRequest logsRequest)
{
data = logsRequest.ToByteArray();
}
vishweshbankwar marked this conversation as resolved.
Show resolved Hide resolved

if (data != null)
{
this.persistentBlobProvider?.TryCreateBlob(data, out _);
vishweshbankwar marked this conversation as resolved.
Show resolved Hide resolved
}

return true;
vishweshbankwar marked this conversation as resolved.
Show resolved Hide resolved
}

return false;
}

protected override void OnShutdown(int timeoutMilliseconds)
{
this.stopEvent.Set();
this.thread.Join(timeoutMilliseconds);
base.OnShutdown(timeoutMilliseconds);
vishweshbankwar marked this conversation as resolved.
Show resolved Hide resolved
}

protected override void Dispose(bool disposing)
utpilla marked this conversation as resolved.
Show resolved Hide resolved
{
if (!this.disposed)
{
if (disposing)
{
this.stopEvent.Dispose();
(this.persistentBlobProvider as FileBlobProvider)?.Dispose();
vishweshbankwar marked this conversation as resolved.
Show resolved Hide resolved
}

this.disposed = true;
}
}

private void RetryStoredRequests()
{
while (true)
{
// Wait 60 seconds before retrying
if (this.stopEvent.WaitOne(this.RetryInterval))
{
break;
}

int fileCount = 0;

// Transmit 10 files at a time.
while (fileCount < 10 && !this.stopEvent.WaitOne(0))
{
if (this.persistentBlobProvider != null && this.persistentBlobProvider.TryGetBlob(out var blob))
{
if (blob != null && blob.TryLease((int)this.TimeoutMilliseconds) && blob.TryRead(out var data))
{
if (this.requestFactory != null)
{
var deadlineUtc = DateTime.UtcNow.AddMilliseconds(this.TimeoutMilliseconds);
var request = this.requestFactory.Invoke(data);
if (this.TryRetryRequest(request, deadlineUtc, out var response) || !RetryHelper.ShouldRetryRequest(request, response, OtlpRetry.InitialBackoffMilliseconds, out _))
{
blob.TryDelete();
}
}
}
}
else
{
break;
}

fileCount++;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation.Transmission;

internal class OtlpExporterTransmissionHandler<TRequest>
internal class OtlpExporterTransmissionHandler<TRequest> : IDisposable
{
public OtlpExporterTransmissionHandler(IExportClient<TRequest> exportClient, double timeoutMilliseconds)
{
Expand Down Expand Up @@ -80,6 +80,13 @@ public bool Shutdown(int timeoutMilliseconds)
return this.ExportClient.Shutdown(timeoutMilliseconds);
}

/// <inheritdoc/>
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}

/// <summary>
/// Fired when the transmission handler is shutdown.
/// </summary>
Expand Down Expand Up @@ -122,4 +129,16 @@ protected bool TryRetryRequest(TRequest request, DateTime deadlineUtc, out Expor

return true;
}

/// <summary>
/// Releases the unmanaged resources used by this class and optionally
/// releases the managed resources.
/// </summary>
/// <param name="disposing">
/// <see langword="true"/> to release both managed and unmanaged resources;
/// <see langword="false"/> to release only unmanaged resources.
/// </param>
protected virtual void Dispose(bool disposing)
{
}
}
Loading
Loading