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

[Exporter.Geneva] Add support for ILogger scopes #390

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
3 changes: 3 additions & 0 deletions src/OpenTelemetry.Exporter.Geneva/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Unreleased

* Add support for exporting `ILogger` scopes.
[390](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/390)

## 1.3.0-beta.1 [2022-May-27]

* Enable PassThru TableNameMappings using the logger category name.
Expand Down
38 changes: 38 additions & 0 deletions src/OpenTelemetry.Exporter.Geneva/GenevaLogExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,44 @@ internal int SerializeLogRecord(LogRecord logRecord)
cntFields += 1;
}

ushort scopeDepth = 0;
int indexArrayLength = 0;
logRecord.ForEachScope(ProcessScope, (object)null);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI this is going to allocate a delegate. Could maybe get around that using a ThreadStatic. Something like this...

private sealed class ScopeForEachState
{
   public ushort ScopeDepth;
   public int IndexArrayLength; // Confusing name
   public byte[] Buffer;
}

[ThreadStatic]
private static ScopeForEachState scopeState;

var state = scopeState ??= new() { Buffer = buffer };

state.ScopeDepth = 0;
state.IndexArrayLength = 0;

logRecord.ForEachScope(ProcessScope, state);

if (state.ScopeDepth > 0)
{
        MessagePackSerializer.WriteUInt16(buffer, state.IndexArrayLength, state.ScopeDepth);
        cntFields += 1;
}

private static readonly Action<LogRecordScope, ScopeForEachState> ProcessScope = (logRecord, state) =>
{
           var buffer = state.Buffer;

            if (++state.ScopeDepth == 1)
            {
                cursor = MessagePackSerializer.SerializeAsciiString(buffer, cursor, "scopes");
                cursor = MessagePackSerializer.WriteArrayHeader(buffer, cursor, ushort.MaxValue);
                state.IndexArrayLength = cursor - 2;
            }

            cursor = MessagePackSerializer.WriteMapHeader(buffer, cursor, ushort.MaxValue);
            int indexMapSizeScope = cursor - 2;
            ushort keysCount = 0;

            foreach (KeyValuePair<string, object> scopeItem in scope)
            {
                string key = "scope";
                if (!string.IsNullOrEmpty(scopeItem.Key))
                {
                    key = scopeItem.Key;
                }

                cursor = MessagePackSerializer.SerializeUnicodeString(buffer, cursor, key);
                cursor = MessagePackSerializer.Serialize(buffer, cursor, scopeItem.Value);
                keysCount++;
            }

            MessagePackSerializer.WriteUInt16(buffer, indexMapSizeScope, keysCount);
};

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have private static readonly ThreadLocal<byte[]> m_buffer = new ThreadLocal<byte[]>(() => null); you could just expand that a little bit to be like ThreadLocal<ExporterState> and then use ExporterState to contain the buffer + the stuff you need for scopes. Basically fold everything you need onto the 1 ThreadLocal.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @CodeBlanch ! I have created #391 to track this. I'll be merging this PR to do a beta release to gather feedback. I'll send out a separate PR to avoid allocation here.

void ProcessScope(LogRecordScope scope, object state)
{
if (++scopeDepth == 1)
{
cursor = MessagePackSerializer.SerializeAsciiString(buffer, cursor, "scopes");
cursor = MessagePackSerializer.WriteArrayHeader(buffer, cursor, ushort.MaxValue);
indexArrayLength = cursor - 2;
}

cursor = MessagePackSerializer.WriteMapHeader(buffer, cursor, ushort.MaxValue);
int indexMapSizeScope = cursor - 2;
ushort keysCount = 0;

foreach (KeyValuePair<string, object> scopeItem in scope)
{
string key = "scope";
if (!string.IsNullOrEmpty(scopeItem.Key))
{
key = scopeItem.Key;
}

cursor = MessagePackSerializer.SerializeUnicodeString(buffer, cursor, key);
cursor = MessagePackSerializer.Serialize(buffer, cursor, scopeItem.Value);
keysCount++;
}

MessagePackSerializer.WriteUInt16(buffer, indexMapSizeScope, keysCount);
}

if (scopeDepth > 0)
{
MessagePackSerializer.WriteUInt16(buffer, indexArrayLength, scopeDepth);
cntFields += 1;
}

if (hasEnvProperties)
{
// Iteration #2 - Get all "other" fields and collapse them into single field
Expand Down