Skip to content

Commit 1f39623

Browse files
[otlp] Avoid filling new buffer on expansion (open-telemetry#6019)
1 parent 75a683e commit 1f39623

File tree

4 files changed

+29
-18
lines changed

4 files changed

+29
-18
lines changed

src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/Serializer/ProtobufOtlpLogSerializer.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@ internal static class ProtobufOtlpLogSerializer
2222

2323
internal static int WriteLogsData(ref byte[] buffer, int writePosition, SdkLimitOptions sdkLimitOptions, ExperimentalOptions experimentalOptions, Resources.Resource? resource, in Batch<LogRecord> logRecordBatch)
2424
{
25-
writePosition = ProtobufSerializer.WriteTag(buffer, writePosition, ProtobufOtlpLogFieldNumberConstants.LogsData_Resource_Logs, ProtobufWireType.LEN);
26-
int logsDataLengthPosition = writePosition;
27-
writePosition += ReserveSizeForLength;
28-
2925
foreach (var logRecord in logRecordBatch)
3026
{
3127
var scopeName = logRecord.Logger.Name;
@@ -48,20 +44,28 @@ internal static int WriteLogsData(ref byte[] buffer, int writePosition, SdkLimit
4844
}
4945

5046
writePosition = TryWriteResourceLogs(ref buffer, writePosition, sdkLimitOptions, experimentalOptions, resource, ScopeLogsList);
51-
ProtobufSerializer.WriteReservedLength(buffer, logsDataLengthPosition, writePosition - (logsDataLengthPosition + ReserveSizeForLength));
5247
ReturnLogRecordListToPool();
5348

5449
return writePosition;
5550
}
5651

5752
internal static int TryWriteResourceLogs(ref byte[] buffer, int writePosition, SdkLimitOptions sdkLimitOptions, ExperimentalOptions experimentalOptions, Resources.Resource? resource, Dictionary<string, List<LogRecord>> scopeLogs)
5853
{
54+
int entryWritePosition = writePosition;
55+
5956
try
6057
{
58+
writePosition = ProtobufSerializer.WriteTag(buffer, writePosition, ProtobufOtlpLogFieldNumberConstants.LogsData_Resource_Logs, ProtobufWireType.LEN);
59+
int logsDataLengthPosition = writePosition;
60+
writePosition += ReserveSizeForLength;
61+
6162
writePosition = WriteResourceLogs(buffer, writePosition, sdkLimitOptions, experimentalOptions, resource, scopeLogs);
63+
64+
ProtobufSerializer.WriteReservedLength(buffer, logsDataLengthPosition, writePosition - (logsDataLengthPosition + ReserveSizeForLength));
6265
}
6366
catch (Exception ex) when (ex is IndexOutOfRangeException || ex is ArgumentException)
6467
{
68+
writePosition = entryWritePosition;
6569
if (!ProtobufSerializer.IncreaseBufferSize(ref buffer, OtlpSignalType.Logs))
6670
{
6771
throw;

src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/Serializer/ProtobufOtlpMetricSerializer.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@ internal static class ProtobufOtlpMetricSerializer
1919

2020
internal static int WriteMetricsData(ref byte[] buffer, int writePosition, Resources.Resource? resource, in Batch<Metric> batch)
2121
{
22-
writePosition = ProtobufSerializer.WriteTag(buffer, writePosition, ProtobufOtlpMetricFieldNumberConstants.MetricsData_Resource_Metrics, ProtobufWireType.LEN);
23-
int mericsDataLengthPosition = writePosition;
24-
writePosition += ReserveSizeForLength;
25-
2622
foreach (var metric in batch)
2723
{
2824
var metricName = metric.MeterName;
@@ -36,20 +32,28 @@ internal static int WriteMetricsData(ref byte[] buffer, int writePosition, Resou
3632
}
3733

3834
writePosition = TryWriteResourceMetrics(ref buffer, writePosition, resource, ScopeMetricsList);
39-
ProtobufSerializer.WriteReservedLength(buffer, mericsDataLengthPosition, writePosition - (mericsDataLengthPosition + ReserveSizeForLength));
4035
ReturnMetricListToPool();
4136

4237
return writePosition;
4338
}
4439

4540
internal static int TryWriteResourceMetrics(ref byte[] buffer, int writePosition, Resources.Resource? resource, Dictionary<string, List<Metric>> scopeMetrics)
4641
{
42+
int entryWritePosition = writePosition;
43+
4744
try
4845
{
46+
writePosition = ProtobufSerializer.WriteTag(buffer, writePosition, ProtobufOtlpMetricFieldNumberConstants.MetricsData_Resource_Metrics, ProtobufWireType.LEN);
47+
int mericsDataLengthPosition = writePosition;
48+
writePosition += ReserveSizeForLength;
49+
4950
writePosition = WriteResourceMetrics(buffer, writePosition, resource, scopeMetrics);
51+
52+
ProtobufSerializer.WriteReservedLength(buffer, mericsDataLengthPosition, writePosition - (mericsDataLengthPosition + ReserveSizeForLength));
5053
}
5154
catch (Exception ex) when (ex is IndexOutOfRangeException || ex is ArgumentException)
5255
{
56+
writePosition = entryWritePosition;
5357
if (!ProtobufSerializer.IncreaseBufferSize(ref buffer, OtlpSignalType.Metrics))
5458
{
5559
throw;

src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/Serializer/ProtobufOtlpTraceSerializer.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@ internal static class ProtobufOtlpTraceSerializer
2020

2121
internal static int WriteTraceData(ref byte[] buffer, int writePosition, SdkLimitOptions sdkLimitOptions, Resources.Resource? resource, in Batch<Activity> batch)
2222
{
23-
writePosition = ProtobufSerializer.WriteTag(buffer, writePosition, ProtobufOtlpTraceFieldNumberConstants.TracesData_Resource_Spans, ProtobufWireType.LEN);
24-
int resourceSpansScopeSpansLengthPosition = writePosition;
25-
writePosition += ReserveSizeForLength;
26-
2723
foreach (var activity in batch)
2824
{
2925
var sourceName = activity.Source.Name;
@@ -38,19 +34,28 @@ internal static int WriteTraceData(ref byte[] buffer, int writePosition, SdkLimi
3834

3935
writePosition = TryWriteResourceSpans(ref buffer, writePosition, sdkLimitOptions, resource);
4036
ReturnActivityListToPool();
41-
ProtobufSerializer.WriteReservedLength(buffer, resourceSpansScopeSpansLengthPosition, writePosition - (resourceSpansScopeSpansLengthPosition + ReserveSizeForLength));
4237

4338
return writePosition;
4439
}
4540

4641
internal static int TryWriteResourceSpans(ref byte[] buffer, int writePosition, SdkLimitOptions sdkLimitOptions, Resources.Resource? resource)
4742
{
43+
int entryWritePosition = writePosition;
44+
4845
try
4946
{
47+
writePosition = ProtobufSerializer.WriteTag(buffer, writePosition, ProtobufOtlpTraceFieldNumberConstants.TracesData_Resource_Spans, ProtobufWireType.LEN);
48+
int resourceSpansScopeSpansLengthPosition = writePosition;
49+
writePosition += ReserveSizeForLength;
50+
5051
writePosition = WriteResourceSpans(buffer, writePosition, sdkLimitOptions, resource);
52+
53+
ProtobufSerializer.WriteReservedLength(buffer, resourceSpansScopeSpansLengthPosition, writePosition - (resourceSpansScopeSpansLengthPosition + ReserveSizeForLength));
5154
}
5255
catch (Exception ex) when (ex is IndexOutOfRangeException || ex is ArgumentException)
5356
{
57+
writePosition = entryWritePosition;
58+
5459
// Attempt to increase the buffer size
5560
if (!ProtobufSerializer.IncreaseBufferSize(ref buffer, OtlpSignalType.Traces))
5661
{

src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/Serializer/ProtobufSerializer.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -359,9 +359,7 @@ internal static bool IncreaseBufferSize(ref byte[] buffer, OtlpSignalType otlpSi
359359
try
360360
{
361361
var newBufferSize = buffer.Length * 2;
362-
var newBuffer = new byte[newBufferSize];
363-
buffer.CopyTo(newBuffer, 0);
364-
buffer = newBuffer;
362+
buffer = new byte[newBufferSize];
365363
return true;
366364
}
367365
catch (OutOfMemoryException)

0 commit comments

Comments
 (0)