Skip to content
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
25 changes: 18 additions & 7 deletions tracer/src/Datadog.Trace/Agent/StatsAggregator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -365,9 +365,7 @@ internal StatsAggregationKey BuildKey(Span span, List<PeerTagKey> peerTagKeys, o
/// The <paramref name="keyPrefix"/> is a pre-encoded UTF-8 byte array (e.g. "tagKey:") and is
/// hashed directly. Only the <paramref name="tagValue"/> needs UTF-8 encoding at call time.
/// </summary>
#if NETCOREAPP
[System.Runtime.CompilerServices.SkipLocalsInit]
#endif
[SkipLocalsInit]
private static ulong HashTag(byte[] keyPrefix, string tagValue, FnvHash64.Version version, ulong? initialHash = null)
{
// Hash the pre-encoded key prefix (e.g. "peer.service:") directly — no encoding needed
Expand All @@ -377,22 +375,35 @@ private static ulong HashTag(byte[] keyPrefix, string tagValue, FnvHash64.Versio

// Now encode and hash just the tag value
var maxByteCount = EncodingHelpers.Utf8NoBom.GetMaxByteCount(tagValue.Length);
#if NETCOREAPP
const int maxStackLimit = 256;

if (maxByteCount <= maxStackLimit)
{
Span<byte> buffer = stackalloc byte[maxStackLimit];
var written = EncodingHelpers.Utf8NoBom.GetBytes(tagValue, buffer);
int written;
#if NETCOREAPP
written = EncodingHelpers.Utf8NoBom.GetBytes(tagValue, buffer);
#else
unsafe
{
var tagValueSpan = tagValue.AsSpan();
fixed (char* tagValuePointer = tagValueSpan)
{
fixed (byte* bufferPointer = buffer)
{
written = EncodingHelpers.Utf8NoBom.GetBytes(tagValuePointer, tagValueSpan.Length, bufferPointer, buffer.Length);
}
}
}
#endif
return FnvHash64.GenerateHash(buffer.Slice(0, written), version, hash);
}
#endif

var rented = ArrayPool<byte>.Shared.Rent(maxByteCount);
try
{
var written = EncodingHelpers.Utf8NoBom.GetBytes(tagValue, charIndex: 0, charCount: tagValue.Length, rented, byteIndex: 0);
return FnvHash64.GenerateHash(rented, 0, written, version, hash);
return FnvHash64.GenerateHash(rented.AsSpan().Slice(0, written), version, hash);
}
finally
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@
using Datadog.Trace.ClrProfiler.AutoInstrumentation.AWS.DynamoDb;
using Datadog.Trace.Util;

#if NETCOREAPP3_1_OR_GREATER
using System.Buffers;
#else
using Datadog.Trace.VendoredMicrosoftCode.System.Buffers;
#endif

namespace Datadog.Trace.ClrProfiler.AutoInstrumentation.AWS.Shared;

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,24 +61,12 @@ public static NodeHash CalculateNodeHash(in NodeHashBase baseNodeHash, IEnumerab
return new NodeHash(hash);
}

#if NETCOREAPP3_1_OR_GREATER
[System.Runtime.CompilerServices.SkipLocalsInit]
#endif
public static PathwayHash CalculatePathwayHash(NodeHash nodeHash, PathwayHash parentHash)
{
#if NETCOREAPP3_1_OR_GREATER
Span<byte> bytes = stackalloc byte[16];
System.Buffers.Binary.BinaryPrimitives.WriteUInt64LittleEndian(bytes, nodeHash.Value);
System.Buffers.Binary.BinaryPrimitives.WriteUInt64LittleEndian(bytes.Slice(8), parentHash.Value);
BinaryPrimitives.WriteUInt64LittleEndian(bytes, nodeHash.Value);
BinaryPrimitives.WriteUInt64LittleEndian(bytes.Slice(8), parentHash.Value);
return new PathwayHash(FnvHash64.GenerateHash(bytes, HashVersion));
#else
// annoyingly allocate-y, but meh
var bytes = new byte[8];
BinaryPrimitivesHelper.WriteUInt64LittleEndian(bytes, nodeHash.Value);
var hash = FnvHash64.GenerateHash(bytes, HashVersion);

BinaryPrimitivesHelper.WriteUInt64LittleEndian(bytes, parentHash.Value);
return new PathwayHash(FnvHash64.GenerateHash(bytes, HashVersion, hash));
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ public static int WriteVarLong(byte[] bytes, int offset, ulong value)
public static int WriteVarLong(BinaryWriter writer, ulong value)
=> WriteVarLong(value, new BinaryWriterByteWriter(writer));

#if NETCOREAPP3_1_OR_GREATER
/// <summary>
/// Serializes 64-bit unsigned integers 7 bits at a time,
/// starting with the least significant bits. The most significant bit in each
Expand All @@ -95,7 +94,6 @@ public static int WriteVarLong(Span<byte> bytes, ulong value)
bytes[length - 1] = (byte)(value);
return length;
}
#endif

/// <summary>
/// Serializes 64-bit signed integers using zig-zag encoding,
Expand All @@ -122,7 +120,6 @@ public static int WriteVarLongZigZag(byte[] bytes, int offset, long value)
public static int WriteVarLongZigZag(BinaryWriter writer, long value)
=> WriteVarLong(writer, ZigZag(value));

#if NETCOREAPP3_1_OR_GREATER
/// <summary>
/// Serializes 64-bit signed integers using zig-zag encoding,
/// which ensures small-scale integers are turned into unsigned integers
Expand All @@ -134,7 +131,6 @@ public static int WriteVarLongZigZag(BinaryWriter writer, long value)
/// <returns>The number of bytes written</returns>
public static int WriteVarLongZigZag(Span<byte> bytes, long value)
=> WriteVarLong(bytes, ZigZag(value));
#endif

/// <summary>
/// Serializes 32-bit unsigned integers 7 bits at a time,
Expand Down Expand Up @@ -165,7 +161,6 @@ public static int WriteVarInt(byte[] bytes, int offset, uint value)
public static int WriteVarInt(BinaryWriter writer, uint value)
=> WriteVarLong(value, new BinaryWriterByteWriter(writer));

#if NETCOREAPP3_1_OR_GREATER
/// <summary>
/// Serializes 32-bit unsigned integers 7 bits at a time,
/// starting with the least significant bits. The most significant bit in each
Expand All @@ -179,7 +174,6 @@ public static int WriteVarInt(BinaryWriter writer, uint value)
/// <returns>The number of bytes written</returns>
public static int WriteVarInt(Span<byte> bytes, uint value)
=> WriteVarLong(bytes, value);
#endif

/// <summary>
/// Serializes 32-bit signed integers using zig-zag encoding,
Expand All @@ -206,7 +200,6 @@ public static int WriteVarIntZigZag(byte[] bytes, int offset, int value)
public static int WriteVarIntZigZag(BinaryWriter writer, int value)
=> WriteVarLongZigZag(writer, value);

#if NETCOREAPP3_1_OR_GREATER
/// <summary>
/// Serializes 32-bit signed integers using zig-zag encoding,
/// which ensures small-scale integers are turned into unsigned integers
Expand All @@ -218,7 +211,6 @@ public static int WriteVarIntZigZag(BinaryWriter writer, int value)
/// <returns>The number of bytes written</returns>
public static int WriteVarIntZigZag(Span<byte> bytes, int value)
=> WriteVarLongZigZag(bytes, value);
#endif

/// <summary>
/// Deserializes 64-bit unsigned integers that have been encoded using
Expand Down Expand Up @@ -252,7 +244,6 @@ public static int WriteVarIntZigZag(Span<byte> bytes, int value)
return null;
}

#if NETCOREAPP3_1_OR_GREATER
/// <summary>
/// Deserializes 64-bit unsigned integers that have been encoded using
/// <see cref="WriteVarLong(byte[],int,ulong)"/>
Expand Down Expand Up @@ -284,7 +275,6 @@ public static int WriteVarIntZigZag(Span<byte> bytes, int value)
bytesRead = default;
return null;
}
#endif

/// <summary>
/// Deserializes 64-bit unsigned integers that have been encoded using
Expand All @@ -297,7 +287,6 @@ public static int WriteVarIntZigZag(Span<byte> bytes, int value)
public static long? ReadVarLongZigZag(byte[] bytes, int offset, out int bytesRead)
=> ReadVarLong(bytes, offset, out bytesRead) is { } decoded ? UnZigZag(decoded) : null;

#if NETCOREAPP3_1_OR_GREATER
/// <summary>
/// Deserializes 64-bit unsigned integers that have been encoded using
/// <see cref="WriteVarLongZigZag(byte[],int,long)"/>
Expand All @@ -307,7 +296,6 @@ public static int WriteVarIntZigZag(Span<byte> bytes, int value)
/// <returns>The decoded value, or null if decoding failed</returns>
public static long? ReadVarLongZigZag(Span<byte> bytes, out int bytesRead)
=> ReadVarLong(bytes, out bytesRead) is { } decoded ? UnZigZag(decoded) : null;
#endif

/// <summary>
/// Deserializes 32-bit unsigned integers that have been encoded using
Expand All @@ -322,7 +310,6 @@ public static int WriteVarIntZigZag(Span<byte> bytes, int value)
? (uint)decoded
: null;

#if NETCOREAPP3_1_OR_GREATER
/// <summary>
/// Deserializes 64-bit unsigned integers that have been encoded using
/// <see cref="WriteVarInt(byte[],int,uint)" />
Expand All @@ -334,7 +321,6 @@ public static int WriteVarIntZigZag(Span<byte> bytes, int value)
=> ReadVarLong(bytes, out bytesRead) is { } decoded and <= int.MaxValue
? (uint)decoded
: null;
#endif

/// <summary>
/// Deserializes 64-bit unsigned integers that have been encoded using
Expand All @@ -349,7 +335,6 @@ public static int WriteVarIntZigZag(Span<byte> bytes, int value)
? (int)decoded
: null;

#if NETCOREAPP3_1_OR_GREATER
/// <summary>
/// Deserializes 64-bit unsigned integers that have been encoded using
/// <see cref="WriteVarIntZigZag(byte[],int,int)" />
Expand All @@ -361,7 +346,6 @@ public static int WriteVarIntZigZag(Span<byte> bytes, int value)
=> ReadVarLongZigZag(bytes, out bytesRead) is { } decoded and >= int.MinValue and <= int.MaxValue
? (int)decoded
: null;
#endif

/// <summary>
/// Returns the number of bytes that <see cref="WriteVarIntZigZag(byte[],int,int)"/> encodes a value into.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,7 @@ public static string ToUUID(this string input)
// note that we _could_ get a UUID from these bytes by trivially doing
// new Guid(bytes).ToString("d") but that uses a slightly different
// layout of the bytes, which would cause a different UUID to be generated
#if NETCOREAPP
Span<char> chars = stackalloc char[36];
#else
var chars = new char[36];
#endif
var targetIndex = 0;

for (var sourceIndex = 0; sourceIndex < 16; sourceIndex++)
Expand All @@ -51,7 +47,11 @@ public static string ToUUID(this string input)
chars[targetIndex++] = Hex[sourceByte & 0xF];
}

#if NETCOREAPP
return new string(chars);
#else
return chars.ToString();
#endif
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Text;
using Datadog.Trace.DuckTyping;
using Datadog.Trace.SourceGenerators;
using Datadog.Trace.Util;
Expand Down Expand Up @@ -128,14 +127,9 @@ internal static string SimplifyRoutePattern(
+ (string.IsNullOrEmpty(actionName) ? 0 : Math.Max(actionName!.Length - 6, 0)) // "action".Length
+ 1; // '/' prefix

#if NETCOREAPP
var sb = maxSize < 512
? new ValueStringBuilder(stackalloc char[512])
: new ValueStringBuilder(); // too big to use stackallocation, so use array builder
#else
// In .NET Core 2.1, the ValueStringBuilder doesn't actually improve anything
var sb = StringBuilderCache.Acquire(maxSize);
#endif

foreach (var pathSegment in routePattern.PathSegments)
{
Expand Down Expand Up @@ -227,23 +221,13 @@ internal static string SimplifyRoutePattern(
}
}

#if NETCOREAPP
if (sb.Length <= 1)
{
sb.Dispose();
return "/";
}

return sb.ToString();
#else
if (sb.Length <= 1)
{
StringBuilderCache.Release(sb);
return "/";
}

return StringBuilderCache.GetStringAndRelease(sb).ToLowerInvariant();
#endif
}

internal static string SimplifyRouteTemplate(
Expand All @@ -261,14 +245,9 @@ internal static string SimplifyRouteTemplate(
+ (string.IsNullOrEmpty(actionName) ? 0 : Math.Max(actionName!.Length - 6, 0)) // "action".Length
+ 1; // '/' prefix

#if NETCOREAPP
var sb = maxSize < 512
? new ValueStringBuilder(stackalloc char[512])
: new ValueStringBuilder(); // too big to use stackallocation, so use array builder
#else
// In .NET Core 2.1, the ValueStringBuilder doesn't actually improve anything
var sb = StringBuilderCache.Acquire(maxSize);
#endif

// Remove the boxing of the enumerator
// In all versions of .NET, this is implemented as a List<TemplateSegment>
Expand Down Expand Up @@ -367,23 +346,13 @@ internal static string SimplifyRouteTemplate(
}

// We never added anything, or we just added the first `/`, no need for explicit ToString()
#if NETCOREAPP
if (sb.Length <= 1)
{
sb.Dispose();
return "/";
}

return sb.ToString();
#else
if (sb.Length <= 1)
{
StringBuilderCache.Release(sb);
return "/";
}

return StringBuilderCache.GetStringAndRelease(sb).ToLowerInvariant();
#endif
}

[TestingAndPrivateOnly]
Expand Down Expand Up @@ -412,10 +381,5 @@ internal static bool IsIdentifierSegment(object? value, out string? valueAsStrin

return UriHelpers.IsIdentifierSegment(valueAsString, 0, valueAsString.Length);
}

#if !NETCOREAPP
// .NET Core 2.1 helper which doesn't _actually_ append as lower invariant, and just does it all at the end instead
private static void AppendAsLowerInvariant(this StringBuilder sb, string? value) => sb.Append(value);
#endif
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ public bool TryExtract<TCarrier, TCarrierGetter>(TCarrier carrier, TCarrierGette
return false;
}

#if NETCOREAPP
ReadOnlySpan<char> rawTraceId;
ReadOnlySpan<char> rawSpanId;
char rawSampled;
Expand Down Expand Up @@ -100,45 +99,6 @@ public bool TryExtract<TCarrier, TCarrierGetter>(TCarrier carrier, TCarrierGette

var samplingPriority = rawSampled == '1' ? 1 : 0;
var spanContext = new SpanContext(traceId, parentId, samplingPriority, serviceName: null, null, rawTraceId.ToString(), rawSpanId.ToString(), isRemote: true);
#else
string? rawTraceId;
string? rawSpanId;
char rawSampled;

if (brValue.Length > 50 && brValue[32] == '-' && brValue[49] == '-')
{
// 128-bit trace id
rawTraceId = brValue.Substring(0, 32);
rawSpanId = brValue.Substring(33, 16);
rawSampled = brValue[50];
}
else if (brValue.Length > 34 && brValue[16] == '-' && brValue[33] == '-')
{
// 64-bit trace id
rawTraceId = brValue.Substring(0, 16);
rawSpanId = brValue.Substring(17, 16);
rawSampled = brValue[34];
}
else
{
return false;
}

var success = HexString.TryParseTraceId(rawTraceId, out var traceId);

if (!success || traceId == TraceId.Zero)
{
return false;
}

if (!HexString.TryParseUInt64(rawSpanId, out var parentId))
{
parentId = 0;
}

var samplingPriority = rawSampled == '1' ? 1 : 0;
var spanContext = new SpanContext(traceId, parentId, samplingPriority, serviceName: null, null, rawTraceId, rawSpanId, isRemote: true);
#endif

context = new PropagationContext(spanContext, baggage: null);

Expand Down
Loading
Loading