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

Logs: Scope improvements #2026

Merged
merged 17 commits into from
May 6, 2021
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Removed Depth tracking.
CodeBlanch committed May 5, 2021
commit d6788091c31240e720f582a39c1e872a11cb741d
6 changes: 4 additions & 2 deletions docs/logs/extending-the-sdk/MyExporter.cs
Original file line number Diff line number Diff line change
@@ -44,11 +44,13 @@ public override ExportResult Export(in Batch<LogRecord> batch)

sb.Append($"{record}(");

int depth = 0;

record.ForEachScope(ProcessScope, sb);

static void ProcessScope(LogRecordScope scope, StringBuilder builder)
void ProcessScope(LogRecordScope scope, StringBuilder builder)
{
if (scope.Depth > 0)
if (depth++ > 0)
{
builder.Append(", ");
}
10 changes: 7 additions & 3 deletions src/OpenTelemetry.Exporter.Console/ConsoleLogRecordExporter.cs
Original file line number Diff line number Diff line change
@@ -65,19 +65,23 @@ public override ExportResult Export(in Batch<LogRecord> batch)
this.WriteLine($"{"LogRecord.Exception:".PadRight(RightPaddingLength)}{logRecord.Exception?.Message}");
}

int scopeDepth = 0;

logRecord.ForEachScope(ProcessScope, this);

static void ProcessScope(LogRecordScope scope, ConsoleLogRecordExporter exporter)
void ProcessScope(LogRecordScope scope, ConsoleLogRecordExporter exporter)
{
if (scope.Depth == 0)
if (scopeDepth == 0)
{
exporter.WriteLine("LogRecord.ScopeValues (Key:Value):");
}

foreach (KeyValuePair<string, object> scopeItem in scope)
{
exporter.WriteLine($"[Scope.{scope.Depth}]:{scopeItem.Key.PadRight(RightPaddingLength)}{scopeItem.Value}");
exporter.WriteLine($"[Scope.{scopeDepth}]:{scopeItem.Key.PadRight(RightPaddingLength)}{scopeItem.Value}");
}

scopeDepth++;
}

var resource = this.ParentProvider.GetResource();
Original file line number Diff line number Diff line change
@@ -3,7 +3,6 @@ OpenTelemetry.Logs.LogRecord.ForEachScope<TState>(OpenTelemetry.Logs.LogRecordSc
OpenTelemetry.Logs.LogRecord.FormattedMessage.get -> string
OpenTelemetry.Logs.LogRecord.StateValues.get -> System.Collections.Generic.IReadOnlyList<System.Collections.Generic.KeyValuePair<string, object>>
OpenTelemetry.Logs.LogRecordScope
OpenTelemetry.Logs.LogRecordScope.Depth.get -> int
OpenTelemetry.Logs.LogRecordScope.Enumerator
OpenTelemetry.Logs.LogRecordScope.Enumerator.Current.get -> System.Collections.Generic.KeyValuePair<string, object>
OpenTelemetry.Logs.LogRecordScope.Enumerator.Dispose() -> void
Original file line number Diff line number Diff line change
@@ -3,7 +3,6 @@ OpenTelemetry.Logs.LogRecord.ForEachScope<TState>(OpenTelemetry.Logs.LogRecordSc
OpenTelemetry.Logs.LogRecord.FormattedMessage.get -> string
OpenTelemetry.Logs.LogRecord.StateValues.get -> System.Collections.Generic.IReadOnlyList<System.Collections.Generic.KeyValuePair<string, object>>
OpenTelemetry.Logs.LogRecordScope
OpenTelemetry.Logs.LogRecordScope.Depth.get -> int
OpenTelemetry.Logs.LogRecordScope.Enumerator
OpenTelemetry.Logs.LogRecordScope.Enumerator.Current.get -> System.Collections.Generic.KeyValuePair<string, object>
OpenTelemetry.Logs.LogRecordScope.Enumerator.Dispose() -> void
27 changes: 9 additions & 18 deletions src/OpenTelemetry/Logs/LogRecord.cs
Original file line number Diff line number Diff line change
@@ -118,24 +118,20 @@ internal LogRecord(
/// <typeparam name="TState">State.</typeparam>
/// <param name="callback">The callback to be executed for every scope object.</param>
/// <param name="state">The state object to be passed into the callback.</param>
public unsafe void ForEachScope<TState>(LogRecordScopeCallback<TState> callback, TState state)
public void ForEachScope<TState>(LogRecordScopeCallback<TState> callback, TState state)
{
var forEachScopeState = new ScopeForEachState<TState>(callback, state);

if (this.bufferedScopes != null)
{
ScopeForEachState<TState> forEachState = new ScopeForEachState<TState>(callback, state, 0);
IntPtr statePointer = new IntPtr(Unsafe.AsPointer(ref forEachState));

foreach (object scope in this.bufferedScopes)
{
ScopeForEachState<TState>.ForEachScope(scope, statePointer);
ScopeForEachState<TState>.ForEachScope(scope, forEachScopeState);
}
}
else if (this.ScopeProvider != null)
{
ScopeForEachState<TState> forEachState = new ScopeForEachState<TState>(callback, state, 0);
IntPtr statePointer = new IntPtr(Unsafe.AsPointer(ref forEachState));

this.ScopeProvider.ForEachScope(ScopeForEachState<TState>.ForEachScope, statePointer);
this.ScopeProvider.ForEachScope(ScopeForEachState<TState>.ForEachScope, forEachScopeState);
}
}

@@ -157,13 +153,11 @@ public void BufferLogScopes()
this.bufferedScopes = scopes;
}

private unsafe struct ScopeForEachState<TState>
private readonly struct ScopeForEachState<TState>
{
public static readonly Action<object, IntPtr> ForEachScope = (object scope, IntPtr statePointer) =>
public static readonly Action<object, ScopeForEachState<TState>> ForEachScope = (object scope, ScopeForEachState<TState> state) =>
{
ref ScopeForEachState<TState> state = ref Unsafe.AsRef<ScopeForEachState<TState>>((void*)statePointer);

LogRecordScope logRecordScope = new LogRecordScope(scope, state.Depth++);
LogRecordScope logRecordScope = new LogRecordScope(scope);

state.Callback(logRecordScope, state.UserState);
};
@@ -172,13 +166,10 @@ private unsafe struct ScopeForEachState<TState>

public readonly TState UserState;

public int Depth;

public ScopeForEachState(LogRecordScopeCallback<TState> callback, TState state, int depth)
public ScopeForEachState(LogRecordScopeCallback<TState> callback, TState state)
{
this.Callback = callback;
this.UserState = state;
this.Depth = depth;
}
}
}
8 changes: 1 addition & 7 deletions src/OpenTelemetry/Logs/LogRecordScope.cs
Original file line number Diff line number Diff line change
@@ -26,22 +26,16 @@ namespace OpenTelemetry.Logs
/// </summary>
public readonly ref struct LogRecordScope
{
internal LogRecordScope(object scope, int depth)
internal LogRecordScope(object scope)
{
this.Scope = scope;
this.Depth = depth;
}

/// <summary>
/// Gets the raw scope value.
/// </summary>
public object Scope { get; }

/// <summary>
/// Gets the depth of the current scope in the stack of active scopes.
/// </summary>
public int Depth { get; }

/// <summary>
/// Gets an <see cref="IEnumerator"/> for looping over the inner values
/// of the scope.
1 change: 0 additions & 1 deletion src/OpenTelemetry/OpenTelemetry.csproj
Original file line number Diff line number Diff line change
@@ -7,7 +7,6 @@
-->
<NoWarn>$(NoWarn),1591,CS0618</NoWarn>
<MinVerTagPrefix>core-</MinVerTagPrefix>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<ItemGroup>
20 changes: 9 additions & 11 deletions test/OpenTelemetry.Tests/Logs/LogRecordTest.cs
Original file line number Diff line number Diff line change
@@ -357,11 +357,11 @@ public void IncludeScopesTest()
this.logger.LogInformation("OpenTelemetry!");
logRecord = this.exportedItems[1];

int actualMaxDepth = -1;
int reachedDepth = -1;
logRecord.ForEachScope<object>(
(scope, state) =>
{
actualMaxDepth = scope.Depth;
reachedDepth++;
scopes.Add(scope.Scope);
foreach (KeyValuePair<string, object> item in scope)
{
@@ -371,7 +371,7 @@ public void IncludeScopesTest()
},
null);
Assert.Single(scopes);
Assert.Equal(0, actualMaxDepth);
Assert.Equal(0, reachedDepth);
Assert.Equal("string_scope", scopes[0]);

scopes.Clear();
@@ -386,13 +386,12 @@ public void IncludeScopesTest()
this.logger.LogInformation("OpenTelemetry!");
logRecord = this.exportedItems[2];

actualMaxDepth = -1;
reachedDepth = -1;
logRecord.ForEachScope<object>(
(scope, state) =>
{
actualMaxDepth = scope.Depth;
scopes.Add(scope.Scope);
if (scope.Depth == 1)
if (reachedDepth++ == 1)
{
foreach (KeyValuePair<string, object> item in scope)
{
@@ -402,7 +401,7 @@ public void IncludeScopesTest()
},
null);
Assert.Equal(2, scopes.Count);
Assert.Equal(1, actualMaxDepth);
Assert.Equal(1, reachedDepth);
Assert.Equal("string_scope", scopes[0]);
Assert.Same(expectedScope2, scopes[1]);

@@ -418,13 +417,12 @@ public void IncludeScopesTest()
this.logger.LogInformation("OpenTelemetry!");
logRecord = this.exportedItems[3];

actualMaxDepth = -1;
reachedDepth = -1;
logRecord.ForEachScope<object>(
(scope, state) =>
{
actualMaxDepth = scope.Depth;
scopes.Add(scope.Scope);
if (scope.Depth == 2)
if (reachedDepth++ == 2)
{
foreach (KeyValuePair<string, object> item in scope)
{
@@ -434,7 +432,7 @@ public void IncludeScopesTest()
},
null);
Assert.Equal(3, scopes.Count);
Assert.Equal(2, actualMaxDepth);
Assert.Equal(2, reachedDepth);
Assert.Equal("string_scope", scopes[0]);
Assert.Same(expectedScope2, scopes[1]);
Assert.Same(expectedScope3, scopes[2]);