Skip to content

Commit

Permalink
Update log scope buffering fix for new api.
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeBlanch committed Oct 5, 2022
1 parent c43d6a2 commit 310e8cb
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 20 deletions.
5 changes: 3 additions & 2 deletions src/OpenTelemetry/Logs/ILogger/OpenTelemetryLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ public void Log<TState>(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;

Expand Down Expand Up @@ -114,9 +116,8 @@ public void Log<TState>(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.
Expand Down
51 changes: 36 additions & 15 deletions src/OpenTelemetry/Logs/LogRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public sealed class LogRecord
internal LogRecordILoggerData ILoggerData;
internal List<KeyValuePair<string, object?>>? AttributeStorage;
internal List<object?>? ScopeStorage;
internal List<object?>? BufferedScopes;
internal int PoolReferenceCount = int.MaxValue;

private static readonly Action<object?, List<object?>> AddScopeToBufferedList = (object? scope, List<object?> state) =>
Expand Down Expand Up @@ -76,6 +75,7 @@ internal LogRecord(
EventId = eventId,
Exception = exception,
State = state,
ScopeProvider = scopeProvider,
};

if (stateValues != null && stateValues.Count > 0)
Expand All @@ -91,8 +91,6 @@ internal LogRecord(
this.InstrumentationScope = null;

this.Attributes = stateValues;

this.ScopeProvider = scopeProvider;
}

/// <summary>
Expand Down Expand Up @@ -263,8 +261,6 @@ public LogRecordSeverity? Severity
/// </summary>
public InstrumentationScope? InstrumentationScope { get; internal set; }

internal IExternalScopeProvider? ScopeProvider { get; set; }

/// <summary>
/// Executes callback for each currently active scope objects in order
/// of creation. All callbacks are guaranteed to be called inline from
Expand All @@ -282,16 +278,16 @@ public void ForEachScope<TState>(Action<LogRecordScope, TState> callback, TState

var forEachScopeState = new ScopeForEachState<TState>(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<TState>.ForEachScope(scope, forEachScopeState);
}
}
else
{
this.ScopeProvider?.ForEachScope(ScopeForEachState<TState>.ForEachScope, forEachScopeState);
this.ILoggerData.ScopeProvider?.ForEachScope(ScopeForEachState<TState>.ForEachScope, forEachScopeState);
}
}

Expand Down Expand Up @@ -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<KeyValuePair<string, object?>>(this.Attributes),
BufferedScopes = this.BufferedScopes == null ? null : new List<object?>(this.BufferedScopes),
};

return copy;
}

/// <summary>
Expand Down Expand Up @@ -371,18 +368,19 @@ private void BufferLogAttributes()
/// </summary>
private void BufferLogScopes()
{
if (this.ScopeProvider == null)
var scopeProvider = this.ILoggerData.ScopeProvider;
if (scopeProvider == null)
{
return;
}

var scopeStorage = this.ScopeStorage ??= new List<object?>(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
Expand All @@ -393,6 +391,29 @@ internal struct LogRecordILoggerData
public string? FormattedMessage;
public Exception? Exception;
public object? State;
public IExternalScopeProvider? ScopeProvider;
public List<object?>? 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<object?>(bufferedScopes);
}

return copy;
}
}

private readonly struct ScopeForEachState<TState>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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<string, object> actualState = logRecord.StateValues[0];

Expand Down

0 comments on commit 310e8cb

Please sign in to comment.