Skip to content

Commit c243345

Browse files
[Exporter.Prometheus] Avoid resizing
Increase the initial size of the buffer and remember the high-water mark for the allocation size to avoid try-catch to resize large tag buffers.
1 parent 1379395 commit c243345

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

src/OpenTelemetry.Exporter.Prometheus.HttpListener/Internal/Shared/Serialization/TextFormatSerializer.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ internal abstract class TextFormatSerializer
101101

102102
private string[]? reservedExemplarOutputKeys;
103103

104+
private int serializedTagsBufferHint = 256;
105+
104106
public static OpenMetricsV0Serializer OpenMetricsV0 => field ??= new();
105107

106108
public static OpenMetricsV1Serializer OpenMetricsV1 => field ??= new();
@@ -987,7 +989,7 @@ internal byte[] SerializeTagsToPooledBuffer(
987989
out int length)
988990
{
989991
var pool = ArrayPool<byte>.Shared;
990-
var buffer = pool.Rent(128);
992+
var buffer = pool.Rent(Volatile.Read(ref this.serializedTagsBufferHint));
991993

992994
while (true)
993995
{
@@ -1009,6 +1011,11 @@ internal byte[] SerializeTagsToPooledBuffer(
10091011

10101012
length = cursor;
10111013

1014+
if (buffer.Length > Volatile.Read(ref this.serializedTagsBufferHint))
1015+
{
1016+
Volatile.Write(ref this.serializedTagsBufferHint, buffer.Length);
1017+
}
1018+
10121019
return buffer;
10131020
}
10141021
catch (Exception ex) when (ex is IndexOutOfRangeException or ArgumentException)

0 commit comments

Comments
 (0)