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] Avoid allocation when serializing scopes #433

Closed
Changes from 4 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
80 changes: 48 additions & 32 deletions src/OpenTelemetry.Exporter.Geneva/GenevaLogExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class GenevaLogExporter : GenevaBaseExporter<LogRecord>
private readonly IReadOnlyDictionary<string, object> m_prepopulatedFields;
private readonly List<string> m_prepopulatedFieldKeys;
private static readonly ThreadLocal<byte[]> m_buffer = new ThreadLocal<byte[]>(() => null);
private static readonly ThreadLocal<ExporterStateForScopes> exporterState = new(() => new ExporterStateForScopes());
private readonly byte[] m_bufferEpilogue;
private static readonly string[] logLevels = new string[7]
{
Expand Down Expand Up @@ -417,41 +418,19 @@ internal int SerializeLogRecord(LogRecord logRecord)
cntFields += 1;
}

ushort scopeDepth = 0;
int indexArrayLength = 0;
logRecord.ForEachScope(ProcessScope, (object)null);
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;
}
var state = exporterState.Value;
state.ScopeDepth = 0;
state.IndexForArrayLength = 0;
state.Cursor = cursor;
state.Buffer = buffer;

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

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

if (scopeDepth > 0)
// logRecord has scopes
if (state.ScopeDepth > 0)
{
MessagePackSerializer.WriteUInt16(buffer, indexArrayLength, scopeDepth);
MessagePackSerializer.WriteUInt16(buffer, state.IndexForArrayLength, state.ScopeDepth);
cursor = state.Cursor;
cntFields += 1;
}

Expand Down Expand Up @@ -580,4 +559,41 @@ private static int SerializeSanitizedCategoryName(byte[] buffer, int cursor, str

return cursor;
}

private class ExporterStateForScopes
{
internal ushort ScopeDepth;
internal int IndexForArrayLength;
internal int Cursor;
internal byte[] Buffer;
Copy link
Member

Choose a reason for hiding this comment

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

If you made this...

    internal byte[] Buffer = new byte[BUFFER_SIZE];

You could probably get rid of...

private static readonly ThreadLocal<byte[]> m_buffer = new ThreadLocal<byte[]>(() => null);

...and just use exporterState for everything the exporter needs on a thread.

}

private static readonly Action<LogRecordScope, ExporterStateForScopes> ProcessScope = (scope, state) =>
{
if (++state.ScopeDepth == 1)
{
state.Cursor = MessagePackSerializer.SerializeAsciiString(state.Buffer, state.Cursor, "scopes");
state.Cursor = MessagePackSerializer.WriteArrayHeader(state.Buffer, state.Cursor, ushort.MaxValue);
state.IndexForArrayLength = state.Cursor - 2;
Copy link
Contributor

Choose a reason for hiding this comment

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

If this line is moved up one then the - 2 is not needed right?

}

state.Cursor = MessagePackSerializer.WriteMapHeader(state.Buffer, state.Cursor, ushort.MaxValue);
int indexForMapSize = state.Cursor - 2;
ushort keysCount = 0;

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

state.Cursor = MessagePackSerializer.SerializeUnicodeString(state.Buffer, state.Cursor, key);
state.Cursor = MessagePackSerializer.Serialize(state.Buffer, state.Cursor, scopeItem.Value);
keysCount++;
}

MessagePackSerializer.WriteUInt16(state.Buffer, indexForMapSize, keysCount);
};
}