Skip to content

Commit

Permalink
Fixing binary propagation (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
austinlparker authored Aug 7, 2019
1 parent 15cfeeb commit a3d9912
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 15 deletions.
5 changes: 0 additions & 5 deletions global.json

This file was deleted.

14 changes: 6 additions & 8 deletions src/OpenTracing/Mock/Propagators.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,20 +75,18 @@ public MockSpanContext Extract<TCarrier>(IFormat<TCarrier> format, TCarrier carr

public MemoryStream Serialize(BinaryContext ctx)
{
using (var ms = new MemoryStream())
var ms = new MemoryStream();
using (var writer = new BinaryWriter(ms, System.Text.Encoding.UTF8, true))
{
using (var writer = new BinaryWriter(ms))
{
writer.Write(ctx.SpanId);
writer.Write(ctx.TraceId);
}

return ms;
writer.Write(ctx.SpanId);
writer.Write(ctx.TraceId);
}
return ms;
}

public BinaryContext Deserialize(MemoryStream stream)
{
stream.Position = 0;
var res = new BinaryContext();
using (var reader = new BinaryReader(stream))
{
Expand Down
2 changes: 1 addition & 1 deletion src/OpenTracing/Propagation/BinaryExtractAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace OpenTracing.Propagation
/// <seealso cref="ITracer.Extract{TCarrier}"/>
public class BinaryExtractAdapter : IBinary
{
private readonly MemoryStream _stream;
private MemoryStream _stream;

public BinaryExtractAdapter(MemoryStream stream)
{
Expand Down
3 changes: 2 additions & 1 deletion src/OpenTracing/Propagation/BinaryInjectAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public BinaryInjectAdapter(MemoryStream stream)
/// <inheritdoc />
public void Set(MemoryStream stream)
{
_stream = stream;
stream.Position = 0;
stream.CopyTo(_stream);
}

/// <inheritdoc />
Expand Down
22 changes: 22 additions & 0 deletions test/OpenTracing.Tests/Mock/MockTracerTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using OpenTracing.Mock;
Expand Down Expand Up @@ -132,6 +133,27 @@ public void TestStartExplicitTimestamp()
Assert.Equal(startTimestamp, finishedSpans[0].StartTimestamp);
}

[Fact]
public void TestBinaryPropagator()
{
MockTracer tracer = new MockTracer(propagator: Propagators.Binary);
var parentSpan = tracer.BuildSpan("foo").Start();
parentSpan.Finish();
var ms = new MemoryStream();
tracer.Inject(parentSpan.Context, BuiltinFormats.Binary, new BinaryInjectAdapter(ms));

var sc = tracer.Extract(BuiltinFormats.Binary, new BinaryExtractAdapter(ms));

var childSpan = tracer.BuildSpan("bar").AsChildOf(sc).Start();
childSpan.Finish();

var finishedSpans = tracer.FinishedSpans();

Assert.Equal(2, finishedSpans.Count);
Assert.Equal(finishedSpans[0].Context.TraceId, finishedSpans[1].Context.TraceId);
Assert.Equal(finishedSpans[0].Context.SpanId, finishedSpans[1].ParentId);
}

[Fact]
public void TestTextMapPropagatorTextMap()
{
Expand Down

0 comments on commit a3d9912

Please sign in to comment.