Skip to content

Commit

Permalink
Format/Carrier now almost equal to Java implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
cwe1ss committed Aug 29, 2016
1 parent 8c25747 commit e9c8ae8
Show file tree
Hide file tree
Showing 19 changed files with 105 additions and 156 deletions.
4 changes: 2 additions & 2 deletions src/OpenTracing.AspNetCore/HeaderDictionaryCarrier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
namespace OpenTracing.AspNetCore
{
/// <summary>
/// A <see cref="ITextMapCarrier"/> which allows <see cref="IHeaderDictionary"/> implementations to be used as carrier objects.
/// A <see cref="ITextMap"/> which allows <see cref="IHeaderDictionary"/> implementations to be used as carrier objects.
/// </summary>
/// <remarks>
/// <see cref="IHeaderDictionary"/> is a multi-value dictionary. Since most other platforms represent http headers as regular
/// dictionaries, this carrier represents it as a regular dictionary to tracer implementations.</remarks>
public class HeaderDictionaryCarrier : ITextMapCarrier
public class HeaderDictionaryCarrier : ITextMap
{
private readonly IHeaderDictionary _headers;

Expand Down
13 changes: 0 additions & 13 deletions src/OpenTracing.BasicTracer/Propagation/IExtractCarrierHandler.cs

This file was deleted.

13 changes: 0 additions & 13 deletions src/OpenTracing.BasicTracer/Propagation/IInjectCarrierHandler.cs

This file was deleted.

10 changes: 4 additions & 6 deletions src/OpenTracing.BasicTracer/Propagation/TextMapCarrierHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@

namespace OpenTracing.BasicTracer.Propagation
{
public class TextMapCarrierHandler :
IInjectCarrierHandler<ITextMapCarrier>,
IExtractCarrierHandler<ITextMapCarrier>
public class TextMapCarrierHandler
{
public void MapContextToCarrier(SpanContext context, ITextMapCarrier carrier)
public void MapContextToCarrier(SpanContext context, ITextMap carrier)
{
carrier.Add(BaggageKeys.TraceId, context.TraceId.ToString());
carrier.Add(BaggageKeys.SpanId, context.SpanId.ToString());
Expand All @@ -19,7 +17,7 @@ public void MapContextToCarrier(SpanContext context, ITextMapCarrier carrier)
}
}

public SpanContext MapCarrierToContext(ITextMapCarrier carrier)
public SpanContext MapCarrierToContext(ITextMap carrier)
{
// we can't create a reference without a trace-id
Guid? traceId = TryGetGuid(carrier, BaggageKeys.TraceId);
Expand Down Expand Up @@ -48,7 +46,7 @@ public SpanContext MapCarrierToContext(ITextMapCarrier carrier)
return new SpanContext(traceId.Value, spanId.Value, sampled, baggage);
}

private Guid? TryGetGuid(ITextMapCarrier carrier, string key)
private Guid? TryGetGuid(ITextMap carrier, string key)
{
string strValue = carrier.Get(key);

Expand Down
49 changes: 13 additions & 36 deletions src/OpenTracing.BasicTracer/Tracer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,55 +46,32 @@ public ISpan StartSpan(string operationName, StartSpanOptions options)
return span;
}

public void Inject(ISpanContext spanContext, string format, IInjectCarrier carrier)
public void Inject<TCarrier>(ISpanContext spanContext, Format<TCarrier> format, TCarrier carrier)
{
switch (format)
{
case Formats.TextMap:
InjectTextMap(spanContext, carrier);
break;
// TODO add other formats
default:
throw new UnsupportedFormatException($"The format '{format}' is not supported.");
}
}
// TODO add other formats (and maybe don't use if/else :D )

private void InjectTextMap(ISpanContext spanContext, IInjectCarrier carrier)
{
var typedContext = (SpanContext)spanContext;

var textMapCarrier = carrier as ITextMapCarrier;
if (textMapCarrier != null)
if (format.Equals(Formats.TextMap))
{
_textMapCarrierHandler.MapContextToCarrier(typedContext, textMapCarrier);
return;
_textMapCarrierHandler.MapContextToCarrier(typedContext, (ITextMap) carrier);
}

throw new InvalidCarrierException($"The carrier '{carrier.GetType()}' is not supported for the format '{Formats.TextMap}'.");
}


public ISpanContext Extract(string format, IExtractCarrier carrier)
{
switch (format)
else
{
case Formats.TextMap:
return ExtractTextMap(carrier);
// TODO add other formats
default:
throw new UnsupportedFormatException($"The format '{format}' is not supported.");
throw new UnsupportedFormatException($"The format '{format}' is not supported.");
}
}

private ISpanContext ExtractTextMap(IExtractCarrier carrier)
public ISpanContext Extract<TCarrier>(Format<TCarrier> format, TCarrier carrier)
{
var textMapCarrier = carrier as ITextMapCarrier;
if (textMapCarrier != null)
// TODO add other formats (and maybe don't use if/else :D )

if (format.Equals(Formats.TextMap))
{
return _textMapCarrierHandler.MapCarrierToContext(textMapCarrier);
return _textMapCarrierHandler.MapCarrierToContext((ITextMap) carrier);
}

throw new InvalidCarrierException($"The carrier '{carrier.GetType()}' is not supported for the format '{Formats.TextMap}'.");
throw new UnsupportedFormatException($"The format '{format}' is not supported.");
}
}
}
4 changes: 2 additions & 2 deletions src/OpenTracing/Extensions/FormatBinaryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static void InjectBinary(this ITracer tracer, ISpanContext spanContext, b
throw new ArgumentNullException(nameof(tracer));
}

tracer.Inject(spanContext, Formats.Binary, new BinaryCarrier(data));
tracer.Inject(spanContext, Formats.Binary, data);
}

/// <summary>
Expand All @@ -51,7 +51,7 @@ public static ISpanContext ExtractBinary(this ITracer tracer, byte[] data)
throw new ArgumentNullException(nameof(tracer));
}

return tracer.Extract(Formats.Binary, new BinaryCarrier(data));
return tracer.Extract(Formats.Binary, data);
}
}
}
4 changes: 2 additions & 2 deletions src/OpenTracing/Extensions/FormatHttpHeadersExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public static void InjectHttpHeaders(this ITracer tracer, ISpanContext spanConte
throw new ArgumentNullException(nameof(tracer));
}

tracer.Inject(spanContext, Formats.HttpHeaders, new TextMapCarrier(headers));
tracer.Inject(spanContext, Formats.HttpHeaders, new DictionaryCarrier(headers));
}

/// <summary>
Expand Down Expand Up @@ -122,7 +122,7 @@ public static ISpanContext ExtractHttpHeaders(this ITracer tracer, IDictionary<s
throw new ArgumentNullException(nameof(tracer));
}

return tracer.Extract(Formats.HttpHeaders, new TextMapCarrier(headers));
return tracer.Extract(Formats.HttpHeaders, new DictionaryCarrier(headers));
}
}
}
10 changes: 5 additions & 5 deletions src/OpenTracing/Extensions/FormatTextMapExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,23 @@ public static void InjectTextMap(this ITracer tracer, ISpanContext spanContext,
throw new ArgumentNullException(nameof(tracer));
}

tracer.Inject(spanContext, Formats.TextMap, new TextMapCarrier(data));
tracer.Inject(spanContext, Formats.TextMap, new DictionaryCarrier(data));
}

/// <summary>
/// Returns a new <see cref="ISpanContext"/> containing the baggage of the given <paramref name="dictionary"/>
/// Returns a new <see cref="ISpanContext"/> containing the baggage of the given <paramref name="data"/>
/// (using the format <see cref="Formats.HttpHeaders"/>), or null if there was no baggage found.
/// </summary>
/// <param name="tracer">A <see cref="ITracer"/> instance.</param>
/// <param name="dictionary">The dictionary will be used as a carrier for the propagation.</param>
public static ISpanContext ExtractTextMap(this ITracer tracer, IDictionary<string, string> dictionary)
/// <param name="data">The dictionary will be used as a carrier for the propagation.</param>
public static ISpanContext ExtractTextMap(this ITracer tracer, IDictionary<string, string> data)
{
if (tracer == null)
{
throw new ArgumentNullException(nameof(tracer));
}

return tracer.Extract(Formats.TextMap, new TextMapCarrier(dictionary));
return tracer.Extract(Formats.TextMap, new DictionaryCarrier(data));
}
}
}
4 changes: 2 additions & 2 deletions src/OpenTracing/ITracer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ public interface ITracer
/// <param name="spanContext">The <see cref="ISpanContext"/> instance to inject into the carrier.</param>
/// <param name="format">The format which should be used.</param>
/// <param name="carrier">See the documentation for the chosen <paramref name="format"/> for a description of the carrier object.</param>
void Inject(ISpanContext spanContext, string format, IInjectCarrier carrier);
void Inject<TCarrier>(ISpanContext spanContext, Format<TCarrier> format, TCarrier carrier);

/// <summary>
/// Returns a new <see cref="ISpanContext"/> containing the baggage of the given <paramref name="carrier"/>,
/// or null if there was no baggage found.
/// </summary>
/// <param name="format">The format which should be used.</param>
/// <param name="carrier">See the documentation for the chosen <paramref name="format"/> for a description of the carrier object</param>
ISpanContext Extract(string format, IExtractCarrier carrier);
ISpanContext Extract<TCarrier>(Format<TCarrier> format, TCarrier carrier);
}
}
22 changes: 0 additions & 22 deletions src/OpenTracing/InvalidCarrierException.cs

This file was deleted.

4 changes: 2 additions & 2 deletions src/OpenTracing/NullTracer/NullTracer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ public ISpan StartSpan(string operationName, StartSpanOptions options = null)
return NullSpan.Instance;
}

public void Inject(ISpanContext spanContext, string format, IInjectCarrier carrier)
public void Inject<TCarrier>(ISpanContext spanContext, Format<TCarrier> format, TCarrier carrier)
{
}

public ISpanContext Extract(string format, IExtractCarrier carrier)
public ISpanContext Extract<TCarrier>(Format<TCarrier> format, TCarrier carrier)
{
return null;
}
Expand Down
24 changes: 0 additions & 24 deletions src/OpenTracing/Propagation/BinaryCarrier.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
namespace OpenTracing.Propagation
{
/// <summary>
/// The default <see cref="ITextMapCarrier"/> implementation which wraps an arbitrary <see cref="IDictionary{TKey,TValue}"/>.
/// The default <see cref="ITextMap"/> implementation which wraps an arbitrary <see cref="IDictionary{TKey,TValue}"/>.
/// </summary>
public class TextMapCarrier : ITextMapCarrier
public class DictionaryCarrier : ITextMap
{
private readonly IDictionary<string, string> _payload;

public TextMapCarrier(IDictionary<string, string> payload)
public DictionaryCarrier(IDictionary<string, string> payload)
{
if (payload == null)
{
Expand Down
59 changes: 59 additions & 0 deletions src/OpenTracing/Propagation/Format.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System;

namespace OpenTracing.Propagation
{
/// <summary>
/// Format instances control the behavior of <see cref="ITracer.Inject" /> and <see cref="ITracer.Extract" />
/// (and also constrain the type of the carrier parameter to same).
/// </summary>
public class Format<TCarrier> : IEquatable<Format<TCarrier>>
{
/// <summary>
/// The unique name for this format.
/// </summary>
public string Name { get; }

public Format(string name)
{
if (string.IsNullOrWhiteSpace(name))
{
throw new ArgumentNullException(nameof(name));
}

Name = name;
}

/// <summary>
/// Two format instances are equal when they have the same <see cref="Name" />.
/// </summary>
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != GetType()) return false;

return Equals(obj as Format<TCarrier>);
}

/// <summary>
/// Two format instances are equal when they have the same <see cref="Name" />.
/// </summary>
public bool Equals(Format<TCarrier> other)
{
if (other == null)
{
return false;
}

return string.Equals(Name, other.Name);
}

/// <summary>
/// Two format instances are equal when they have the same <see cref="Name" />.
/// </summary>
public override int GetHashCode()
{
return Name.GetHashCode();
}
}
}
Loading

0 comments on commit e9c8ae8

Please sign in to comment.