-
Notifications
You must be signed in to change notification settings - Fork 297
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
utpilla
wants to merge
6
commits into
open-telemetry:main
from
utpilla:utpilla/Optimize-serialization-for-scopes
+48
−32
Closed
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e51bf81
Code changes to avoid allocation when serializing scopes
utpilla e00020a
Use a static readonly Action
utpilla b8da57f
Add comment
utpilla 2b3bc6b
Merge branch 'main' into utpilla/Optimize-serialization-for-scopes
utpilla 29fc14f
Merge branch 'main' into utpilla/Optimize-serialization-for-scopes
utpilla 8407af7
Merge branch 'main' into utpilla/Optimize-serialization-for-scopes
utpilla File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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] | ||
{ | ||
|
@@ -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; | ||
} | ||
|
||
|
@@ -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; | ||
} | ||
|
||
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this line is moved up one then the |
||
} | ||
|
||
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); | ||
}; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you made this...
You could probably get rid of...
...and just use
exporterState
for everything the exporter needs on a thread.