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

Serialize tags/metrics in a single pass #1416

Merged
merged 1 commit into from
Apr 21, 2021
Merged
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
71 changes: 32 additions & 39 deletions src/Datadog.Trace/Tagging/TagsList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,13 @@ public override string ToString()

protected virtual IProperty<double?>[] GetAdditionalMetrics() => ArrayHelper.Empty<IProperty<double?>>();

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void WriteTag(ref byte[] bytes, ref int offset, string key, string value)
{
offset += MessagePackBinary.WriteString(ref bytes, offset, key);
offset += MessagePackBinary.WriteString(ref bytes, offset, value);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void WriteMetric(ref byte[] bytes, ref int offset, string key, double value)
{
Expand All @@ -252,48 +259,42 @@ private int WriteTags(ref byte[] bytes, int offset)

int count = 0;

var tags = Tags;
var additionalTags = GetAdditionalTags();
// We don't know the final count yet, write a fixed-size header and note the offset
var countOffset = offset;
offset += MessagePackBinary.WriteMapHeaderForceMap32Block(ref bytes, offset, 0);

foreach (var property in additionalTags)
{
if (property.Getter(this) != null)
{
count++;
}
}
var tags = Tags;

if (tags != null)
{
lock (tags)
{
count += tags.Count;

offset += MessagePackBinary.WriteMapHeader(ref bytes, offset, count);

foreach (var pair in tags)
{
offset += MessagePackBinary.WriteString(ref bytes, offset, pair.Key);
offset += MessagePackBinary.WriteString(ref bytes, offset, pair.Value);
WriteTag(ref bytes, ref offset, pair.Key, pair.Value);
lucaspimentel marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
else
{
offset += MessagePackBinary.WriteMapHeader(ref bytes, offset, count);
}

foreach (var property in additionalTags)
foreach (var property in GetAdditionalTags())
{
var value = property.Getter(this);

if (value != null)
{
offset += MessagePackBinary.WriteString(ref bytes, offset, property.Key);
offset += MessagePackBinary.WriteString(ref bytes, offset, value);
count++;
WriteTag(ref bytes, ref offset, property.Key, value);
}
}

if (count > 0)
{
// Back-patch the count
MessagePackBinary.WriteMapHeaderForceMap32Block(ref bytes, countOffset, (uint)count);
}

return offset - originalOffset;
}

Expand All @@ -305,56 +306,48 @@ private int WriteMetrics(ref byte[] bytes, int offset, Span span)

int count = 0;

if (span.IsTopLevel)
{
count++;
}
// We don't know the final count yet, write a fixed-size header and note the offset
var countOffset = offset;
offset += MessagePackBinary.WriteMapHeaderForceMap32Block(ref bytes, offset, 0);

var metrics = Metrics;
var additionalMetrics = GetAdditionalMetrics();

foreach (var property in additionalMetrics)
{
if (property.Getter(this) != null)
{
count++;
}
}

if (metrics != null)
{
lock (metrics)
{
count += metrics.Count;

offset += MessagePackBinary.WriteMapHeader(ref bytes, offset, count);

foreach (var pair in metrics)
{
WriteMetric(ref bytes, ref offset, pair.Key, pair.Value);
}
}
}
else
{
offset += MessagePackBinary.WriteMapHeader(ref bytes, offset, count);
}

foreach (var property in GetAdditionalMetrics())
{
var value = property.Getter(this);

if (value != null)
{
count++;
WriteMetric(ref bytes, ref offset, property.Key, value.Value);
}
}

if (span.IsTopLevel)
{
count++;
WriteMetric(ref bytes, ref offset, Trace.Metrics.TopLevelSpan, 1.0);
}

if (count > 0)
{
// Back-patch the count
MessagePackBinary.WriteMapHeaderForceMap32Block(ref bytes, countOffset, (uint)count);
}

return offset - originalOffset;
}
}
Expand Down