-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Add transmit from storage handler #35017
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
Merged
vishweshbankwar
merged 7 commits into
main
from
vibankwa/add-transmit-from-storage-handler
Mar 24, 2023
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
100 changes: 100 additions & 0 deletions
100
sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/TransmitFromStorageHandler.cs
This file contains hidden or 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,100 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System; | ||
| using System.Threading; | ||
| using System.Timers; | ||
| using OpenTelemetry; | ||
| using OpenTelemetry.Extensions.PersistentStorage.Abstractions; | ||
|
|
||
| namespace Azure.Monitor.OpenTelemetry.Exporter.Internals | ||
| { | ||
| internal class TransmitFromStorageHandler : IDisposable | ||
| { | ||
| private readonly ApplicationInsightsRestClient _applicationInsightsRestClient; | ||
| private readonly PersistentBlobProvider _blobProvider; | ||
| private readonly TransmissionStateManager _transmissionStateManager; | ||
| private readonly System.Timers.Timer _transmitFromStorageTimer; | ||
| private bool _disposed; | ||
|
|
||
| internal TransmitFromStorageHandler(ApplicationInsightsRestClient applicationInsightsRestClient, PersistentBlobProvider blobProvider, TransmissionStateManager transmissionStateManager) | ||
| { | ||
| _applicationInsightsRestClient = applicationInsightsRestClient; | ||
| _blobProvider = blobProvider; | ||
| _transmissionStateManager = transmissionStateManager; | ||
| _transmitFromStorageTimer = new System.Timers.Timer(); | ||
| _transmitFromStorageTimer.Elapsed += TransmitFromStorage; | ||
| _transmitFromStorageTimer.AutoReset = true; | ||
| _transmitFromStorageTimer.Interval = 120000; | ||
|
rajkumar-rangaraj marked this conversation as resolved.
|
||
| _transmitFromStorageTimer.Start(); | ||
| } | ||
|
|
||
| internal void TransmitFromStorage(object? sender, ElapsedEventArgs? e) | ||
| { | ||
| // Only proces 10 files at a time so that we don't end up taking lot of cpu | ||
| // if the number of files are large. | ||
| int fileCount = 10; | ||
| while (fileCount > 0) | ||
| { | ||
| if (_transmissionStateManager.State == TransmissionState.Closed && _blobProvider.TryGetBlob(out var blob) && blob.TryLease(120000)) | ||
| { | ||
| try | ||
| { | ||
| blob.TryRead(out var data); | ||
|
|
||
| using var httpMessage = _applicationInsightsRestClient.InternalTrackAsync(data, CancellationToken.None).Result; | ||
| var result = HttpPipelineHelper.IsSuccess(httpMessage); | ||
|
|
||
| if (result == ExportResult.Success) | ||
| { | ||
| _transmissionStateManager.ResetConsecutiveErrors(); | ||
| _transmissionStateManager.CloseTransmission(); | ||
|
|
||
| AzureMonitorExporterEventSource.Log.WriteInformational("TransmitFromStorageSuccess", "Successfully transmitted a blob from storage."); | ||
|
|
||
| // In case if the delete fails, there is a possibility | ||
| // that the current batch will be transmitted more than once resulting in duplicates. | ||
| blob.TryDelete(); | ||
| } | ||
| else | ||
| { | ||
| _transmissionStateManager.EnableBackOff(httpMessage.Response); | ||
| HttpPipelineHelper.HandleFailures(httpMessage, blob, _blobProvider); | ||
| break; | ||
| } | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| AzureMonitorExporterEventSource.Log.WriteError("FailedToTransmitFromStorage", ex); | ||
| } | ||
| } | ||
| else | ||
| { | ||
| break; | ||
| } | ||
|
|
||
| fileCount--; | ||
| } | ||
| } | ||
|
|
||
| protected virtual void Dispose(bool disposing) | ||
| { | ||
| if (!_disposed) | ||
| { | ||
| if (disposing) | ||
| { | ||
| _transmitFromStorageTimer?.Dispose(); | ||
| } | ||
|
|
||
| _disposed = true; | ||
| } | ||
| } | ||
|
|
||
| public void Dispose() | ||
| { | ||
| // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method | ||
| Dispose(disposing: true); | ||
| GC.SuppressFinalize(this); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.