Esempio n. 1
0
        public void Basic_Trace_config()
        {
            using (var parentTrace = new StartClientTrace("parent"))
            {
                var dict = new Dictionary <string, object>();
                TraceContextPropagation.PropagateTraceIdOnto(dict);

                using (var trace = new StartServerTrace("name", dict))
                {
                    trace.Span.Should().NotBeNull();
                    trace.Span.Name.Should().Be("name");
                    trace.Span.TraceId.Should().Be(parentTrace.Span.TraceId);
                    trace.Span.ParentId.Should().Be(parentTrace.Span.Id);

                    trace.Span.Annotations[0].Value.Should().Be("sr");

                    _recorder.Recorded.Should().HaveCount(0);

                    TraceContextPropagation.CurrentSpan.Should().Be(trace.Span);
                }
            }
            _recorder.Recorded.Should().HaveCount(2);

            TraceContextPropagation.CurrentSpan.Should().BeNull();
        }
Esempio n. 2
0
        /// <summary>
        /// 服务追踪
        /// </summary>
        /// <param name="nodeClientContainer"></param>
        /// <param name="info"></param>
        /// <returns></returns>
        public async override Task <ServiceCallResult> CallAsync(INodeClientContainer nodeClientContainer, ServiceCallInfo info)
        {
            string name;

            if (!string.IsNullOrEmpty(info.ServiceName) && !string.IsNullOrEmpty(info.ActionName))
            {
                name = $"Proxy:{info.ServiceName}.{info.ActionName}({info.ServiceId.ToString()}.{info.ActionId.ToString()})";
            }
            else
            {
                name = $"Proxy:{info.ServiceId.ToString()}.{info.ActionId.ToString()}";
            }

            var crossProcessBag = new Dictionary <string, string>();

            if (ServiceContext.Current == null)
            {
                using (var trace = new StartClientTrace(name))
                {
                    TraceContextPropagation.PropagateTraceIdOnto(crossProcessBag);
                    return(await CallNext(nodeClientContainer, info, crossProcessBag, trace));
                }
            }
            else
            {
                using (var trace = new LocalTrace(name))
                {
                    TraceContextPropagation.PropagateTraceIdOnto(crossProcessBag);
                    return(await CallNext(nodeClientContainer, info, crossProcessBag, trace));
                }
            }
        }
Esempio n. 3
0
        public void IsWithinTrace_Should_be_true_When_there_is_a_context2()
        {
            // Arrange
            var span = new Span(123, "a", 1000);

            TraceContextPropagation.PushSpan(span);

            // Act + Assert
            TraceContextPropagation.IsWithinTrace.Should().BeTrue();
        }
Esempio n. 4
0
        public void PropagateTraceIdOnto_Should_not_do_anything_When_there_is_no_context()
        {
            // Arrange
            var dict = new Dictionary <string, object>();

            // Act
            TraceContextPropagation.PropagateTraceIdOnto(dict);

            // Assert
            dict.Should().HaveCount(0);
        }
Esempio n. 5
0
        public void TryObtainTraceIdFrom_Should_ignore_empty_dictionary()
        {
            // Arrange
            var dict = new Dictionary <string, string>();

            // Act
            TraceInfo?traceInfo;

            TraceContextPropagation.TryObtainTraceIdFrom(dict, out traceInfo).Should().BeFalse();

            // Assert
            dict.Should().HaveCount(0);
        }
Esempio n. 6
0
        public void CurrentSpan_Should_return_top_of_the_stack_When_there_is_a_context()
        {
            // Arrange
            var span1 = new Span(123, "a", 1000);
            var span2 = new Span(321, "a", 2000);

            TraceContextPropagation.PushSpan(span1);
            TraceContextPropagation.PushSpan(span2);

            // Act
            var currentSpan = TraceContextPropagation.CurrentSpan;

            // Assert
            currentSpan.Should().NotBeNull();
            currentSpan.Id.Should().Be(2000);
        }
Esempio n. 7
0
        public void PropagateTraceIdOnto_Should_add_entries_to_dict_When_there_is_a_context2()
        {
            // Arrange
            var dict = new Dictionary <string, string>();
            var span = new Span(123, "a", 1000);

            TraceContextPropagation.PushSpan(span);

            // Act
            TraceContextPropagation.PropagateTraceIdOnto(dict);

            // Assert
            dict.Should().HaveCount(2);
            dict["_zipkin_traceid"].Should().Be("123");
            dict["_zipkin_spanid"].Should().Be("1000");
        }
Esempio n. 8
0
        static void Main(string[] args)
        {
            new Zipkin.ZipkinBootstrapper("client-sample", IPAddress.Loopback, 1234)
            .ZipkinAt("localhost")
            .WithSampleRate(1.0)                     // means log everything
            .Start();

            const string rpc_exchange = "zipkin_test_exchange";

            var task = new Task <Task <bool> >(async(_) =>
            {
                var conn    = await RabbitMqNext.ConnectionFactory.Connect("localhost");
                var channel = await conn.CreateChannel();

                await channel.ExchangeDeclare(rpc_exchange, "direct", durable: false, autoDelete: false, arguments: null, waitConfirmation: true);

                var rpc = await channel.CreateRpcHelper(ConsumeMode.ParallelWithBufferCopy, 1000 * 60 * 5);

                var prop = channel.RentBasicProperties();

                using (new StartClientTrace("client-op"))                 // Starts a root trace + span
                {
                    // Trace id set to be passed to the server side
                    TraceContextPropagation.PropagateTraceIdOnto(prop.Headers);

                    Console.WriteLine("Sending RPC call");

                    var result = await rpc.Call(rpc_exchange, "rpc", prop, Encoding.UTF8.GetBytes("hello world!"));

                    Console.WriteLine("Reply received");
                }

                conn.Dispose();

                return(true);
            }, null);

            task.Start();
            task.Result.Wait();


            Thread.Sleep(1000);
            // Thread.CurrentThread.Join();

            Console.WriteLine("Goodbye from client!");
        }
Esempio n. 9
0
        public void TryObtainTraceIdFrom_Should_extract_ids_from_valid_dictionary()
        {
            // Arrange
            var dict = new Dictionary <string, string>();
            var span = new Span(123, "a", 1000);

            TraceContextPropagation.PushSpan(span);
            TraceContextPropagation.PropagateTraceIdOnto(dict);

            // Act
            TraceInfo?traceInfo;

            TraceContextPropagation.TryObtainTraceIdFrom(dict, out traceInfo).Should().BeTrue();

            // Assert
            dict.Should().HaveCount(2);
            traceInfo.Value.span.TraceId.Should().Be(123L);
            traceInfo.Value.span.ParentId.Should().Be(1000L);
        }
Esempio n. 10
0
        static void Main(string[] args)
        {
            new Zipkin.ZipkinBootstrapper("api-sample", IPAddress.Loopback, 1234)
            .ZipkinAt("localhost")
            .WithSampleRate(1.0)                     // means log everything
            .Start();


            using (var roottrace = new StartClientTrace("client-op"))             // Starts a root trace + span
            {
                var crossProcessBag = new Dictionary <string, object>();
                TraceContextPropagation.PropagateTraceIdOnto(crossProcessBag);

                Thread.Sleep(20);

                roottrace.TimeAnnotateWith("custom");

                using (new StartServerTrace("server-op", crossProcessBag).SetLocalComponentName("fake-server"))
                {
                    using (new LocalTrace("op1").AnnotateWith(PredefinedTag.SqlQuery, "select * from  ..."))
                    {
                        Thread.Sleep(70);
                    }

                    using (var trace = new LocalTrace("op2"))
                    {
                        Thread.Sleep(90);

                        trace.AnnotateWith(PredefinedTag.Error, "error message");                         // mark it with an error
                    }

                    using (new LocalTrace("op3").TimeAnnotateWith(PredefinedTag.ServerSend))
                    {
                        Thread.Sleep(90);
                    }
                }
            }

            Thread.Sleep(1000);
        }
Esempio n. 11
0
 public void Init()
 {
     TraceContextPropagation.Reset();
 }