Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[otlp] Avoid filling new buffer on expansion #6019

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ internal static class ProtobufOtlpLogSerializer

internal static int WriteLogsData(ref byte[] buffer, int writePosition, SdkLimitOptions sdkLimitOptions, ExperimentalOptions experimentalOptions, Resources.Resource? resource, in Batch<LogRecord> logRecordBatch)
{
writePosition = ProtobufSerializer.WriteTag(buffer, writePosition, ProtobufOtlpLogFieldNumberConstants.LogsData_Resource_Logs, ProtobufWireType.LEN);
int logsDataLengthPosition = writePosition;
writePosition += ReserveSizeForLength;

foreach (var logRecord in logRecordBatch)
{
var scopeName = logRecord.Logger.Name;
Expand All @@ -48,20 +44,28 @@ internal static int WriteLogsData(ref byte[] buffer, int writePosition, SdkLimit
}

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

return writePosition;
}

internal static int TryWriteResourceLogs(ref byte[] buffer, int writePosition, SdkLimitOptions sdkLimitOptions, ExperimentalOptions experimentalOptions, Resources.Resource? resource, Dictionary<string, List<LogRecord>> scopeLogs)
{
int entryWritePosition = writePosition;

try
{
writePosition = ProtobufSerializer.WriteTag(buffer, writePosition, ProtobufOtlpLogFieldNumberConstants.LogsData_Resource_Logs, ProtobufWireType.LEN);
int logsDataLengthPosition = writePosition;
writePosition += ReserveSizeForLength;

writePosition = WriteResourceLogs(buffer, writePosition, sdkLimitOptions, experimentalOptions, resource, scopeLogs);

ProtobufSerializer.WriteReservedLength(buffer, logsDataLengthPosition, writePosition - (logsDataLengthPosition + ReserveSizeForLength));
}
catch (Exception ex) when (ex is IndexOutOfRangeException || ex is ArgumentException)
{
writePosition = entryWritePosition;
if (!ProtobufSerializer.IncreaseBufferSize(ref buffer, OtlpSignalType.Logs))
{
throw;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ internal static class ProtobufOtlpMetricSerializer

internal static int WriteMetricsData(ref byte[] buffer, int writePosition, Resources.Resource? resource, in Batch<Metric> batch)
{
writePosition = ProtobufSerializer.WriteTag(buffer, writePosition, ProtobufOtlpMetricFieldNumberConstants.MetricsData_Resource_Metrics, ProtobufWireType.LEN);
int mericsDataLengthPosition = writePosition;
writePosition += ReserveSizeForLength;

foreach (var metric in batch)
{
var metricName = metric.MeterName;
Expand All @@ -36,20 +32,28 @@ internal static int WriteMetricsData(ref byte[] buffer, int writePosition, Resou
}

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

return writePosition;
}

internal static int TryWriteResourceMetrics(ref byte[] buffer, int writePosition, Resources.Resource? resource, Dictionary<string, List<Metric>> scopeMetrics)
{
int entryWritePosition = writePosition;

try
{
writePosition = ProtobufSerializer.WriteTag(buffer, writePosition, ProtobufOtlpMetricFieldNumberConstants.MetricsData_Resource_Metrics, ProtobufWireType.LEN);
int mericsDataLengthPosition = writePosition;
writePosition += ReserveSizeForLength;

writePosition = WriteResourceMetrics(buffer, writePosition, resource, scopeMetrics);

ProtobufSerializer.WriteReservedLength(buffer, mericsDataLengthPosition, writePosition - (mericsDataLengthPosition + ReserveSizeForLength));
}
catch (Exception ex) when (ex is IndexOutOfRangeException || ex is ArgumentException)
{
writePosition = entryWritePosition;
if (!ProtobufSerializer.IncreaseBufferSize(ref buffer, OtlpSignalType.Metrics))
{
throw;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ internal static class ProtobufOtlpTraceSerializer

internal static int WriteTraceData(ref byte[] buffer, int writePosition, SdkLimitOptions sdkLimitOptions, Resources.Resource? resource, in Batch<Activity> batch)
{
writePosition = ProtobufSerializer.WriteTag(buffer, writePosition, ProtobufOtlpTraceFieldNumberConstants.TracesData_Resource_Spans, ProtobufWireType.LEN);
int resourceSpansScopeSpansLengthPosition = writePosition;
writePosition += ReserveSizeForLength;

foreach (var activity in batch)
{
var sourceName = activity.Source.Name;
Expand All @@ -38,19 +34,28 @@ internal static int WriteTraceData(ref byte[] buffer, int writePosition, SdkLimi

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

return writePosition;
}

internal static int TryWriteResourceSpans(ref byte[] buffer, int writePosition, SdkLimitOptions sdkLimitOptions, Resources.Resource? resource)
{
int entryWritePosition = writePosition;

try
{
writePosition = ProtobufSerializer.WriteTag(buffer, writePosition, ProtobufOtlpTraceFieldNumberConstants.TracesData_Resource_Spans, ProtobufWireType.LEN);
int resourceSpansScopeSpansLengthPosition = writePosition;
writePosition += ReserveSizeForLength;

writePosition = WriteResourceSpans(buffer, writePosition, sdkLimitOptions, resource);

ProtobufSerializer.WriteReservedLength(buffer, resourceSpansScopeSpansLengthPosition, writePosition - (resourceSpansScopeSpansLengthPosition + ReserveSizeForLength));
}
catch (Exception ex) when (ex is IndexOutOfRangeException || ex is ArgumentException)
{
writePosition = entryWritePosition;

// Attempt to increase the buffer size
if (!ProtobufSerializer.IncreaseBufferSize(ref buffer, OtlpSignalType.Traces))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,9 +359,7 @@ internal static bool IncreaseBufferSize(ref byte[] buffer, OtlpSignalType otlpSi
try
{
var newBufferSize = buffer.Length * 2;
var newBuffer = new byte[newBufferSize];
buffer.CopyTo(newBuffer, 0);
buffer = newBuffer;
buffer = new byte[newBufferSize];
return true;
}
catch (OutOfMemoryException)
Expand Down
Loading