public void Trace_Simple_Buffer()
        {
            string rootSpanName = EntryData.GetMessage(nameof(Trace_Simple_Buffer), _testId);
            // Create annotations with very large labels to ensure the buffer is flushed.
            string label  = string.Join("", Enumerable.Repeat("1234567890", 1000));
            var    labels = new Dictionary <string, string>
            {
                { "key-one", label },
                { "key-two", label },
                { "key-three", label },
                { "key-four", label },
                { "key-five", label },
            };

            using (var consumer = SizedBufferingConsumer <TraceProto> .Create(_grpcConsumer, MessageSizer <TraceProto> .GetSize, BufferOptions.DefaultBufferSize / 2))
            {
                var tracer = CreateSimpleManagedTracer(consumer);
                using (tracer.StartSpan(rootSpanName))
                {
                    BlockUntilClockTick();
                    tracer.AnnotateSpan(labels);
                }
            }

            TraceProto trace = TraceEntryPolling.Default.GetTrace(rootSpanName, _startTime);

            TraceEntryVerifiers.AssertSingleSpan(trace, rootSpanName);
        }
        public void Trace_MultipleSpans()
        {
            string rootSpanName = EntryData.GetMessage(nameof(Trace_MultipleSpans), _testId);
            var    labels       = new Dictionary <string, string>
            {
                { "annotation-key", "annotation-value" }
            };

            var tracer = CreateSimpleManagedTracer(_grpcConsumer);

            using (tracer.StartSpan(rootSpanName))
            {
                BlockUntilClockTick();
                using (tracer.StartSpan("child-one"))
                {
                    tracer.SetStackTrace(TraceEntryData.CreateStackTrace());
                    BlockUntilClockTick();
                }
                using (tracer.StartSpan("child-two"))
                {
                    BlockUntilClockTick();
                    using (tracer.StartSpan("grandchild-one", StartSpanOptions.Create(SpanKind.RpcClient)))
                    {
                        BlockUntilClockTick();
                    }
                    using (tracer.StartSpan("grandchild-two"))
                    {
                        BlockUntilClockTick();
                        tracer.AnnotateSpan(labels);
                    }
                }
            }

            TraceProto trace = TraceEntryPolling.Default.GetTrace(rootSpanName, _startTime);

            Assert.NotNull(trace);
            Assert.Equal(5, trace.Spans.Count);

            TraceSpan root          = trace.Spans.First(s => s.Name.Equals(rootSpanName));
            TraceSpan childOne      = trace.Spans.First(s => s.Name.Equals("child-one"));
            TraceSpan childTwo      = trace.Spans.First(s => s.Name.Equals("child-two"));
            TraceSpan grandchildOne = trace.Spans.First(s => s.Name.Equals("grandchild-one"));
            TraceSpan grandchildTwo = trace.Spans.First(s => s.Name.Equals("grandchild-two"));

            Assert.Equal(root.SpanId, childOne.ParentSpanId);
            TraceEntryVerifiers.AssertContainsStackTrace(childOne,
                                                         nameof(TraceEntryData.CreateStackTrace), nameof(SimpleManagedTracerTest));

            Assert.Equal(root.SpanId, childTwo.ParentSpanId);

            Assert.Equal(childTwo.SpanId, grandchildOne.ParentSpanId);
            Assert.Equal(TraceSpan.Types.SpanKind.RpcClient, grandchildOne.Kind);

            Assert.Equal(childTwo.SpanId, grandchildTwo.ParentSpanId);
            Assert.Equal(TraceSpan.Types.SpanKind.Unspecified, grandchildTwo.Kind);
            TraceEntryVerifiers.AssertSpanLabelsExact(grandchildTwo, labels);
        }
        public void Trace_Simple()
        {
            string rootSpanName = EntryData.GetMessage(nameof(Trace_Simple), _testId);

            using (CreateSimpleManagedTracer(_grpcConsumer).StartSpan(rootSpanName))
            {
                BlockUntilClockTick();
            }

            TraceProto trace = TraceEntryPolling.Default.GetTrace(rootSpanName, _startTime);

            TraceEntryVerifiers.AssertSingleSpan(trace, rootSpanName);
        }
        public async Task TraceOutGoing()
        {
            string googleUri = "https://google.com/";
            var    spanName  = EntryData.GetMessage(nameof(TraceOutGoing), _testId);

            await TraceOutGoingRequest(spanName, googleUri, false);

            var trace = TraceEntryPolling.Default.GetTrace(spanName, _startTime);

            TraceEntryVerifiers.AssertParentChildSpan(trace, spanName, googleUri);
            TraceEntryVerifiers.AssertSpanLabelsExact(
                trace.Spans.First(s => s.Name == googleUri), TraceEntryData.HttpGetSuccessLabels);
        }
        public async Task TraceOutGoing_HttpError()
        {
            string fakeUri  = "https://google.com/404";
            var    spanName = EntryData.GetMessage(nameof(TraceOutGoing_HttpError), _testId);

            await TraceOutGoingRequest(spanName, fakeUri, false);

            var trace = TraceEntryPolling.Default.GetTrace(spanName, _startTime);

            TraceEntryVerifiers.AssertParentChildSpan(trace, spanName, fakeUri);
            TraceEntryVerifiers.AssertSpanLabelsExact(trace.Spans.Where(s => s.Name == fakeUri).Single(),
                                                      new Dictionary <string, string>
            {
                { TraceLabels.HttpMethod, "GET" },
                { TraceLabels.HttpStatusCode, "404" }
            });
        }
        public void Trace_SimpleStacktrace()
        {
            string rootSpanName = EntryData.GetMessage(nameof(Trace_SimpleStacktrace), _testId);

            var tracer = CreateSimpleManagedTracer(_grpcConsumer);

            using (tracer.StartSpan(rootSpanName))
            {
                BlockUntilClockTick();
                tracer.SetStackTrace(TraceEntryData.CreateStackTrace());
            }

            TraceProto trace = TraceEntryPolling.Default.GetTrace(rootSpanName, _startTime);

            TraceEntryVerifiers.AssertSingleSpan(trace, rootSpanName);
            TraceEntryVerifiers.AssertContainsStackTrace(trace.Spans[0],
                                                         nameof(TraceEntryData.CreateStackTrace), nameof(SimpleManagedTracerTest));
        }
        public void Trace_Simple_BufferNoTrace()
        {
            string rootSpanName = EntryData.GetMessage(nameof(Trace_Simple_BufferNoTrace), _testId);

            using (var consumer = SizedBufferingConsumer <TraceProto> .Create(_grpcConsumer, MessageSizer <TraceProto> .GetSize, BufferOptions.DefaultBufferSize))
            {
                using (CreateSimpleManagedTracer(consumer).StartSpan(rootSpanName))
                {
                    BlockUntilClockTick();
                }

                // Verifying before disposing of the consumer so as to check that the buffer
                // wasn't flush because of the size of the messages. If we verify after disposing
                // any message, big or small, would have been flushed on disposing.
                TraceProto trace = TraceEntryPolling.NoEntry.GetTrace(rootSpanName, _startTime, false);

                Assert.Null(trace);
            }
        }
        public async Task TraceOutGoing_Exception()
        {
            string fakeUri  = "http://www.thiscannotpossiblyexist934719238.com/";
            var    spanName = EntryData.GetMessage(nameof(TraceOutGoing_Exception), _testId);

            await TraceOutGoingRequest(spanName, fakeUri, true);

            var trace = TraceEntryPolling.Default.GetTrace(spanName, _startTime);

            TraceEntryVerifiers.AssertParentChildSpan(trace, spanName, fakeUri);
            var span = trace.Spans.Where(s => s.Name == fakeUri).Single();

            TraceEntryVerifiers.AssertSpanLabelsContains(span,
                                                         new Dictionary <string, string>
            {
                { TraceLabels.HttpMethod, "GET" },
            });
            TraceEntryVerifiers.AssertContainsStackTrace(span);
        }
        public void Trace_IncompleteSpans()
        {
            string rootSpanName = EntryData.GetMessage(nameof(Trace_IncompleteSpans), _testId);

            var tracer = CreateSimpleManagedTracer(_grpcConsumer);

            tracer.StartSpan(rootSpanName);
            using (tracer.StartSpan("span-name-1"))
            {
                BlockUntilClockTick();
                using (tracer.StartSpan("span-name-2"))
                {
                    BlockUntilClockTick();
                }
            }

            TraceProto trace = TraceEntryPolling.NoEntry.GetTrace(rootSpanName, _startTime, false);

            Assert.Null(trace);
        }
        public void Trace_SimpleAnnotation()
        {
            string rootSpanName = EntryData.GetMessage(nameof(Trace_SimpleAnnotation), _testId);
            var    labels       = new Dictionary <string, string>
            {
                { "annotation-key", "annotation-value" },
                { "some-key", "some-value" }
            };

            var tracer = CreateSimpleManagedTracer(_grpcConsumer);

            using (tracer.StartSpan(rootSpanName))
            {
                BlockUntilClockTick();
                tracer.AnnotateSpan(labels);
            }

            TraceProto trace = TraceEntryPolling.Default.GetTrace(rootSpanName, _startTime);

            TraceEntryVerifiers.AssertSingleSpan(trace, rootSpanName);
            TraceEntryVerifiers.AssertSpanLabelsExact(trace.Spans.First(), labels);
        }