Skip to content
This repository has been archived by the owner on Apr 28, 2022. It is now read-only.

Commit

Permalink
Added 128bit trace id configuration support (#94)
Browse files Browse the repository at this point in the history
* Added 128bit trace id configuration support
* Updated README.md

Signed-off-by: Benjamin Krämer <falco20019@hotmail.com>
  • Loading branch information
Falco20019 committed Jul 19, 2018
1 parent 4b5d0bf commit e537787
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 9 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ JAEGER_SAMPLER_TYPE | no | The sampler type
JAEGER_SAMPLER_PARAM | no | The sampler parameter (number)
JAEGER_SAMPLER_MANAGER_HOST_PORT | no | The host name and port when using the remote controlled sampler
JAEGER_TAGS | no | A comma separated list of `name = value` tracer level tags, which get added to all reported spans. The value can also refer to an environment variable using the format `${envVarName:default}`, where the `:default` is optional, and identifies a value to be used if the environment variable cannot be found
JAEGER_TRACEID_128BIT | no | Whether to use 128bit TraceID instead of 64bit

Setting `JAEGER_AGENT_HOST`/`JAEGER_AGENT_PORT` will make the client send traces to the agent via `UdpSender`.
If the `JAEGER_ENDPOINT` environment variable is also set, the traces are sent to the endpoint, effectively making
Expand Down
20 changes: 19 additions & 1 deletion src/Jaeger/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public class Configuration
/// <summary>
/// The sampler parameter (number).
/// </summary>
public const string JaegerSamplerParam = "JAEGER_SAMPLER_PARAM";
public const string JaegerSamplerParam = JaegerPrefix + "SAMPLER_PARAM";

/// <summary>
/// The sampler manager host:port.
Expand All @@ -96,6 +96,11 @@ public class Configuration
/// </summary>
public const string JaegerTags = JaegerPrefix + "TAGS";

/// <summary>
/// Whether to use 128bit TraceID instead of 64bit.
/// </summary>
public const string JaegerTraceId128Bit = JaegerPrefix + "TRACEID_128BIT";

/// <summary>
/// Comma separated list of formats to use for propagating the trace context. Default will the
/// standard Jaeger format. Valid values are jaeger and b3.
Expand Down Expand Up @@ -129,6 +134,7 @@ public enum Propagation
private CodecConfiguration _codecConfig;
private IMetricsFactory _metricsFactory;
private Dictionary<string, string> _tracerTags;
private bool _useTraceId128Bit;

/// <summary>
/// Lazy singleton <see cref="Tracer"/> initialized in <see cref="GetTracer()"/> method.
Expand All @@ -154,6 +160,7 @@ public static Configuration FromEnv(ILoggerFactory loggerFactory)

return new Configuration(GetProperty(JaegerServiceName), loggerFactory)
.WithTracerTags(TracerTagsFromEnv(logger))
.WithTraceId128Bit(GetPropertyAsBool(JaegerTraceId128Bit, logger).GetValueOrDefault(false))
.WithReporter(ReporterConfiguration.FromEnv(loggerFactory))
.WithSampler(SamplerConfiguration.FromEnv(loggerFactory))
.WithCodec(CodecConfiguration.FromEnv(loggerFactory));
Expand Down Expand Up @@ -187,6 +194,11 @@ public Tracer.Builder GetTracerBuilder()
.WithMetrics(metrics)
.WithTags(_tracerTags);

if (_useTraceId128Bit)
{
builder = builder.WithTraceId128Bit();
}

_codecConfig.Apply(builder);

return builder;
Expand Down Expand Up @@ -245,6 +257,12 @@ public Configuration WithCodec(CodecConfiguration codecConfig)
return this;
}

private Configuration WithTraceId128Bit(bool useTraceId128Bit)
{
_useTraceId128Bit = useTraceId128Bit;
return this;
}

public Configuration WithTracerTags(Dictionary<string, string> tracerTags)
{
if (tracerTags != null)
Expand Down
2 changes: 1 addition & 1 deletion src/Jaeger/SpanBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public ISpan Start()

private SpanContext CreateNewContext(string debugId)
{
TraceId traceId = TraceId.NewUniqueId();
TraceId traceId = TraceId.NewUniqueId(_tracer.UseTraceId128Bit);
SpanId spanId = new SpanId(traceId);

var flags = SpanContextFlags.None;
Expand Down
7 changes: 4 additions & 3 deletions src/Jaeger/TraceId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ namespace Jaeger
public long Low { get; }
public bool IsZero => Low == 0 && High == 0;

public static TraceId NewUniqueId()
public static TraceId NewUniqueId(bool useHigh)
{
// TODO: Only using 64bit for compatability. Make it configurable to use 128bit TraceIds.
return new TraceId(Utils.UniqueId());
var high = useHigh ? Utils.UniqueId() : 0;
var low = Utils.UniqueId();
return new TraceId(high, low);
}

public TraceId(long low) : this(0, low)
Expand Down
15 changes: 13 additions & 2 deletions src/Jaeger/Tracer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public class Tracer : ITracer, IDisposable
public string Version { get; }
public bool ZipkinSharedRpcSpan { get; }
public bool ExpandExceptionLogs { get; }
public bool UseTraceId128Bit { get; }
public long IPv4 { get; }

private Tracer(
Expand All @@ -56,7 +57,8 @@ public class Tracer : ITracer, IDisposable
bool zipkinSharedRpcSpan,
IScopeManager scopeManager,
IBaggageRestrictionManager baggageRestrictionManager,
bool expandExceptionLogs)
bool expandExceptionLogs,
bool useTraceId128Bit)
{
ServiceName = serviceName;
Reporter = reporter;
Expand All @@ -69,6 +71,7 @@ public class Tracer : ITracer, IDisposable
ScopeManager = scopeManager;
_baggageSetter = new BaggageSetter(baggageRestrictionManager, metrics);
ExpandExceptionLogs = expandExceptionLogs;
UseTraceId128Bit = useTraceId128Bit;

Version = LoadVersion();
tags[Constants.JaegerClientVersionTagKey] = Version;
Expand Down Expand Up @@ -122,6 +125,7 @@ public override string ToString()
sb.Append($"Tags={string.Join(", ", Tags)}, ");
sb.Append($"ZipkinSharedRpcSpan={ZipkinSharedRpcSpan}, ");
sb.Append($"ExpandExceptionLogs={ExpandExceptionLogs}");
sb.Append($"UseTraceId128Bit={UseTraceId128Bit}");
sb.Append(')');
return sb.ToString();
}
Expand Down Expand Up @@ -207,6 +211,7 @@ public sealed class Builder
private IScopeManager _scopeManager = new AsyncLocalScopeManager();
private IBaggageRestrictionManager _baggageRestrictionManager = new DefaultBaggageRestrictionManager();
private bool _expandExceptionLogs;
private bool _useTraceId128Bit;

// We need the loggerFactory for the PropagationRegistry so we have to defer these invocations.
private readonly List<Action<PropagationRegistry>> _registryActions = new List<Action<PropagationRegistry>>();
Expand Down Expand Up @@ -293,6 +298,12 @@ public Builder WithExpandExceptionLogs()
return this;
}

public Builder WithTraceId128Bit()
{
_useTraceId128Bit = true;
return this;
}

public Builder WithTag(string key, bool value)
{
_tags[key] = value;
Expand Down Expand Up @@ -369,7 +380,7 @@ public Tracer Build()
}

return new Tracer(_serviceName, _reporter, _sampler, _registry, _clock, _metrics, _loggerFactory,
_tags, _zipkinSharedRpcSpan, _scopeManager, _baggageRestrictionManager, _expandExceptionLogs);
_tags, _zipkinSharedRpcSpan, _scopeManager, _baggageRestrictionManager, _expandExceptionLogs, _useTraceId128Bit);
}

public static String CheckValidServiceName(String serviceName)
Expand Down
19 changes: 19 additions & 0 deletions test/Jaeger.Tests/ConfigurationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ private void ClearProperties()
ClearProperty(Configuration.JaegerSamplerManagerHostPort);
ClearProperty(Configuration.JaegerServiceName);
ClearProperty(Configuration.JaegerTags);
ClearProperty(Configuration.JaegerTraceId128Bit);
ClearProperty(Configuration.JaegerEndpoint);
ClearProperty(Configuration.JaegerAuthToken);
ClearProperty(Configuration.JaegerUser);
Expand Down Expand Up @@ -133,6 +134,24 @@ public void TestReporterConfigurationInvalidLogSpans()
Assert.False(reporterConfig.LogSpans);
}

[Fact]
public void TestTracerUseTraceIdHigh()
{
SetProperty(Configuration.JaegerServiceName, "Test");
SetProperty(Configuration.JaegerTraceId128Bit, "1");
Tracer tracer = (Tracer)Configuration.FromEnv(_loggerFactory).GetTracer();
Assert.True(tracer.UseTraceId128Bit);
}

[Fact]
public void TestTracerInvalidUseTraceIdHigh()
{
SetProperty(Configuration.JaegerServiceName, "Test");
SetProperty(Configuration.JaegerTraceId128Bit, "X");
Tracer tracer = (Tracer)Configuration.FromEnv(_loggerFactory).GetTracer();
Assert.False(tracer.UseTraceId128Bit);
}

[Fact]
public void TestTracerTagslist()
{
Expand Down
35 changes: 33 additions & 2 deletions test/Jaeger.Tests/SpanTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,37 @@ public SpanTests()
span = (Span)tracer.BuildSpan("some-operation").Start();
}

[Fact]
public void TestTraceId64Bit()
{
var tracer = new Tracer.Builder("name")
.Build();

// no exception
var span = tracer.BuildSpan("foo").Start();
span.Finish();

var spanContext = span.Context as SpanContext;
Assert.NotNull(spanContext);
Assert.True(spanContext.TraceId.High == 0);
}

[Fact]
public void TestTraceId128Bit()
{
var tracer = new Tracer.Builder("name")
.WithTraceId128Bit()
.Build();

// no exception
var span = tracer.BuildSpan("foo").Start();
span.Finish();

var spanContext = span.Context as SpanContext;
Assert.NotNull(spanContext);
Assert.True(spanContext.TraceId.High != 0);
}

[Fact]
public void TestSpanMetrics()
{
Expand All @@ -51,13 +82,13 @@ public void TestSetAndGetBaggageItem()
{
string service = "SamplerTest";
IBaggageRestrictionManager mgr = Substitute.ForPartsOf<DefaultBaggageRestrictionManager>();
tracer = new Tracer.Builder(service)
var tracer = new Tracer.Builder(service)
.WithReporter(reporter)
.WithSampler(new ConstSampler(true))
.WithClock(clock)
.WithBaggageRestrictionManager(mgr)
.Build();
span = (Span)tracer.BuildSpan("some-operation").Start();
var span = (Span)tracer.BuildSpan("some-operation").Start();

string key = "key";
string value = "value";
Expand Down
19 changes: 19 additions & 0 deletions test/Jaeger.Tests/TracerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,25 @@ public void TestDefaultConstructor()
tracer.BuildSpan("foo").Start().Finish();
}

[Fact]
public void TestTraceId64Bit()
{
var tracer = new Tracer.Builder("name")
.Build();

Assert.False(tracer.UseTraceId128Bit);
}

[Fact]
public void TestTraceId128Bit()
{
var tracer = new Tracer.Builder("name")
.WithTraceId128Bit()
.Build();

Assert.True(tracer.UseTraceId128Bit);
}

[Fact]
public void TestBuildSpan()
{
Expand Down

0 comments on commit e537787

Please sign in to comment.