Skip to content

Commit ea70cd5

Browse files
authored
[Exporter.OpenTelemetry.Protocol] update PersistentStorage (#6249)
1 parent ab32e39 commit ea70cd5

File tree

8 files changed

+23
-31
lines changed

8 files changed

+23
-31
lines changed

src/OpenTelemetry.Exporter.OpenTelemetryProtocol/PersistentStorage/DirectorySizeTracker.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,16 @@ internal static long CalculateFolderSize(string path)
5656
long directorySize = 0;
5757
try
5858
{
59-
foreach (string file in Directory.EnumerateFiles(path))
59+
foreach (var file in Directory.EnumerateFiles(path))
6060
{
6161
if (File.Exists(file))
6262
{
63-
FileInfo fileInfo = new FileInfo(file);
63+
var fileInfo = new FileInfo(file);
6464
directorySize += fileInfo.Length;
6565
}
6666
}
6767

68-
foreach (string dir in Directory.GetDirectories(path))
68+
foreach (var dir in Directory.GetDirectories(path))
6969
{
7070
directorySize += CalculateFolderSize(dir);
7171
}

src/OpenTelemetry.Exporter.OpenTelemetryProtocol/PersistentStorage/FileBlob.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ protected override bool OnTryWrite(byte[] buffer, int leasePeriodMilliseconds =
5858
{
5959
Guard.ThrowIfNull(buffer);
6060

61-
string path = this.FullPath + ".tmp";
61+
var path = this.FullPath + ".tmp";
6262

6363
try
6464
{

src/OpenTelemetry.Exporter.OpenTelemetryProtocol/PersistentStorage/FileBlobProvider.cs

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System.Timers;
66
using OpenTelemetry.Internal;
77
using OpenTelemetry.PersistentStorage.Abstractions;
8-
using Timer = System.Timers.Timer;
98

109
namespace OpenTelemetry.PersistentStorage.FileSystem;
1110

@@ -24,7 +23,7 @@ public class FileBlobProvider : PersistentBlobProvider, IDisposable
2423
private readonly DirectorySizeTracker directorySizeTracker;
2524
private readonly long retentionPeriodInMilliseconds;
2625
private readonly int writeTimeoutInMilliseconds;
27-
private readonly Timer maintenanceTimer;
26+
private readonly System.Timers.Timer maintenanceTimer;
2827
private bool disposedValue;
2928

3029
/// <summary>
@@ -61,7 +60,7 @@ public class FileBlobProvider : PersistentBlobProvider, IDisposable
6160
/// path exceeds system defined maximum length.
6261
/// </exception>
6362
/// <exception cref="UnauthorizedAccessException">
64-
/// insufficient priviledges for provided path.
63+
/// insufficient privileges for provided path.
6564
/// </exception>
6665
/// <exception cref="NotSupportedException">
6766
/// path contains a colon character (:) that is not part of a drive label ("C:\").
@@ -87,7 +86,7 @@ public FileBlobProvider(
8786
this.retentionPeriodInMilliseconds = retentionPeriodInMilliseconds;
8887
this.writeTimeoutInMilliseconds = writeTimeoutInMilliseconds;
8988

90-
this.maintenanceTimer = new Timer(maintenancePeriodInMilliseconds);
89+
this.maintenanceTimer = new System.Timers.Timer(maintenancePeriodInMilliseconds);
9190
this.maintenanceTimer.Elapsed += this.OnMaintenanceEvent;
9291
this.maintenanceTimer.AutoReset = true;
9392
this.maintenanceTimer.Enabled = true;
@@ -105,7 +104,7 @@ protected override IEnumerable<PersistentBlob> OnGetBlobs()
105104

106105
foreach (var file in Directory.EnumerateFiles(this.DirectoryPath, "*.blob", SearchOption.TopDirectoryOnly).OrderByDescending(f => f))
107106
{
108-
DateTime fileDateTime = PersistentStorageHelper.GetDateTimeFromBlobName(file);
107+
var fileDateTime = PersistentStorageHelper.GetDateTimeFromBlobName(file);
109108
if (fileDateTime > retentionDeadline)
110109
{
111110
yield return new FileBlob(file, this.directorySizeTracker);
@@ -174,7 +173,7 @@ private void OnMaintenanceEvent(object? source, ElapsedEventArgs e)
174173

175174
private bool CheckStorageSize()
176175
{
177-
if (!this.directorySizeTracker.IsSpaceAvailable(out long size))
176+
if (!this.directorySizeTracker.IsSpaceAvailable(out var size))
178177
{
179178
// TODO: check accuracy of size reporting.
180179
PersistentStorageEventSource.Log.PersistentStorageWarning(
@@ -186,7 +185,7 @@ private bool CheckStorageSize()
186185
return true;
187186
}
188187

189-
private PersistentBlob? CreateFileBlob(byte[] buffer, int leasePeriodMilliseconds = 0)
188+
private FileBlob? CreateFileBlob(byte[] buffer, int leasePeriodMilliseconds = 0)
190189
{
191190
if (!this.CheckStorageSize())
192191
{
@@ -198,14 +197,7 @@ private bool CheckStorageSize()
198197
var blobFilePath = Path.Combine(this.DirectoryPath, PersistentStorageHelper.GetUniqueFileName(".blob"));
199198
var blob = new FileBlob(blobFilePath, this.directorySizeTracker);
200199

201-
if (blob.TryWrite(buffer, leasePeriodMilliseconds))
202-
{
203-
return blob;
204-
}
205-
else
206-
{
207-
return null;
208-
}
200+
return blob.TryWrite(buffer, leasePeriodMilliseconds) ? blob : null;
209201
}
210202
catch (Exception ex)
211203
{

src/OpenTelemetry.Exporter.OpenTelemetryProtocol/PersistentStorage/PersistentBlobProvider.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,12 @@ public IEnumerable<PersistentBlob> GetBlobs()
102102
{
103103
try
104104
{
105-
return this.OnGetBlobs() ?? Enumerable.Empty<PersistentBlob>();
105+
return this.OnGetBlobs() ?? [];
106106
}
107107
catch (Exception ex)
108108
{
109109
PersistentStorageAbstractionsEventSource.Log.PersistentStorageAbstractionsException(nameof(PersistentBlobProvider), "Failed to get all the blobs", ex);
110-
return Enumerable.Empty<PersistentBlob>();
110+
return [];
111111
}
112112
}
113113

src/OpenTelemetry.Exporter.OpenTelemetryProtocol/PersistentStorage/PersistentStorageAbstractionsEventSource.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace OpenTelemetry.PersistentStorage.Abstractions;
99
[EventSource(Name = EventSourceName)]
1010
internal sealed class PersistentStorageAbstractionsEventSource : EventSource
1111
{
12-
public static readonly PersistentStorageAbstractionsEventSource Log = new PersistentStorageAbstractionsEventSource();
12+
public static PersistentStorageAbstractionsEventSource Log = new PersistentStorageAbstractionsEventSource();
1313
#if BUILDING_INTERNAL_PERSISTENT_STORAGE
1414
private const string EventSourceName = "OpenTelemetry-PersistentStorage-Abstractions-Otlp";
1515
#else

src/OpenTelemetry.Exporter.OpenTelemetryProtocol/PersistentStorage/PersistentStorageEventSource.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace OpenTelemetry.PersistentStorage.FileSystem;
99
[EventSource(Name = EventSourceName)]
1010
internal sealed class PersistentStorageEventSource : EventSource
1111
{
12-
public static readonly PersistentStorageEventSource Log = new PersistentStorageEventSource();
12+
public static PersistentStorageEventSource Log = new PersistentStorageEventSource();
1313
#if BUILDING_INTERNAL_PERSISTENT_STORAGE
1414
private const string EventSourceName = "OpenTelemetry-PersistentStorage-FileSystem-Otlp";
1515
#else

src/OpenTelemetry.Exporter.OpenTelemetryProtocol/PersistentStorage/PersistentStorageHelper.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ internal static void RemoveExpiredBlob(DateTime retentionDeadline, string filePa
1212
{
1313
if (filePath.EndsWith(".blob", StringComparison.OrdinalIgnoreCase))
1414
{
15-
DateTime fileDateTime = GetDateTimeFromBlobName(filePath);
15+
var fileDateTime = GetDateTimeFromBlobName(filePath);
1616
if (fileDateTime < retentionDeadline)
1717
{
1818
try
@@ -30,11 +30,11 @@ internal static void RemoveExpiredBlob(DateTime retentionDeadline, string filePa
3030

3131
internal static bool RemoveExpiredLease(DateTime leaseDeadline, string filePath)
3232
{
33-
bool success = false;
33+
var success = false;
3434

3535
if (filePath.EndsWith(".lock", StringComparison.OrdinalIgnoreCase))
3636
{
37-
DateTime fileDateTime = GetDateTimeFromLeaseName(filePath);
37+
var fileDateTime = GetDateTimeFromLeaseName(filePath);
3838
if (fileDateTime < leaseDeadline)
3939
{
4040
var newFilePath = filePath.Substring(0, filePath.LastIndexOf('@'));
@@ -55,11 +55,11 @@ internal static bool RemoveExpiredLease(DateTime leaseDeadline, string filePath)
5555

5656
internal static bool RemoveTimedOutTmpFiles(DateTime timeoutDeadline, string filePath)
5757
{
58-
bool success = false;
58+
var success = false;
5959

6060
if (filePath.EndsWith(".tmp", StringComparison.OrdinalIgnoreCase))
6161
{
62-
DateTime fileDateTime = GetDateTimeFromBlobName(filePath);
62+
var fileDateTime = GetDateTimeFromBlobName(filePath);
6363
if (fileDateTime < timeoutDeadline)
6464
{
6565
try
@@ -144,7 +144,7 @@ internal static DateTime GetDateTimeFromLeaseName(string filePath)
144144
{
145145
var fileName = Path.GetFileNameWithoutExtension(filePath);
146146
var startIndex = fileName.LastIndexOf('@') + 1;
147-
var time = fileName.Substring(startIndex, fileName.Length - startIndex);
147+
var time = fileName.Substring(startIndex);
148148
DateTime.TryParseExact(time, "yyyy-MM-ddTHHmmss.fffffffZ", CultureInfo.InvariantCulture, DateTimeStyles.None, out var dateTime);
149149
return dateTime.ToUniversalTime();
150150
}

src/OpenTelemetry.Exporter.OpenTelemetryProtocol/PersistentStorage/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# Persistent Storage APIs for OTLP Exporter
22

33
The files in this folder have been copied over from
4-
[OpenTelemetry.PersistentStorage.Abstractions](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/tree/58607b7cdeb15207027a6fa4ca56e7fac897bda4/src/OpenTelemetry.PersistentStorage.Abstractions)
4+
[OpenTelemetry.PersistentStorage.Abstractions](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/tree/1be4157075ad09e13aa54b8e5845d2310bd82673/src/OpenTelemetry.PersistentStorage.Abstractions)
55
and
6-
[OpenTelemetry.PersistentStorage.FileSystem](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/tree/58607b7cdeb15207027a6fa4ca56e7fac897bda4/src/OpenTelemetry.PersistentStorage.FileSystem).
6+
[OpenTelemetry.PersistentStorage.FileSystem](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/tree/1be4157075ad09e13aa54b8e5845d2310bd82673/src/OpenTelemetry.PersistentStorage.FileSystem).
77
Any code changes in this folder MUST go through changes in the original location
88
i.e. in the contrib repo. Here is the sequence of steps to be followed when
99
making changes:

0 commit comments

Comments
 (0)