From 15cfeeb5c58b301ada89f4e8f5a3915dc97888cf Mon Sep 17 00:00:00 2001 From: Austin Parker Date: Wed, 7 Aug 2019 11:30:37 -0400 Subject: [PATCH] refactor to include interface and mock example (#129) --- src/OpenTracing/Mock/Propagators.cs | 79 +++++++++++++++++++ .../Propagation/BinaryExtractAdapter.cs | 34 ++++++++ .../Propagation/BinaryInjectAdapter.cs | 32 ++++++++ src/OpenTracing/Propagation/BuiltinFormats.cs | 3 +- src/OpenTracing/Propagation/IBinary.cs | 26 ++++++ 5 files changed, 172 insertions(+), 2 deletions(-) create mode 100644 src/OpenTracing/Propagation/BinaryExtractAdapter.cs create mode 100644 src/OpenTracing/Propagation/BinaryInjectAdapter.cs create mode 100644 src/OpenTracing/Propagation/IBinary.cs diff --git a/src/OpenTracing/Mock/Propagators.cs b/src/OpenTracing/Mock/Propagators.cs index c88edb1..022f1d4 100644 --- a/src/OpenTracing/Mock/Propagators.cs +++ b/src/OpenTracing/Mock/Propagators.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.IO; using OpenTracing.Propagation; namespace OpenTracing.Mock @@ -9,6 +10,8 @@ public static class Propagators public static readonly IPropagator Console = new ConsolePropagator(); public static readonly IPropagator TextMap = new TextMapPropagator(); + + public static readonly IPropagator Binary = new BinaryPropagator(); } /// @@ -21,6 +24,82 @@ public interface IPropagator MockSpanContext Extract(IFormat format, TCarrier carrier); } + public sealed class BinaryPropagator : IPropagator + { + public class BinaryContext + { + public string TraceId { get; set; } + public string SpanId { get; set; } + } + + public void Inject(MockSpanContext context, IFormat format, TCarrier carrier) + { + if (carrier is IBinary stream) + { + var contextObject = new BinaryContext + { + SpanId = context.SpanId, TraceId = context.TraceId + }; + var serialContext = Serialize(contextObject); + stream.Set(serialContext); + } + else + { + throw new InvalidOperationException($"Unknown carrier [{carrier.GetType()}]"); + } + } + + public MockSpanContext Extract(IFormat format, TCarrier carrier) + { + string traceId = ""; + string spanId = ""; + + if (carrier is IBinary stream) + { + var ctx = Deserialize(stream.Get()); + traceId = ctx.TraceId; + spanId = ctx.SpanId; + } + else + { + throw new InvalidOperationException($"Unknown carrier [{carrier.GetType()}]"); + } + + if (!string.IsNullOrEmpty(traceId) && !string.IsNullOrEmpty(spanId)) + { + return new MockSpanContext(traceId, spanId, null); + } + + return null; + } + + public MemoryStream Serialize(BinaryContext ctx) + { + using (var ms = new MemoryStream()) + { + using (var writer = new BinaryWriter(ms)) + { + writer.Write(ctx.SpanId); + writer.Write(ctx.TraceId); + } + + return ms; + } + } + + public BinaryContext Deserialize(MemoryStream stream) + { + var res = new BinaryContext(); + using (var reader = new BinaryReader(stream)) + { + res.SpanId = reader.ReadString(); + res.TraceId = reader.ReadString(); + } + + return res; + } + } + public sealed class ConsolePropagator : IPropagator { public void Inject(MockSpanContext context, IFormat format, TCarrier carrier) diff --git a/src/OpenTracing/Propagation/BinaryExtractAdapter.cs b/src/OpenTracing/Propagation/BinaryExtractAdapter.cs new file mode 100644 index 0000000..4c04846 --- /dev/null +++ b/src/OpenTracing/Propagation/BinaryExtractAdapter.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.IO; +using System.Linq; + +namespace OpenTracing.Propagation +{ + /// + /// A carrier for use with only. It cannot be mutated, only read. + /// + /// + public class BinaryExtractAdapter : IBinary + { + private readonly MemoryStream _stream; + + public BinaryExtractAdapter(MemoryStream stream) + { + _stream = stream; + } + + /// + public void Set(MemoryStream stream) + { + throw new NotSupportedException($"{nameof(BinaryExtractAdapter)} should only be used with {nameof(ITracer)}.{nameof(ITracer.Extract)}"); + } + + /// + public MemoryStream Get() + { + return _stream; + } + } +} diff --git a/src/OpenTracing/Propagation/BinaryInjectAdapter.cs b/src/OpenTracing/Propagation/BinaryInjectAdapter.cs new file mode 100644 index 0000000..78c54ef --- /dev/null +++ b/src/OpenTracing/Propagation/BinaryInjectAdapter.cs @@ -0,0 +1,32 @@ +using System; +using System.IO; + +namespace OpenTracing.Propagation +{ + /// + /// A carrier for use with only. It cannot be read, only set. + /// + /// + public class BinaryInjectAdapter : IBinary + { + private MemoryStream _stream; + + public BinaryInjectAdapter(MemoryStream stream) + { + _stream = stream; + } + + /// + public void Set(MemoryStream stream) + { + _stream = stream; + } + + /// + public MemoryStream Get() + { + throw new NotSupportedException($"{nameof(BinaryInjectAdapter)} should only be used with {nameof(ITracer)}.{nameof(ITracer.Inject)}"); + } + + } +} diff --git a/src/OpenTracing/Propagation/BuiltinFormats.cs b/src/OpenTracing/Propagation/BuiltinFormats.cs index 7e254e7..a30c6de 100644 --- a/src/OpenTracing/Propagation/BuiltinFormats.cs +++ b/src/OpenTracing/Propagation/BuiltinFormats.cs @@ -30,13 +30,12 @@ public static class BuiltinFormats /// /// The 'Binary' format allows for unconstrained byte encoding of state /// for and using a . - /// Note that this should be considered experimental, and subject to change. /// /// /// /// /// - public static readonly IFormat Binary = new Builtin("BINARY"); + public static readonly IFormat Binary = new Builtin("BINARY"); private struct Builtin : IFormat { diff --git a/src/OpenTracing/Propagation/IBinary.cs b/src/OpenTracing/Propagation/IBinary.cs new file mode 100644 index 0000000..48771f6 --- /dev/null +++ b/src/OpenTracing/Propagation/IBinary.cs @@ -0,0 +1,26 @@ +using System.Collections.Generic; +using System.IO; + +namespace OpenTracing.Propagation +{ + /// + /// is a built-in carrier for and + /// . IBinary implementations allow for the reading and writing of arbitrary streams. + /// + /// + /// + public interface IBinary + { + /// + /// Sets the backing MemoryStream of an store. + /// + /// A memory-backed stream. + void Set(MemoryStream stream); + + /// + /// Gets the backing MemoryStream of an store. + /// + /// + MemoryStream Get(); + } +}