From 310e8cb90b59daf23eb12ad01b7e3e26dc47e68c Mon Sep 17 00:00:00 2001 From: Mikel Blanchard Date: Wed, 5 Oct 2022 16:22:16 -0700 Subject: [PATCH] Update log scope buffering fix for new api. --- .../Logs/ILogger/OpenTelemetryLogger.cs | 5 +- src/OpenTelemetry/Logs/LogRecord.cs | 51 +++++++++++++------ .../BatchLogRecordExportProcessorTests.cs | 6 +-- 3 files changed, 42 insertions(+), 20 deletions(-) diff --git a/src/OpenTelemetry/Logs/ILogger/OpenTelemetryLogger.cs b/src/OpenTelemetry/Logs/ILogger/OpenTelemetryLogger.cs index c5107dec35e..d8c8bdad9c7 100644 --- a/src/OpenTelemetry/Logs/ILogger/OpenTelemetryLogger.cs +++ b/src/OpenTelemetry/Logs/ILogger/OpenTelemetryLogger.cs @@ -75,6 +75,8 @@ public void Log(LogLevel logLevel, EventId eventId, TState state, Except iloggerData.CategoryName = this.categoryName; iloggerData.EventId = eventId; iloggerData.Exception = exception; + iloggerData.ScopeProvider = iloggerProvider.IncludeScopes ? this.ScopeProvider : null; + iloggerData.BufferedScopes = null; ref LogRecordData data = ref record.Data; @@ -114,9 +116,8 @@ public void Log(LogLevel logLevel, EventId eventId, TState state, Except iloggerData.FormattedMessage = iloggerProvider.IncludeFormattedMessage ? formatter?.Invoke(state, exception) : null; } - record.ScopeProvider = iloggerProvider.IncludeScopes ? this.ScopeProvider : null; processor.OnEnd(record); - record.ScopeProvider = null; + iloggerData.ScopeProvider = null; // Attempt to return the LogRecord to the pool. This will no-op // if a batch exporter has added a reference. diff --git a/src/OpenTelemetry/Logs/LogRecord.cs b/src/OpenTelemetry/Logs/LogRecord.cs index 936e3f2e5b7..3e8836d8df5 100644 --- a/src/OpenTelemetry/Logs/LogRecord.cs +++ b/src/OpenTelemetry/Logs/LogRecord.cs @@ -35,7 +35,6 @@ public sealed class LogRecord internal LogRecordILoggerData ILoggerData; internal List>? AttributeStorage; internal List? ScopeStorage; - internal List? BufferedScopes; internal int PoolReferenceCount = int.MaxValue; private static readonly Action> AddScopeToBufferedList = (object? scope, List state) => @@ -76,6 +75,7 @@ internal LogRecord( EventId = eventId, Exception = exception, State = state, + ScopeProvider = scopeProvider, }; if (stateValues != null && stateValues.Count > 0) @@ -91,8 +91,6 @@ internal LogRecord( this.InstrumentationScope = null; this.Attributes = stateValues; - - this.ScopeProvider = scopeProvider; } /// @@ -263,8 +261,6 @@ public LogRecordSeverity? Severity /// public InstrumentationScope? InstrumentationScope { get; internal set; } - internal IExternalScopeProvider? ScopeProvider { get; set; } - /// /// Executes callback for each currently active scope objects in order /// of creation. All callbacks are guaranteed to be called inline from @@ -282,16 +278,16 @@ public void ForEachScope(Action callback, TState var forEachScopeState = new ScopeForEachState(callback, state); - if (this.BufferedScopes != null) + if (this.ILoggerData.BufferedScopes != null) { - foreach (object? scope in this.BufferedScopes) + foreach (object? scope in this.ILoggerData.BufferedScopes) { ScopeForEachState.ForEachScope(scope, forEachScopeState); } } else { - this.ScopeProvider?.ForEachScope(ScopeForEachState.ForEachScope, forEachScopeState); + this.ILoggerData.ScopeProvider?.ForEachScope(ScopeForEachState.ForEachScope, forEachScopeState); } } @@ -331,13 +327,14 @@ internal LogRecord Copy() // directly below. this.BufferLogScopes(); - return new() + var copy = new LogRecord() { Data = this.Data, - ILoggerData = this.ILoggerData, + ILoggerData = this.ILoggerData.Copy(), Attributes = this.Attributes == null ? null : new List>(this.Attributes), - BufferedScopes = this.BufferedScopes == null ? null : new List(this.BufferedScopes), }; + + return copy; } /// @@ -371,18 +368,19 @@ private void BufferLogAttributes() /// private void BufferLogScopes() { - if (this.ScopeProvider == null) + var scopeProvider = this.ILoggerData.ScopeProvider; + if (scopeProvider == null) { return; } var scopeStorage = this.ScopeStorage ??= new List(LogRecordPoolHelper.DefaultMaxNumberOfScopes); - this.ScopeProvider.ForEachScope(AddScopeToBufferedList, scopeStorage); + scopeProvider.ForEachScope(AddScopeToBufferedList, scopeStorage); - this.ScopeProvider = null; + this.ILoggerData.ScopeProvider = null; - this.BufferedScopes = scopeStorage; + this.ILoggerData.BufferedScopes = scopeStorage; } internal struct LogRecordILoggerData @@ -393,6 +391,29 @@ internal struct LogRecordILoggerData public string? FormattedMessage; public Exception? Exception; public object? State; + public IExternalScopeProvider? ScopeProvider; + public List? BufferedScopes; + + public LogRecordILoggerData Copy() + { + var copy = new LogRecordILoggerData + { + TraceState = this.TraceState, + CategoryName = this.CategoryName, + EventId = this.EventId, + FormattedMessage = this.FormattedMessage, + Exception = this.Exception, + State = this.State, + }; + + var bufferedScopes = this.BufferedScopes; + if (bufferedScopes != null) + { + copy.BufferedScopes = new List(bufferedScopes); + } + + return copy; + } } private readonly struct ScopeForEachState diff --git a/test/OpenTelemetry.Tests/Logs/BatchLogRecordExportProcessorTests.cs b/test/OpenTelemetry.Tests/Logs/BatchLogRecordExportProcessorTests.cs index 95295f1b482..105f848a796 100644 --- a/test/OpenTelemetry.Tests/Logs/BatchLogRecordExportProcessorTests.cs +++ b/test/OpenTelemetry.Tests/Logs/BatchLogRecordExportProcessorTests.cs @@ -41,7 +41,7 @@ public void StateValuesAndScopeBufferingTest() var state = new LogRecordTest.DisposingState("Hello world"); - logRecord.ScopeProvider = scopeProvider; + logRecord.ILoggerData.ScopeProvider = scopeProvider; logRecord.StateValues = state; processor.OnEnd(logRecord); @@ -50,10 +50,10 @@ public void StateValuesAndScopeBufferingTest() Assert.Empty(exportedItems); - Assert.Null(logRecord.ScopeProvider); + Assert.Null(logRecord.ILoggerData.ScopeProvider); Assert.False(ReferenceEquals(state, logRecord.StateValues)); Assert.NotNull(logRecord.AttributeStorage); - Assert.NotNull(logRecord.BufferedScopes); + Assert.NotNull(logRecord.ILoggerData.BufferedScopes); KeyValuePair actualState = logRecord.StateValues[0];