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

Commit

Permalink
CodecConfiguration without FromEnv usable (#102)
Browse files Browse the repository at this point in the history
Signed-off-by: Kraemer, Benjamin <falco20019@hotmail.com>
  • Loading branch information
Falco20019 committed Oct 4, 2018
1 parent 62a2010 commit 68dac31
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 24 deletions.
52 changes: 30 additions & 22 deletions src/Jaeger/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public Tracer.Builder GetTracerBuilder()
}
if (_codecConfig == null)
{
_codecConfig = new CodecConfiguration(new Dictionary<IFormat<ITextMap>, List<Codec<ITextMap>>>());
_codecConfig = new CodecConfiguration(_loggerFactory);
}
if (_metricsFactory == null)
{
Expand Down Expand Up @@ -367,56 +367,64 @@ public SamplerConfiguration WithManagerHostPort(string managerHostPort)
/// </summary>
public class CodecConfiguration
{
private readonly ILogger _logger;
private readonly IDictionary<IFormat<ITextMap>, List<Codec<ITextMap>>> _codecs;

internal CodecConfiguration(IDictionary<IFormat<ITextMap>, List<Codec<ITextMap>>> codecs)
public CodecConfiguration(ILoggerFactory loggerFactory)
{
_codecs = codecs;
_logger = loggerFactory.CreateLogger<Configuration>();
_codecs = new Dictionary<IFormat<ITextMap>, List<Codec<ITextMap>>>();
}

public static CodecConfiguration FromEnv(ILoggerFactory loggerFactory)
{
ILogger logger = loggerFactory.CreateLogger<Configuration>();

var codecs = new Dictionary<IFormat<ITextMap>, List<Codec<ITextMap>>>();
CodecConfiguration codecConfiguration = new CodecConfiguration(loggerFactory);
string propagation = GetProperty(JaegerPropagation);
if (propagation != null)
{
foreach (string format in propagation.Split(','))
{
if (Enum.TryParse<Propagation>(format, true, out var propagationEnum))
{
switch (propagationEnum)
{
case Propagation.Jaeger:
AddCodec(codecs, BuiltinFormats.HttpHeaders, new TextMapCodec(true));
AddCodec(codecs, BuiltinFormats.TextMap, new TextMapCodec(false));
break;
case Propagation.B3:
AddCodec(codecs, BuiltinFormats.HttpHeaders, new B3TextMapCodec());
AddCodec(codecs, BuiltinFormats.TextMap, new B3TextMapCodec());
break;
default:
logger.LogError("Unhandled propagation format {format}", format);
break;
}
codecConfiguration.WithPropagation(propagationEnum);
}
else
{
logger.LogError("Unknown propagation format {format}", format);
}
}
}
return new CodecConfiguration(codecs);
return codecConfiguration;
}

public CodecConfiguration WithPropagation(Propagation propagation)
{
switch (propagation)
{
case Propagation.Jaeger:
AddCodec(BuiltinFormats.HttpHeaders, new TextMapCodec(true));
AddCodec(BuiltinFormats.TextMap, new TextMapCodec(false));
break;
case Propagation.B3:
AddCodec(BuiltinFormats.HttpHeaders, new B3TextMapCodec());
AddCodec(BuiltinFormats.TextMap, new B3TextMapCodec());
break;
default:
_logger.LogError("Unhandled propagation {propagation}", propagation);
break;
}
return this;
}

private static void AddCodec(IDictionary<IFormat<ITextMap>, List<Codec<ITextMap>>> codecs, IFormat<ITextMap> format, Codec<ITextMap> codec)
private void AddCodec(IFormat<ITextMap> format, Codec<ITextMap> codec)
{
List<Codec<ITextMap>> codecList;
if (!codecs.TryGetValue(format, out codecList))
if (!_codecs.TryGetValue(format, out codecList))
{
codecList = new List<Codec<ITextMap>>();
codecs.Add(format, codecList);
_codecs.Add(format, codecList);
}
codecList.Add(codec);
}
Expand Down
55 changes: 53 additions & 2 deletions test/Jaeger.Tests/ConfigurationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,32 @@ public void TestSenderWithAllPropertiesReturnsHttpSender()
}

[Fact]
public void TestPropagationB3Only()
public void TestPropagationB3OnlyFromConfig()
{
TraceId traceId = new TraceId(1234);
SpanId spanId = new SpanId(5678);

TestTextMap textMap = new TestTextMap();
SpanContext spanContext = new SpanContext(traceId, spanId, new SpanId(0), (byte)0);

CodecConfiguration codecConfiguration = new CodecConfiguration(_loggerFactory)
.WithPropagation(Configuration.Propagation.B3);
ITracer tracer = new Configuration("Test", _loggerFactory)
.WithCodec(codecConfiguration)
.GetTracer();
tracer.Inject(spanContext, BuiltinFormats.TextMap, textMap);

Assert.NotNull(textMap.Get("X-B3-TraceId"));
Assert.NotNull(textMap.Get("X-B3-SpanId"));
Assert.Null(textMap.Get("uber-trace-id"));

SpanContext extractedContext = (SpanContext)tracer.Extract(BuiltinFormats.TextMap, textMap);
Assert.Equal(traceId, extractedContext.TraceId);
Assert.Equal(spanId, extractedContext.SpanId);
}

[Fact]
public void TestPropagationB3OnlyFromEnv()
{
SetProperty(Configuration.JaegerPropagation, "b3");
SetProperty(Configuration.JaegerServiceName, "Test");
Expand All @@ -288,7 +313,33 @@ public void TestPropagationB3Only()
}

[Fact]
public void TestPropagationJaegerAndB3()
public void TestPropagationJaegerAndB3FromConfig()
{
TraceId traceId = new TraceId(1234);
SpanId spanId = new SpanId(5678);

TestTextMap textMap = new TestTextMap();
SpanContext spanContext = new SpanContext(traceId, spanId, new SpanId(0), (byte)0);

CodecConfiguration codecConfiguration = new CodecConfiguration(_loggerFactory)
.WithPropagation(Configuration.Propagation.Jaeger)
.WithPropagation(Configuration.Propagation.B3);
ITracer tracer = new Configuration("Test", _loggerFactory)
.WithCodec(codecConfiguration)
.GetTracer();
tracer.Inject(spanContext, BuiltinFormats.TextMap, textMap);

Assert.NotNull(textMap.Get("uber-trace-id"));
Assert.NotNull(textMap.Get("X-B3-TraceId"));
Assert.NotNull(textMap.Get("X-B3-SpanId"));

SpanContext extractedContext = (SpanContext)tracer.Extract(BuiltinFormats.TextMap, textMap);
Assert.Equal(traceId, extractedContext.TraceId);
Assert.Equal(spanId, extractedContext.SpanId);
}

[Fact]
public void TestPropagationJaegerAndB3FromEnv()
{
SetProperty(Configuration.JaegerPropagation, "jaeger,b3");
SetProperty(Configuration.JaegerServiceName, "Test");
Expand Down

0 comments on commit 68dac31

Please sign in to comment.