Skip to content

Commit f412e47

Browse files
[Exporter.OTLP] Disk persistance - handle malformed files names (#7108)
Co-authored-by: Martin Costello <martin@martincostello.com>
1 parent 810fba4 commit f412e47

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

src/OpenTelemetry.Exporter.OpenTelemetryProtocol/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ Notes](../../RELEASENOTES.md).
77

88
## Unreleased
99

10+
* Fixed an issue in persistent storage cleanup where malformed `.blob`, `.tmp`,
11+
or `.lock` filenames could throw and interrupt maintenance.
12+
([#7108](https://github.com/open-telemetry/opentelemetry-dotnet/pull/7108))
13+
1014
* **Breaking change:** Fixed an insecure disk retry default. Disk retry now
1115
requires `OTEL_DOTNET_EXPERIMENTAL_OTLP_DISK_RETRY_DIRECTORY_PATH` when
1216
`OTEL_DOTNET_EXPERIMENTAL_OTLP_RETRY=disk` is configured. The exporter no

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,13 @@ internal static bool RemoveExpiredLease(DateTime leaseDeadline, string filePath)
4040
var fileDateTime = GetDateTimeFromLeaseName(filePath);
4141
if (fileDateTime < leaseDeadline)
4242
{
43-
var newFilePath = filePath.Substring(0, filePath.LastIndexOf('@'));
43+
var atSignIndex = filePath.LastIndexOf('@');
44+
if (atSignIndex == -1)
45+
{
46+
return false;
47+
}
48+
49+
var newFilePath = filePath.Substring(0, atSignIndex);
4450
try
4551
{
4652
File.Move(filePath, newFilePath);
@@ -132,7 +138,13 @@ internal static string CreateSubdirectory(string path)
132138
internal static DateTime GetDateTimeFromBlobName(string filePath)
133139
{
134140
var fileName = GetFileNameWithoutExtension(filePath);
135-
var timestamp = fileName.Substring(0, fileName.LastIndexOf('-'));
141+
var dashIndex = fileName.LastIndexOf('-');
142+
if (dashIndex == -1)
143+
{
144+
return DateTime.SpecifyKind(DateTime.MinValue, DateTimeKind.Utc);
145+
}
146+
147+
var timestamp = fileName.Substring(0, dashIndex);
136148

137149
return Parse(timestamp);
138150
}

test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/PersistentStorage/PersistentStorageHelperTests.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public void GetDateTimeFromBlobName_WithDifferentPathFormats_ReturnsCorrectDateT
4949
[InlineData("invalid-format.blob")]
5050
[InlineData("2024-01-15T14:30:25Z-abc123.blob")]
5151
[InlineData("abc-def.blob")]
52+
[InlineData("invalidformat.blob")]
5253
public void GetDateTimeFromBlobName_InvalidFormat_ReturnsDateTimeMinValue(string filePath)
5354
{
5455
var result = PersistentStorageHelper.GetDateTimeFromBlobName(filePath);
@@ -217,4 +218,14 @@ public void GetDateTimeFromLeaseName_ConsistentResults_ForMultipleCalls(string f
217218
Assert.Equal(result1a, result1b);
218219
Assert.NotEqual(result1a, result2);
219220
}
221+
222+
[Fact]
223+
public void RemoveExpiredLease_WithoutLeaseDelimiter_ReturnsFalse()
224+
{
225+
var leaseDeadline = DateTime.UtcNow;
226+
227+
var result = PersistentStorageHelper.RemoveExpiredLease(leaseDeadline, "invalid-format.lock");
228+
229+
Assert.False(result);
230+
}
220231
}

0 commit comments

Comments
 (0)