Skip to content

Commit

Permalink
[sdk] Support split reads in OTEL_DIAGNOSTICS.json config file parser…
Browse files Browse the repository at this point in the history
… logic (#5884)

Co-authored-by: Piotr Kiełkowicz <[email protected]>
  • Loading branch information
rajkumar-rangaraj and Kielek authored Oct 8, 2024
1 parent a55a5ac commit 66c2e4b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
19 changes: 15 additions & 4 deletions src/OpenTelemetry/Internal/SelfDiagnosticsConfigParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,21 @@ public bool TryGetConfiguration(
this.configBuffer = buffer;
}

// TODO: Fix CA2022 - Avoid inexact read with 'System.IO.FileStream.Read(byte[], int, int)'
// Added _ = as a workaround to suppress the warning
_ = file.Read(buffer, 0, buffer.Length);
string configJson = Encoding.UTF8.GetString(buffer);
int bytesRead = 0;
int totalBytesRead = 0;

while (totalBytesRead < buffer.Length)
{
bytesRead = file.Read(buffer, totalBytesRead, buffer.Length - totalBytesRead);
if (bytesRead == 0)
{
break;
}

totalBytesRead += bytesRead;
}

string configJson = Encoding.UTF8.GetString(buffer, 0, totalBytesRead);

if (!TryParseLogDirectory(configJson, out logDirectory))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,20 @@ public void SelfDiagnosticsEventListener_EmitEvent_OmitAsConfigured()
using FileStream file = File.Open(LOGFILEPATH, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete);
var buffer = new byte[256];

// Suppress CA2022 error: Avoid inexact read with 'System.IO.FileStream.Read(byte[], int, int)'
_ = file.Read(buffer, 0, buffer.Length);
int bytesRead = 0;
int totalBytesRead = 0;

while (totalBytesRead < buffer.Length)
{
bytesRead = file.Read(buffer, totalBytesRead, buffer.Length - totalBytesRead);
if (bytesRead == 0)
{
break;
}

totalBytesRead += bytesRead;
}

Assert.Equal('\0', (char)buffer[0]);
}

Expand Down Expand Up @@ -259,9 +271,21 @@ private static void AssertFileOutput(string filePath, string eventMessage)
using FileStream file = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete);
var buffer = new byte[256];

// Suppress CA2022 error: Avoid inexact read with 'System.IO.FileStream.Read(byte[], int, int)'
_ = file.Read(buffer, 0, buffer.Length);
string logLine = Encoding.UTF8.GetString(buffer);
int bytesRead = 0;
int totalBytesRead = 0;

while (totalBytesRead < buffer.Length)
{
bytesRead = file.Read(buffer, totalBytesRead, buffer.Length - totalBytesRead);
if (bytesRead == 0)
{
break;
}

totalBytesRead += bytesRead;
}

string logLine = Encoding.UTF8.GetString(buffer, 0, totalBytesRead);
string logMessage = ParseLogMessage(logLine);
Assert.StartsWith(eventMessage, logMessage);
}
Expand Down

0 comments on commit 66c2e4b

Please sign in to comment.