Skip to content

Commit

Permalink
Reuse logsByCategory dictionary
Browse files Browse the repository at this point in the history
  • Loading branch information
paulomorgado committed Jun 27, 2024
1 parent 7922e30 commit ef9dbdf
Showing 1 changed file with 29 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation;
internal sealed class OtlpLogRecordTransformer
{
internal static readonly ConcurrentBag<OtlpLogs.ScopeLogs> LogListPool = new();
private Dictionary<string, OtlpLogs.ScopeLogs>? logsByCategory;

private readonly SdkLimitOptions sdkLimitOptions;
private readonly ExperimentalOptions experimentalOptions;
Expand All @@ -31,35 +32,42 @@ internal OtlpCollector.ExportLogsServiceRequest BuildExportRequest(
OtlpResource.Resource processResource,
in Batch<LogRecord> logRecordBatch)
{
// TODO: https://github.com/open-telemetry/opentelemetry-dotnet/issues/4943
Dictionary<string, OtlpLogs.ScopeLogs> logsByCategory = new Dictionary<string, OtlpLogs.ScopeLogs>();
var logsByCategory = Interlocked.Exchange(ref this.logsByCategory, null) ?? new Dictionary<string, OtlpLogs.ScopeLogs>();

var request = new OtlpCollector.ExportLogsServiceRequest();

var resourceLogs = new OtlpLogs.ResourceLogs
try
{
Resource = processResource,
};
request.ResourceLogs.Add(resourceLogs);
var request = new OtlpCollector.ExportLogsServiceRequest();

foreach (var logRecord in logRecordBatch)
{
var otlpLogRecord = this.ToOtlpLog(logRecord);
if (otlpLogRecord != null)
var resourceLogs = new OtlpLogs.ResourceLogs
{
var scopeName = logRecord.Logger.Name;
if (!logsByCategory.TryGetValue(scopeName, out var scopeLogs))
Resource = processResource,
};
request.ResourceLogs.Add(resourceLogs);

foreach (var logRecord in logRecordBatch)
{
var otlpLogRecord = this.ToOtlpLog(logRecord);
if (otlpLogRecord != null)
{
scopeLogs = this.GetLogListFromPool(scopeName);
logsByCategory.Add(scopeName, scopeLogs);
resourceLogs.ScopeLogs.Add(scopeLogs);
}
var scopeName = logRecord.Logger.Name;
if (!logsByCategory.TryGetValue(scopeName, out var scopeLogs))
{
scopeLogs = this.GetLogListFromPool(scopeName);
logsByCategory.Add(scopeName, scopeLogs);
resourceLogs.ScopeLogs.Add(scopeLogs);
}

scopeLogs.LogRecords.Add(otlpLogRecord);
scopeLogs.LogRecords.Add(otlpLogRecord);
}
}
}

return request;
return request;
}
finally
{
logsByCategory.Clear();
Interlocked.Exchange(ref this.logsByCategory, logsByCategory);
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down

0 comments on commit ef9dbdf

Please sign in to comment.