private static void Main(string[] args)
        {
            var url    = "http://localhost:9411";
            var tracer = new ZipkinTracer(new ZipkinTracerOptions(new Endpoint("AaronsAppKafka"),
                                                                  ZipkinKafkaSpanReporter.Create(new ZipkinKafkaReportingOptions(new[] { "localhost:19092" },
                                                                                                                                 debugLogging: true))));

            Console.WriteLine("Connected to Zipkin at {0}", url);
            Console.WriteLine("Type some gibberish and press enter to create a trace!");
            Console.WriteLine("Type '/exit to quit.");
            var         line    = Console.ReadLine();
            IZipkinSpan current = null;

            while (string.IsNullOrEmpty(line) || !line.Equals("/exit"))
            {
                IZipkinSpanBuilder sb = null;
                if (string.IsNullOrEmpty(line))
                {
                    sb = tracer.BuildSpan("no-op").WithTag("empty", true);
                    if (current != null)
                    {
                        current.Finish();
                    }
                }
                else
                {
                    sb = tracer.BuildSpan("actual-op").WithTag("empty", false);
                    if (current != null)
                    {
                        current.Finish();
                        sb = sb.AsChildOf(current);
                    }
                }

                current = sb.Start();

                if (!string.IsNullOrEmpty(line))
                {
                    current.Log(line);
                }

                line = Console.ReadLine();
            }

            current?.Finish();

            tracer.Dispose();
        }
        public void Should_have_active_span_when_scope_is_used()
        {
            var url = "http://localhost:9411";

            using (var tracer = new ZipkinTracer(new ZipkinTracerOptions(url, "ZipkinTest", debug: true)))
            {
                using (var scope = tracer.BuildSpan("ShouldSendSpans").StartActive(finishSpanOnDispose: true))
                {
                    tracer.ActiveSpan.Should().NotBeNull();
                }

                tracer.ActiveSpan.Should().BeNull();
            }
        }
Example #3
0
        public async Task HttpReporterShouldPostCorrectlyToZipkin()
        {
            var testTracer = new ZipkinTracer(new ZipkinTracerOptions(
                                                  new Endpoint(_appName), new NoOpReporter())
            {
                ScopeManager = new AsyncLocalScopeManager()
            });

            string traceId;
            Span   parentSpan = null;
            Span   childSpan  = null;

            using (var parentScope = testTracer.BuildSpan("parent").StartActive())
            {
                parentSpan = (Span)parentScope.Span;
                using (var childScope = testTracer.BuildSpan("child").StartActive())
                {
                    childSpan = (Span)childScope.Span;
                }

                traceId = parentSpan.TypedContext.TraceId;
            }

            // sanity check
            parentSpan.TypedContext.ParentId.Should().BeNullOrEmpty();
            childSpan.TypedContext.ParentId.Should().Be(parentSpan.Context.SpanId);

            // create an HTTP reporting engine
            var httpReporter = new ZipkinHttpApiTransmitter(_zipkinClient,
                                                            ZipkinHttpApiTransmitter.GetFullZipkinUri(_httpBaseUri.AbsoluteUri));

            // manually transmit data to Zipkin for parent span
            var resp1 = await httpReporter.TransmitSpans(new[] { parentSpan }, TimeSpan.FromSeconds(3));

            resp1.IsSuccessStatusCode.Should().BeTrue(
                $"Expected success status code, but instead found [{resp1.StatusCode}][{resp1.ReasonPhrase}]");

            // manually transmit data to Zipkin for child span
            var resp2 = await httpReporter.TransmitSpans(new[] { childSpan }, TimeSpan.FromSeconds(3));

            resp2.IsSuccessStatusCode.Should().BeTrue(
                $"Expected success status code, but instead found [{resp2.StatusCode}][{resp2.ReasonPhrase}]");

            var fullUri = new Uri(_httpBaseUri, $"api/v2/trace/{traceId}");
            HttpResponseMessage traceResp = null;
            var retries = 3;

            for (var i = 1; i <= retries; i++)
            {
                try
                {
                    await Task.Delay(1000); // give it some time to get uploaded

                    traceResp =
                        await _zipkinClient.GetAsync(fullUri);

                    traceResp.IsSuccessStatusCode.Should()
                    .BeTrue(
                        $"Expected success status code, but instead found [{traceResp.StatusCode}][{traceResp.ReasonPhrase}]");
                }
                catch (Exception ex)
                {
                    if (i == retries)
                    {
                        throw;
                    }
                }
            }

            var json = await traceResp.Content.ReadAsStringAsync();

            var traces = ZipkinDeserializer.Deserialize(json);

            traces.Count.Should().Be(2);
        }