Skip to content

Commit

Permalink
rewrite again but with proper endianess
Browse files Browse the repository at this point in the history
  • Loading branch information
vandonr committed Aug 20, 2024
1 parent 08ece1e commit 0e5032b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@

using System;
using System.Data;
using System.Numerics;
using Datadog.Trace.Configuration;
using Datadog.Trace.Propagators;
using Datadog.Trace.Tagging;
using Datadog.Trace.Util;
using Datadog.Trace.VendoredMicrosoftCode.System.Buffers.Binary;

#nullable enable

Expand Down Expand Up @@ -137,11 +137,15 @@ private static byte[] BuildContextValue(byte version, bool isSampled, ulong span
var contextBytes = new byte[1 + sizeof(ulong) + TraceId.Size];

contextBytes[0] = versionAndSampling;

Buffer.BlockCopy(BitConverter.GetBytes(spanId), srcOffset: 0, contextBytes, dstOffset: 1, sizeof(ulong));

Buffer.BlockCopy(BitConverter.GetBytes(traceId.Upper), srcOffset: 0, contextBytes, 1 + sizeof(ulong), sizeof(ulong));
Buffer.BlockCopy(BitConverter.GetBytes(traceId.Lower), srcOffset: 0, contextBytes, 1 + sizeof(ulong) + sizeof(ulong), sizeof(ulong));
BinaryPrimitives.WriteUInt64BigEndian(
new VendoredMicrosoftCode.System.Span<byte>(contextBytes, start: 1, sizeof(ulong)),
spanId);
BinaryPrimitives.WriteUInt64BigEndian(
new VendoredMicrosoftCode.System.Span<byte>(contextBytes, 1 + sizeof(ulong), sizeof(ulong)),
traceId.Upper);
BinaryPrimitives.WriteUInt64BigEndian(
new VendoredMicrosoftCode.System.Span<byte>(contextBytes, 1 + sizeof(ulong) + sizeof(ulong), sizeof(ulong)),
traceId.Lower);

return contextBytes;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ public void ExpectedCommentInjectedV1()
}

[Theory]
[InlineData("full", "sqlclient", SamplingPriorityValues.UserKeep, true, "01000000000000beef0000000000000000000000000000cafe")]
[InlineData("full", "sqlclient", SamplingPriorityValues.UserReject, true, "00000000000000beef0000000000000000000000000000cafe")]
[InlineData("full", "sqlclient", SamplingPriorityValues.UserKeep, true, "01000000000000BEEF0000000000000000000000000000CAFE")]
[InlineData("full", "sqlclient", SamplingPriorityValues.UserReject, true, "00000000000000BEEF0000000000000000000000000000CAFE")]
[InlineData("nope", "sqlclient", SamplingPriorityValues.UserKeep, false, null)]
// disabled for all db types except mysql for now
[InlineData("full", "npgsql", SamplingPriorityValues.UserKeep, false, null)]
Expand All @@ -139,7 +139,7 @@ public void ExpectedContextSet(string propagationMode, string integration, int?

// capture command and parameter sent
string sql = null;
var context = BigInteger.Zero;
byte[] context = null;
var connectionMock = new Mock<IDbConnection>(MockBehavior.Strict);
var commandMock = new Mock<IDbCommand>();
var parameterMock = new Mock<IDbDataParameter>();
Expand All @@ -148,8 +148,8 @@ public void ExpectedContextSet(string propagationMode, string integration, int?
.Callback<string>(value => sql = value);
commandMock.Setup(c => c.CreateParameter()).Returns(parameterMock.Object);
commandMock.SetupGet(c => c.Parameters).Returns(Mock.Of<IDataParameterCollection>());
parameterMock.SetupSet(p => p.Value = It.IsAny<BigInteger>())
.Callback<object>(value => context = (BigInteger)value);
parameterMock.SetupSet(p => p.Value = It.IsAny<byte[]>())
.Callback<object>(value => context = (byte[])value);

var span = _v0Tracer.StartSpan("mysql.query", parent: SpanContext.None, serviceName: "pouet", traceId: (TraceId)0xCAFE, spanId: 0xBEEF);
span.SetTraceSamplingPriority((SamplingPriority)samplingPriority.Value);
Expand All @@ -159,12 +159,12 @@ public void ExpectedContextSet(string propagationMode, string integration, int?
if (shouldInject)
{
sql.Should().StartWith("set context_info ");
context.ToString("x50").Should().Be(expectedContext);
BitConverter.ToString(context).Replace("-", string.Empty).Should().Be(expectedContext);
}
else
{
sql.Should().BeNull();
context.IsZero.Should().BeTrue();
context.Should().BeNull();
}
}
}
Expand Down

0 comments on commit 0e5032b

Please sign in to comment.