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 17, 2024
1 parent 49a454a commit c758827
Show file tree
Hide file tree
Showing 8 changed files with 724 additions and 631 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 @@ -29,32 +29,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, baggage: null), 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, baggage: null), 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, baggage: null), 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 +68,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, baggage: null), 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, baggage: null), 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, baggage: null), 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 +159,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 +207,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 +243,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 +282,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 +302,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 +322,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 +343,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 +363,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 +383,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 c758827

Please sign in to comment.