From b8c37cfd24a03400e756c02006b5907efd976544 Mon Sep 17 00:00:00 2001 From: Andreas Gehrke Date: Sun, 16 Nov 2025 21:08:52 +0100 Subject: [PATCH 1/2] Avoid unneeded resizes of List when adding from a Batch --- .../src/Internals/LogsHelper.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/LogsHelper.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/LogsHelper.cs index eb4e2b23e719..71d2e851b588 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/LogsHelper.cs +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/LogsHelper.cs @@ -52,7 +52,7 @@ internal static class LogsHelper internal static (List TelemetryItems, TelemetrySchemaTypeCounter TelemetrySchemaTypeCounter) OtelToAzureMonitorLogs(Batch batchLogRecord, AzureMonitorResource? resource, string instrumentationKey) { - List telemetryItems = new List(); + List telemetryItems = new List(capacity: (int)batchLogRecord.Count); var telemetrySchemaTypeCounter = new TelemetrySchemaTypeCounter(); TelemetryItem telemetryItem; From 283109b741016341e623d56b4fb479a2f263f6e5 Mon Sep 17 00:00:00 2001 From: Andreas Gehrke Date: Sun, 16 Nov 2025 21:10:01 +0100 Subject: [PATCH 2/2] Remove uneeded mem copy in NDJsonWriter --- .../src/Internals/NDJsonWriter.cs | 25 ++++++++++--------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/NDJsonWriter.cs b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/NDJsonWriter.cs index a70c3f4c4ae1..4740e6168255 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/NDJsonWriter.cs +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/NDJsonWriter.cs @@ -2,7 +2,7 @@ // Licensed under the MIT License. using System; -using System.IO; +using System.Buffers; using System.Text.Json; namespace Azure.Monitor.OpenTelemetry.Exporter.Internals @@ -11,39 +11,40 @@ internal sealed class NDJsonWriter : IDisposable { private static readonly byte[] s_separator = { (byte)'\n' }; + private readonly Azure.Core.ArrayBufferWriter _buffer = new(); + public NDJsonWriter() { - Stream = new MemoryStream(); - JsonWriter = new Utf8JsonWriter(Stream, new JsonWriterOptions() { SkipValidation = true }); + JsonWriter = new Utf8JsonWriter(_buffer, new JsonWriterOptions() { SkipValidation = true }); } - private MemoryStream Stream { get; } public Utf8JsonWriter JsonWriter { get; } public void WriteNewLine() { JsonWriter.Flush(); JsonWriter.Reset(); - Stream.Write(s_separator, 0, 1); + _buffer.Write(s_separator); } - public Memory ToBytes() + public ReadOnlyMemory ToBytes() { JsonWriter.Flush(); - return Stream.ToArray(); + return _buffer.WrittenMemory; } public override string ToString() { - Stream.Position = 0; - using var streamReader = new StreamReader(Stream); - return streamReader.ReadToEnd(); +#if NET8_0_OR_GREATER + return System.Text.Encoding.UTF8.GetString(_buffer.WrittenSpan); +#else + return System.Text.Encoding.UTF8.GetString(_buffer.WrittenSpan.ToArray()); +#endif } public void Dispose() { - Stream?.Dispose(); - JsonWriter?.Dispose(); + JsonWriter.Dispose(); } } }