Exemple #1
0
        public async Task RootTraceSpanCreation()
        {
            //tracer faking
            Tracer tracer = A.Fake <Tracer>();

            A.CallTo(() => tracer.CreateTraceId())
            .Returns("trace_id");

            A.CallTo(() => tracer.CreateSpanId())
            .Returns("span_id");

            string   spanId       = null;
            string   parentSpanId = null;
            string   traceId      = null;
            DateTime startTime    = DateTime.MinValue;
            DateTime endTime      = DateTime.MinValue;
            string   displayName  = null;
            Dictionary <string, string> attributes = null;

            A.CallTo(() => tracer.SendAsync(A <TraceSpan> .Ignored))
            .Invokes((TraceSpan traceSpan) =>
            {
                spanId       = traceSpan.Id;
                parentSpanId = traceSpan.ParentId;
                traceId      = traceSpan.TraceId;
                startTime    = traceSpan.StartTime;
                endTime      = traceSpan.EndTime;
                displayName  = traceSpan.DisplayName;
                attributes   = traceSpan.Attributes;
            });

            //span creating
            TraceScope traceScope = new TraceScope("root", tracer);

            traceScope.Attributes.Add("key", "value");
            await Task.Delay(1);

            traceScope.Dispose();

            //checking
            await Task.Delay(100);

            Assert.Equal("span_id", spanId);
            Assert.Null(parentSpanId);
            Assert.Equal("trace_id", traceId);
            Assert.NotEqual(startTime, DateTime.MinValue);
            Assert.NotEqual(endTime, DateTime.MinValue);
            Assert.True(endTime > startTime);
            Assert.Equal("root", displayName);
            Assert.NotNull(attributes);
            Assert.Single(attributes);
        }