Skip to content

Commit

Permalink
fix propagator usages in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaspimentel committed Oct 18, 2024
1 parent 3b57719 commit be793d8
Show file tree
Hide file tree
Showing 8 changed files with 794 additions and 639 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ public void GetDistributedTrace()

using (var scope = Tracer.Instance.StartActive("Test"))
{
var spanContext = SpanContextPropagator.Instance.Extract(automaticTracer.GetDistributedTrace());
var context = SpanContextPropagator.Instance.Extract(automaticTracer.GetDistributedTrace());
var spanContext = context.SpanContext!;

spanContext.Should().NotBeNull();
spanContext.TraceId128.Should().Be(((Scope)scope).Span.TraceId128);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc.
// </copyright>

using System.Collections.Generic;
using Datadog.Trace.Headers;
using Datadog.Trace.Propagators;
using FluentAssertions;
Expand All @@ -15,12 +16,22 @@ public class B3MultipleHeadersPropagatorTests
{
private static readonly SpanContextPropagator B3Propagator;

private static readonly Baggage TestBaggage;

static B3MultipleHeadersPropagatorTests()
{
B3Propagator = SpanContextPropagatorFactory.GetSpanContextPropagator(
[ContextPropagationHeaderStyle.B3MultipleHeaders],
[ContextPropagationHeaderStyle.B3MultipleHeaders],
false);

var baggageItems = new KeyValuePair<string, string>[]
{
new("key1", "value1"),
new("key2", "value2")
};

TestBaggage = new Baggage(baggageItems);
}

[Fact]
Expand All @@ -29,32 +40,32 @@ public void Inject_IHeadersCollection()
var traceId = new TraceId(0x0123456789abcdef, 0x1122334455667788); // 0x0123456789abcdef1122334455667788
ulong spanId = 0x000000003ade68b1;
var samplingPriority = SamplingPriorityValues.UserKeep;
var context = new SpanContext(traceId, spanId, samplingPriority, serviceName: null, null);
var spanContext1 = new SpanContext(traceId, spanId, samplingPriority, serviceName: null, null);
var headers = new Mock<IHeadersCollection>();

B3Propagator.Inject(context, headers.Object);
B3Propagator.Inject(new PropagationContext(spanContext1, TestBaggage), headers.Object);

headers.Verify(h => h.Set("x-b3-traceid", "0123456789abcdef1122334455667788"), Times.Once());
headers.Verify(h => h.Set("x-b3-spanid", "000000003ade68b1"), Times.Once());
headers.Verify(h => h.Set("x-b3-sampled", "1"), Times.Once());
headers.VerifyNoOtherCalls();

// Extract default (no sampler) sampling from trace context
var newContext = new SpanContext(parent: null, new TraceContext(Mock.Of<IDatadogTracer>()), serviceName: null, traceId, spanId);
var spanContext2 = new SpanContext(parent: null, new TraceContext(Mock.Of<IDatadogTracer>()), serviceName: null, traceId, spanId);
var newHeaders = new Mock<IHeadersCollection>();

B3Propagator.Inject(newContext, newHeaders.Object);
B3Propagator.Inject(new PropagationContext(spanContext2, TestBaggage), newHeaders.Object);

newHeaders.Verify(h => h.Set("x-b3-traceid", "0123456789abcdef1122334455667788"), Times.Once());
newHeaders.Verify(h => h.Set("x-b3-spanid", "000000003ade68b1"), Times.Once());
newHeaders.Verify(h => h.Set("x-b3-sampled", "1"), Times.Once());
newHeaders.VerifyNoOtherCalls();

// override sampling decision
newContext.TraceContext.SetSamplingPriority(SamplingPriorityValues.UserReject);
spanContext2.TraceContext.SetSamplingPriority(SamplingPriorityValues.UserReject);
newHeaders = new Mock<IHeadersCollection>();

B3Propagator.Inject(newContext, newHeaders.Object);
B3Propagator.Inject(new PropagationContext(spanContext2, TestBaggage), newHeaders.Object);

newHeaders.Verify(h => h.Set("x-b3-traceid", "0123456789abcdef1122334455667788"), Times.Once());
newHeaders.Verify(h => h.Set("x-b3-spanid", "000000003ade68b1"), Times.Once());
Expand All @@ -68,34 +79,34 @@ public void Inject_CarrierAndDelegate()
var traceId = new TraceId(0x0123456789abcdef, 0x1122334455667788); // 0x0123456789abcdef1122334455667788
ulong spanId = 0x000000003ade68b1;
var samplingPriority = SamplingPriorityValues.UserKeep;
var context = new SpanContext(traceId, spanId, samplingPriority, serviceName: null, null);
var spanContext1 = new SpanContext(traceId, spanId, samplingPriority, serviceName: null, null);

// using IHeadersCollection for convenience, but carrier could be any type
var headers = new Mock<IHeadersCollection>();

B3Propagator.Inject(context, headers.Object, (carrier, name, value) => carrier.Set(name, value));
B3Propagator.Inject(new PropagationContext(spanContext1, TestBaggage), headers.Object, (carrier, name, value) => carrier.Set(name, value));

headers.Verify(h => h.Set("x-b3-traceid", "0123456789abcdef1122334455667788"), Times.Once());
headers.Verify(h => h.Set("x-b3-spanid", "000000003ade68b1"), Times.Once());
headers.Verify(h => h.Set("x-b3-sampled", "1"), Times.Once());
headers.VerifyNoOtherCalls();

// Extract default (no sampler) sampling from trace context
var newContext = new SpanContext(parent: null, new TraceContext(Mock.Of<IDatadogTracer>()), serviceName: null, traceId, spanId);
var spanContext2 = new SpanContext(parent: null, new TraceContext(Mock.Of<IDatadogTracer>()), serviceName: null, traceId, spanId);
var newHeaders = new Mock<IHeadersCollection>();

B3Propagator.Inject(newContext, newHeaders.Object, (carrier, name, value) => carrier.Set(name, value));
B3Propagator.Inject(new PropagationContext(spanContext2, TestBaggage), newHeaders.Object, (carrier, name, value) => carrier.Set(name, value));

newHeaders.Verify(h => h.Set("x-b3-traceid", "0123456789abcdef1122334455667788"), Times.Once());
newHeaders.Verify(h => h.Set("x-b3-spanid", "000000003ade68b1"), Times.Once());
newHeaders.Verify(h => h.Set("x-b3-sampled", "1"), Times.Once());
newHeaders.VerifyNoOtherCalls();

// override sampling decision
newContext.TraceContext.SetSamplingPriority(SamplingPriorityValues.UserReject);
spanContext2.TraceContext.SetSamplingPriority(SamplingPriorityValues.UserReject);
newHeaders = new Mock<IHeadersCollection>();

B3Propagator.Inject(newContext, newHeaders.Object, (carrier, name, value) => carrier.Set(name, value));
B3Propagator.Inject(new PropagationContext(spanContext2, TestBaggage), newHeaders.Object, (carrier, name, value) => carrier.Set(name, value));

newHeaders.Verify(h => h.Set("x-b3-traceid", "0123456789abcdef1122334455667788"), Times.Once());
newHeaders.Verify(h => h.Set("x-b3-spanid", "000000003ade68b1"), Times.Once());
Expand Down Expand Up @@ -159,19 +170,22 @@ public void Extract_IHeadersCollection(
headers.Verify(h => h.GetValues("x-b3-spanid"), Times.Once());
headers.Verify(h => h.GetValues("x-b3-sampled"), Times.Once());

result.Should()
result.SpanContext
.Should()
.NotBeNull()
.And
.BeEquivalentTo(
new SpanContextMock
{
TraceId128 = new TraceId(traceIdUpper, traceIdLower),
TraceId = traceIdLower,
SpanId = spanId,
RawTraceId = rawTraceId,
RawSpanId = rawSpanId,
Origin = null,
SamplingPriority = samplingPriority,
IsRemote = true,
});
new SpanContextMock
{
TraceId128 = new TraceId(traceIdUpper, traceIdLower),
TraceId = traceIdLower,
SpanId = spanId,
RawTraceId = rawTraceId,
RawSpanId = rawSpanId,
Origin = null,
SamplingPriority = samplingPriority,
IsRemote = true,
});
}

[Theory]
Expand Down Expand Up @@ -204,19 +218,22 @@ public void Extract_CarrierAndDelegate(
headers.Verify(h => h.GetValues("x-b3-spanid"), Times.Once());
headers.Verify(h => h.GetValues("x-b3-sampled"), Times.Once());

result.Should()
result.SpanContext
.Should()
.NotBeNull()
.And
.BeEquivalentTo(
new SpanContextMock
{
TraceId128 = new TraceId(traceIdUpper, traceIdLower),
TraceId = traceIdLower,
SpanId = spanId,
RawTraceId = rawTraceId,
RawSpanId = rawSpanId,
Origin = null,
SamplingPriority = samplingPriority,
IsRemote = true,
});
new SpanContextMock
{
TraceId128 = new TraceId(traceIdUpper, traceIdLower),
TraceId = traceIdLower,
SpanId = spanId,
RawTraceId = rawTraceId,
RawSpanId = rawSpanId,
Origin = null,
SamplingPriority = samplingPriority,
IsRemote = true,
});
}

[Fact]
Expand All @@ -237,20 +254,21 @@ public void ExtractAndInject_PreserveOriginalTraceId()
var expectedTraceId = new TraceId(0x0af7651916cd43dd, 0x8448eb211c80319c);
const ulong expectedSpanId = 0x00f067aa0ba902b7UL;

var result = B3Propagator.Extract(headers.Object);
var context = B3Propagator.Extract(headers.Object);
var spanContext = context.SpanContext!;

result.Should().NotBeNull();
result!.TraceId128.Should().Be(expectedTraceId);
result.TraceId.Should().Be(expectedTraceId.Lower);
result.SpanId.Should().Be(expectedSpanId);
spanContext.Should().NotBeNull();
spanContext.TraceId128.Should().Be(expectedTraceId);
spanContext.TraceId.Should().Be(expectedTraceId.Lower);
spanContext.SpanId.Should().Be(expectedSpanId);

// Check the injection restoring the 128 bits traceId.
var headersForInjection = new Mock<IHeadersCollection>(MockBehavior.Strict);
headersForInjection.Setup(h => h.Set("x-b3-traceid", traceId));
headersForInjection.Setup(h => h.Set("x-b3-spanid", spanId));
headersForInjection.Setup(h => h.Set("x-b3-sampled", sampled));

B3Propagator.Inject(result, headersForInjection.Object);
B3Propagator.Inject(context, headersForInjection.Object);

headersForInjection.Verify(h => h.Set("x-b3-traceid", traceId), Times.Once());
headersForInjection.Verify(h => h.Set("x-b3-spanid", spanId), Times.Once());
Expand All @@ -275,7 +293,7 @@ public void Extract_InvalidLength()
headers.Verify(h => h.GetValues("x-b3-spanid"), Times.Never); // extractor doesn't get this far
headers.Verify(h => h.GetValues("x-b3-sampled"), Times.Never); // extractor doesn't get this far

result.Should().BeNull();
result.SpanContext.Should().BeNull();
}

[Fact]
Expand All @@ -295,7 +313,7 @@ public void Extract_InvalidFormat()
headers.Verify(h => h.GetValues("x-b3-spanid"), Times.Never); // extractor doesn't get this far
headers.Verify(h => h.GetValues("x-b3-sampled"), Times.Never); // extractor doesn't get this far

result.Should().BeNull();
result.SpanContext.Should().BeNull();
}

[Fact]
Expand All @@ -315,7 +333,7 @@ public void Extract_EmptyTraceId()
headers.Verify(h => h.GetValues("x-b3-spanid"), Times.Never); // extractor doesn't get this far
headers.Verify(h => h.GetValues("x-b3-sampled"), Times.Never); // extractor doesn't get this far

result.Should().BeNull();
result.SpanContext.Should().BeNull();
}

[Fact]
Expand All @@ -336,7 +354,7 @@ public void Extract_EmptySpanId()
headers.Verify(h => h.GetValues("x-b3-spanid"), Times.Once());
headers.Verify(h => h.GetValues("x-b3-sampled"), Times.Never); // extractor doesn't get this far

result.Should().BeNull();
result.SpanContext.Should().BeNull();
}

[Fact]
Expand All @@ -356,7 +374,7 @@ public void Extract_InvalidTraceId()
headers.Verify(h => h.GetValues("x-b3-spanid"), Times.Never()); // extractor doesn't get this far
headers.Verify(h => h.GetValues("x-b3-sampled"), Times.Never()); // extractor doesn't get this far

result.Should().BeNull();
result.SpanContext.Should().BeNull();
}

[Fact]
Expand All @@ -376,7 +394,7 @@ public void Extract_InvalidSpanIdLength()
headers.Verify(h => h.GetValues("x-b3-spanid"), Times.Once());
headers.Verify(h => h.GetValues("x-b3-sampled"), Times.Never); // extractor doesn't get this far

result.Should().BeNull();
result.SpanContext.Should().BeNull();
}
}
}
Loading

0 comments on commit be793d8

Please sign in to comment.