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] Enable OTLP exporter for multiple registrations #6020

Merged
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 @@ -14,21 +14,26 @@ internal static class ProtobufOtlpLogSerializer
private const int TraceIdSize = 16;
private const int SpanIdSize = 8;

private static readonly Stack<List<LogRecord>> LogsListPool = [];
private static readonly Dictionary<string, List<LogRecord>> ScopeLogsList = [];
[ThreadStatic]
private static Stack<List<LogRecord>>? logsListPool;
[ThreadStatic]
private static Dictionary<string, List<LogRecord>>? scopeLogsList;

[ThreadStatic]
private static SerializationState? threadSerializationState;

internal static int WriteLogsData(ref byte[] buffer, int writePosition, SdkLimitOptions sdkLimitOptions, ExperimentalOptions experimentalOptions, Resources.Resource? resource, in Batch<LogRecord> logRecordBatch)
{
logsListPool ??= [];
scopeLogsList ??= [];

foreach (var logRecord in logRecordBatch)
{
var scopeName = logRecord.Logger.Name;
if (!ScopeLogsList.TryGetValue(scopeName, out var logRecords))
if (!scopeLogsList.TryGetValue(scopeName, out var logRecords))
{
logRecords = LogsListPool.Count > 0 ? LogsListPool.Pop() : [];
ScopeLogsList[scopeName] = logRecords;
logRecords = logsListPool.Count > 0 ? logsListPool.Pop() : [];
scopeLogsList[scopeName] = logRecords;
}

if (logRecord.Source == LogRecord.LogRecordSource.FromSharedPool)
Expand All @@ -43,7 +48,7 @@ internal static int WriteLogsData(ref byte[] buffer, int writePosition, SdkLimit
logRecords.Add(logRecord);
}

writePosition = TryWriteResourceLogs(ref buffer, writePosition, sdkLimitOptions, experimentalOptions, resource, ScopeLogsList);
writePosition = TryWriteResourceLogs(ref buffer, writePosition, sdkLimitOptions, experimentalOptions, resource, scopeLogsList);
ReturnLogRecordListToPool();

return writePosition;
Expand Down Expand Up @@ -79,9 +84,9 @@ internal static int TryWriteResourceLogs(ref byte[] buffer, int writePosition, S

internal static void ReturnLogRecordListToPool()
{
if (ScopeLogsList.Count != 0)
if (scopeLogsList?.Count != 0)
{
foreach (var entry in ScopeLogsList)
foreach (var entry in scopeLogsList!)
{
foreach (var logRecord in entry.Value)
{
Expand All @@ -96,10 +101,10 @@ internal static void ReturnLogRecordListToPool()
}

entry.Value.Clear();
LogsListPool.Push(entry.Value);
logsListPool?.Push(entry.Value);
}

ScopeLogsList.Clear();
scopeLogsList.Clear();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,31 @@ internal static class ProtobufOtlpMetricSerializer
private const int TraceIdSize = 16;
private const int SpanIdSize = 8;

private static readonly Stack<List<Metric>> MetricListPool = [];
private static readonly Dictionary<string, List<Metric>> ScopeMetricsList = [];
[ThreadStatic]
private static Stack<List<Metric>>? metricListPool;
[ThreadStatic]
private static Dictionary<string, List<Metric>>? scopeMetricsList;

private delegate int WriteExemplarFunc(byte[] buffer, int writePosition, in Exemplar exemplar);

internal static int WriteMetricsData(ref byte[] buffer, int writePosition, Resources.Resource? resource, in Batch<Metric> batch)
{
metricListPool ??= [];
scopeMetricsList ??= [];

foreach (var metric in batch)
{
var metricName = metric.MeterName;
if (!ScopeMetricsList.TryGetValue(metricName, out var metrics))
if (!scopeMetricsList.TryGetValue(metricName, out var metrics))
{
metrics = MetricListPool.Count > 0 ? MetricListPool.Pop() : new List<Metric>();
ScopeMetricsList[metricName] = metrics;
metrics = metricListPool.Count > 0 ? metricListPool.Pop() : new List<Metric>();
scopeMetricsList[metricName] = metrics;
}

metrics.Add(metric);
}

writePosition = TryWriteResourceMetrics(ref buffer, writePosition, resource, ScopeMetricsList);
writePosition = TryWriteResourceMetrics(ref buffer, writePosition, resource, scopeMetricsList);
ReturnMetricListToPool();

return writePosition;
Expand Down Expand Up @@ -67,15 +72,15 @@ internal static int TryWriteResourceMetrics(ref byte[] buffer, int writePosition

private static void ReturnMetricListToPool()
{
if (ScopeMetricsList.Count != 0)
if (scopeMetricsList?.Count != 0)
{
foreach (var entry in ScopeMetricsList)
foreach (var entry in scopeMetricsList!)
{
entry.Value.Clear();
MetricListPool.Push(entry.Value);
metricListPool?.Push(entry.Value);
}

ScopeMetricsList.Clear();
scopeMetricsList.Clear();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,23 @@ internal static class ProtobufOtlpTraceSerializer
private const int TraceIdSize = 16;
private const int SpanIdSize = 8;

private static readonly Stack<List<Activity>> ActivityListPool = [];
private static readonly Dictionary<string, List<Activity>> ScopeTracesList = [];
[ThreadStatic]
private static Stack<List<Activity>>? activityListPool;
[ThreadStatic]
private static Dictionary<string, List<Activity>>? scopeTracesList;

internal static int WriteTraceData(ref byte[] buffer, int writePosition, SdkLimitOptions sdkLimitOptions, Resources.Resource? resource, in Batch<Activity> batch)
{
activityListPool ??= [];
scopeTracesList ??= [];

foreach (var activity in batch)
{
var sourceName = activity.Source.Name;
if (!ScopeTracesList.TryGetValue(sourceName, out var activities))
if (!scopeTracesList.TryGetValue(sourceName, out var activities))
{
activities = ActivityListPool.Count > 0 ? ActivityListPool.Pop() : [];
ScopeTracesList[sourceName] = activities;
activities = activityListPool.Count > 0 ? activityListPool.Pop() : [];
scopeTracesList[sourceName] = activities;
}

activities.Add(activity);
Expand Down Expand Up @@ -74,15 +79,15 @@ internal static int TryWriteResourceSpans(ref byte[] buffer, int writePosition,

internal static void ReturnActivityListToPool()
{
if (ScopeTracesList.Count != 0)
if (scopeTracesList?.Count != 0)
{
foreach (var entry in ScopeTracesList)
foreach (var entry in scopeTracesList!)
{
entry.Value.Clear();
ActivityListPool.Push(entry.Value);
activityListPool?.Push(entry.Value);
}

ScopeTracesList.Clear();
scopeTracesList.Clear();
}
}

Expand All @@ -96,9 +101,9 @@ internal static int WriteResourceSpans(byte[] buffer, int writePosition, SdkLimi

internal static int WriteScopeSpans(byte[] buffer, int writePosition, SdkLimitOptions sdkLimitOptions)
{
if (ScopeTracesList != null)
if (scopeTracesList != null)
{
foreach (KeyValuePair<string, List<Activity>> entry in ScopeTracesList)
foreach (KeyValuePair<string, List<Activity>> entry in scopeTracesList)
{
writePosition = ProtobufSerializer.WriteTag(buffer, writePosition, ProtobufOtlpTraceFieldNumberConstants.ResourceSpans_Scope_Spans, ProtobufWireType.LEN);
int resourceSpansScopeSpansLengthPosition = writePosition;
Expand Down
Loading