Skip to content

Commit 0794ea7

Browse files
Falco20019yurishkuro
authored andcommitted
Added support for 128bit to B3 decoder (jaegertracing#144)
1 parent 18def57 commit 0794ea7

File tree

5 files changed

+43
-135
lines changed

5 files changed

+43
-135
lines changed

src/Jaeger/Propagation/B3TextMapCodec.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ public class B3TextMapCodec : Codec<ITextMap>
2727

2828
protected override void Inject(SpanContext spanContext, ITextMap carrier)
2929
{
30-
carrier.Set(TraceIdName, HexCodec.ToLowerHex(spanContext.TraceId.High, spanContext.TraceId.Low));
30+
carrier.Set(TraceIdName, spanContext.TraceId.ToString());
3131
if (spanContext.ParentId != 0L)
3232
{
3333
// Conventionally, parent id == 0 means the root span
34-
carrier.Set(ParentSpanIdName, HexCodec.ToLowerHex(spanContext.ParentId));
34+
carrier.Set(ParentSpanIdName, spanContext.ParentId.ToString());
3535
}
36-
carrier.Set(SpanIdName, HexCodec.ToLowerHex(spanContext.SpanId));
36+
carrier.Set(SpanIdName, spanContext.SpanId.ToString());
3737
carrier.Set(SampledName, spanContext.IsSampled ? "1" : "0");
3838
if (spanContext.IsDebug)
3939
{
@@ -43,9 +43,9 @@ protected override void Inject(SpanContext spanContext, ITextMap carrier)
4343

4444
protected override SpanContext Extract(ITextMap carrier)
4545
{
46-
long? traceId = null;
47-
long? spanId = null;
48-
long? parentId = 0L; // Conventionally, parent id == 0 means the root span
46+
TraceId? traceId = null;
47+
SpanId? spanId = null;
48+
SpanId parentId = new SpanId(0); // Conventionally, parent id == 0 means the root span
4949
SpanContextFlags flags = SpanContextFlags.None;
5050

5151
foreach (var entry in carrier)
@@ -60,15 +60,15 @@ protected override SpanContext Extract(ITextMap carrier)
6060
}
6161
else if (string.Equals(entry.Key, TraceIdName, StringComparison.OrdinalIgnoreCase))
6262
{
63-
traceId = HexCodec.LowerHexToUnsignedLong(entry.Value);
63+
traceId = TraceId.FromString(entry.Value);
6464
}
6565
else if (string.Equals(entry.Key, ParentSpanIdName, StringComparison.OrdinalIgnoreCase))
6666
{
67-
parentId = HexCodec.LowerHexToUnsignedLong(entry.Value);
67+
parentId = SpanId.FromString(entry.Value);
6868
}
6969
else if (string.Equals(entry.Key, SpanIdName, StringComparison.OrdinalIgnoreCase))
7070
{
71-
spanId = HexCodec.LowerHexToUnsignedLong(entry.Value);
71+
spanId = SpanId.FromString(entry.Value);
7272
}
7373
else if (string.Equals(entry.Key, FlagsName, StringComparison.OrdinalIgnoreCase))
7474
{
@@ -79,9 +79,9 @@ protected override SpanContext Extract(ITextMap carrier)
7979
}
8080
}
8181

82-
if (traceId != null && parentId != null && spanId != null)
82+
if (traceId != null && spanId != null)
8383
{
84-
return new SpanContext(new TraceId(traceId.Value), new SpanId(spanId.Value), new SpanId(parentId.Value), flags);
84+
return new SpanContext(traceId.Value, spanId.Value, parentId, flags);
8585
}
8686
return null;
8787
}

src/Jaeger/Propagation/HexCodec.cs

Lines changed: 0 additions & 107 deletions
This file was deleted.

src/Jaeger/SpanContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public SpanContext WithFlags(SpanContextFlags flags)
138138

139139
/// <summary>
140140
/// Returns <c>true</c> when the instance of the context is only used to return the debug/correlation ID
141-
/// from <see cref="ITracer.Extract"/> method. This happens in the situation when "jaeger-debug-id" header is passed in
141+
/// from <see cref="ITracer.Extract{TCarrier}"/> method. This happens in the situation when "jaeger-debug-id" header is passed in
142142
/// the carrier to the extract method, but the request otherwise has no span context in it.
143143
/// Previously this would've returned <c>null</c> from the extract method, but now it returns a dummy
144144
/// context with only debugId filled in.

test/Jaeger.Tests/Propagation/B3TextMapCodecResiliencyTests.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Collections.Generic;
23
using Jaeger.Propagation;
34
using OpenTracing.Propagation;
@@ -28,10 +29,7 @@ public void ShouldFallbackWhenMaliciousInput(string headerName, string malicious
2829
{
2930
ITextMap maliciousCarrier = ValidHeaders();
3031
maliciousCarrier.Set(headerName, maliciousInput);
31-
//when
32-
SpanContext extract = _sut.Extract(maliciousCarrier);
33-
//then
34-
Assert.Null(extract);
32+
Assert.ThrowsAny<Exception>(() => _sut.Extract(maliciousCarrier));
3533
}
3634

3735
private ITextMap ValidHeaders()

test/Jaeger.Tests/Propagation/B3TextMapCodecTests.cs

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,57 @@
11
using System.Collections;
22
using System.Collections.Generic;
3+
using System.Globalization;
34
using Jaeger.Propagation;
45
using OpenTracing.Propagation;
56
using Xunit;
67

78
namespace Jaeger.Tests.Propagation
89
{
9-
/**
10-
* NOTE:
11-
* These tests are based on the ones from jaeger-b3, and included to improve the test
12-
* coverage. The main testing of the B3TextMapCodec is still performed via the tests
13-
* in the jaeger-b3 module.
14-
*
15-
*/
1610
public class B3TextMapCodecTest
1711
{
1812
B3TextMapCodec b3Codec = new B3TextMapCodec();
1913

2014
[Fact]
21-
public void Downgrades128BitTraceIdToLower64Bits()
15+
public void Supports64BitTraceId()
16+
{
17+
string upper64Bits = "463ac35c9f6413ad";
18+
string lower64Bits = "48485a3953bb6124";
19+
20+
DelegatingTextMap textMap = new DelegatingTextMap();
21+
textMap.Set(B3TextMapCodec.TraceIdName, upper64Bits);
22+
textMap.Set(B3TextMapCodec.SpanIdName, lower64Bits);
23+
textMap.Set(B3TextMapCodec.ParentSpanIdName, "0");
24+
textMap.Set(B3TextMapCodec.SampledName, "1");
25+
textMap.Set(B3TextMapCodec.FlagsName, "1");
26+
27+
SpanContext context = b3Codec.Extract(textMap);
28+
29+
Assert.Equal(0L, context.TraceId.High);
30+
Assert.Equal(long.Parse(upper64Bits, NumberStyles.HexNumber), context.TraceId.Low);
31+
Assert.Equal(long.Parse(lower64Bits, NumberStyles.HexNumber), context.SpanId);
32+
Assert.Equal(new SpanId(0), context.ParentId);
33+
Assert.True(context.Flags.HasFlag(SpanContextFlags.Sampled));
34+
Assert.True(context.Flags.HasFlag(SpanContextFlags.Debug));
35+
}
36+
37+
[Fact]
38+
public void Supports128BitTraceId()
2239
{
2340
string hex128Bits = "463ac35c9f6413ad48485a3953bb6124";
41+
string upper64Bits = "463ac35c9f6413ad";
2442
string lower64Bits = "48485a3953bb6124";
2543

2644
DelegatingTextMap textMap = new DelegatingTextMap();
2745
textMap.Set(B3TextMapCodec.TraceIdName, hex128Bits);
2846
textMap.Set(B3TextMapCodec.SpanIdName, lower64Bits);
29-
textMap.Set(B3TextMapCodec.ParentSpanIdName, "0");
3047
textMap.Set(B3TextMapCodec.SampledName, "1");
3148
textMap.Set(B3TextMapCodec.FlagsName, "1");
3249

3350
SpanContext context = b3Codec.Extract(textMap);
3451

35-
//Assert.NotNull(HexCodec.LowerHexToUnsignedLong(lower64Bits));
36-
Assert.Equal(HexCodec.LowerHexToUnsignedLong(lower64Bits), context.TraceId.Low);
37-
Assert.Equal(HexCodec.LowerHexToUnsignedLong(lower64Bits), context.SpanId);
52+
Assert.Equal(long.Parse(upper64Bits, NumberStyles.HexNumber), context.TraceId.High);
53+
Assert.Equal(long.Parse(lower64Bits, NumberStyles.HexNumber), context.TraceId.Low);
54+
Assert.Equal(long.Parse(lower64Bits, NumberStyles.HexNumber), context.SpanId);
3855
Assert.Equal(new SpanId(0), context.ParentId);
3956
Assert.True(context.Flags.HasFlag(SpanContextFlags.Sampled));
4057
Assert.True(context.Flags.HasFlag(SpanContextFlags.Debug));

0 commit comments

Comments
 (0)