public void TracerShouldRecordAllSpanData()
        {
            var recorder = new SimpleMockRecorder();
            var tracer   = GetTracer(recorder);

            var startTimeStamp = new DateTimeOffset(2018, 2, 19, 12, 0, 0, TimeSpan.Zero);
            var endTimeStamp   = new DateTimeOffset(2018, 2, 19, 12, 0, 5, TimeSpan.Zero);

            var span = tracer.BuildSpan("test")
                       .WithStartTimestamp(startTimeStamp)
                       .Start()
                       .SetTag("key", "testValue")
                       .SetBaggageItem("baggageKey", "bagTestValue");

            span.Finish(endTimeStamp);

            var recordedSpans = recorder.GetSpans().First();

            Assert.Equal("test", recordedSpans.OperationName);
            Assert.Equal(startTimeStamp, recordedSpans.StartTimestamp);
            Assert.Equal(TimeSpan.FromSeconds(5), recordedSpans.Duration);

            Assert.Equal("bagTestValue", recordedSpans.Context.GetBaggageItem("baggageKey"));
            Assert.Equal("testValue", recordedSpans.Tags["key"]);

            Assert.False(string.IsNullOrWhiteSpace(recordedSpans.Context.TraceId));
            Assert.False(string.IsNullOrWhiteSpace(recordedSpans.Context.SpanId));
        }
        public void SpansShouldAllowThreadSafeAccess()
        {
            var recorder = new SimpleMockRecorder();
            var tracer   = GetTracer(recorder);
            var span     = tracer.BuildSpan("testOperation").Start();

            var t1 = Task.Run(() =>
            {
                span.Log("t1 run");
                span.SetTag("sameTag", "t1");
                span.SetTag("t1Tag", "t1");
            });
            var t2 = Task.Run(() =>
            {
                span.Log("t2 run");
                span.SetTag("sameTag", "t2");
                span.SetTag("t2Tag", "t2");
            });

            t1.Wait();
            t2.Wait();

            span.Finish();

            var finishedSpan = recorder.GetSpans().First();

            // we expect there to be 2 logs and 3 tags
            Assert.True(finishedSpan.LogData.Count == 2);
            Assert.True(finishedSpan.Tags.Count == 3);
        }
        public void SpansShouldRecordLogs()
        {
            var recorder = new SimpleMockRecorder();
            var tracer   = GetTracer(recorder);
            var span     = tracer.BuildSpan("testOperation").Start();

            span.Log("hello world!");
            span.Finish();

            var finishedSpan = recorder.GetSpans().First();
            // project the sequence of logdata into an array with one item, the aforementioned log message
            var finishedSpanLogData = finishedSpan.LogData.Select(
                item => item.Fields.First(
                    f => f.Value.Equals("hello world!")
                    ).Value).ToArray()[0];

            Assert.Equal("hello world!", (string)finishedSpanLogData);
        }
        public void SpansShouldBuildProperTypedKeyTags()
        {
            var recorder = new SimpleMockRecorder();
            var tracer   = GetTracer(recorder);
            var span     = tracer.BuildSpan("testOperation")
                           .WithTag(new BooleanTag("testBoolTag"), true)
                           .WithTag(new IntTag("testIntTag"), 1)
                           .WithTag(new StringTag("testStringTag"), "test")
                           .WithTag(new IntOrStringTag("testIntOrStringTagAsString"), "string")
                           .WithTag(new IntOrStringTag("testIntOrStringTagAsInt"), 1)
                           .Start();

            span.Finish();
            var finishedSpan = recorder.GetSpans().First();

            Assert.True((bool)finishedSpan.Tags["testBoolTag"]);
            Assert.Equal(1, finishedSpan.Tags["testIntTag"]);
            Assert.Equal("test", finishedSpan.Tags["testStringTag"]);
            Assert.Equal("string", finishedSpan.Tags["testIntOrStringTagAsString"]);
            Assert.Equal(1, finishedSpan.Tags["testIntOrStringTagAsInt"]);
        }
        public void SpansShouldBuildProperStringKeyTags()
        {
            var recorder = new SimpleMockRecorder();
            var tracer   = GetTracer(recorder);
            var span     = tracer.BuildSpan("testOperation")
                           .WithTag("boolTrueTag", true)
                           .WithTag("boolFalseTag", false)
                           .WithTag("intTag", 0)
                           .WithTag("stringTag", "test")
                           .WithTag("doubleTag", 0.1)
                           .Start();

            span.Finish();
            var finishedSpan = recorder.GetSpans().First();

            Assert.True((bool)finishedSpan.Tags["boolTrueTag"]);
            Assert.False((bool)finishedSpan.Tags["boolFalseTag"]);
            Assert.Equal(0, finishedSpan.Tags["intTag"]);
            Assert.Equal("test", finishedSpan.Tags["stringTag"]);
            Assert.Equal(0.1, finishedSpan.Tags["doubleTag"]);
        }