示例#1
0
 public static void RootCase()
 {
     using (var rootContext = Trace.CreateRootContext("Root"))
     {
         rootContext.RecordTimepoint(Timepoint.Start);
         rootContext.RecordTimepoint(Timepoint.Finish);
     }
 }
示例#2
0
        public void CreateChildContext_should_set_context_id()
        {
            var contextId = TraceIdGenerator.CreateTraceContextId();

            using (var rootContext = Trace.CreateRootContext("Test"))
                using (var childContext = Trace.CreateChildContext("Child", contextId))
                    childContext.ContextId.Should().BeSameAs(contextId);
        }
示例#3
0
 public void CreateRootContext_should_throw_exception_if_context_name_is_null_or_empty(string contextName)
 {
     Assert.Throws <InvalidOperationException>(() =>
     {
         using (var traceContext = Trace.CreateRootContext(contextName))
         {
         }
     });
 }
示例#4
0
 public void CreateRootContext_should_throw_exception_if_context_is_set_already()
 {
     using (var rootContext = Trace.CreateRootContext("Test"))
     {
         Assert.DoesNotThrow(() =>
         {
             using (var newRootContext = Trace.CreateRootContext("NewRoot"))
             {
             }
         });
     }
 }
示例#5
0
        public void FinishContext_should_pop_context()
        {
            using (var rootContext = Trace.CreateRootContext("Test"))
            {
                var originalContext = TraceContext.Current;

                var childContext = Trace.CreateChildContext("Child");
                Trace.FinishCurrentContext();

                TraceContext.Current.Should().BeSameAs(originalContext);
            }
        }
示例#6
0
        public void CreateChildContext_should_update_context_and_pop_after_dispose()
        {
            using (var rootContext = Trace.CreateRootContext("Test"))
            {
                var originalContext = TraceContext.Current;

                using (var childContext = Trace.CreateChildContext("Child"))
                    childContext.TraceId.Should().BeSameAs(rootContext.TraceId);

                TraceContext.Current.Should().BeSameAs(originalContext);
            }
        }
示例#7
0
 public static void RootChildCase()
 {
     using (var rootContext = Trace.CreateRootContext("Root"))
     {
         rootContext.RecordTimepoint(Timepoint.Start);
         using (var childContext = Trace.CreateChildContext("Child"))
         {
             childContext.RecordTimepoint(Timepoint.Start);
             childContext.RecordTimepoint(Timepoint.Finish);
         }
         rootContext.RecordTimepoint(Timepoint.Finish);
     }
 }
示例#8
0
        public void ContinueContext_should_throw_exception_if_context_is_set_already(bool isActive)
        {
            var traceId   = TraceIdGenerator.CreateTraceId();
            var contextId = TraceIdGenerator.CreateTraceContextId();

            using (var traceContext = Trace.CreateRootContext("Test"))
            {
                Assert.DoesNotThrow(() =>
                {
                    using (var continueContext = Trace.ContinueContext(traceId, contextId, isActive, isRoot: false))
                    {
                    }
                });
            }
        }
示例#9
0
        public static void RootChildContinueCase()
        {
            string traceId       = null;
            string rootContextId = null;

            using (var rootContext = Trace.CreateRootContext("Root"))
            {
                rootContext.RecordTimepoint(Timepoint.Start);
                traceId       = rootContext.TraceId;
                rootContextId = rootContext.ContextId;

                using (var childContext = Trace.CreateChildContext("Child"))
                {
                    childContext.RecordTimepoint(Timepoint.Start);
                    childContext.RecordTimepoint(Timepoint.Finish);
                }
            }

            using (var rootContext = Trace.ContinueContext(traceId, rootContextId, isActive: true, isRoot: false))
            {
                rootContext.RecordTimepoint(Timepoint.Finish);
            }
        }
示例#10
0
        public void SendMessage(string message)
        {
            try
            {
                using (var clientContext = Trace.CreateRootContext("Client-Server"))
                {
                    Console.WriteLine(clientContext.TraceId);
                    clientContext.RecordAnnotation(Annotation.RequestUrl, url);
                    clientContext.RecordTimepoint(Timepoint.ClientSend);

                    var bytes = Encoding.ASCII.GetBytes(message);

                    var request = (HttpWebRequest)WebRequest.Create(url);
                    request.Method        = "POST";
                    request.ContentType   = "application/x-www-form-urlencoded";
                    request.ContentLength = bytes.Length;
                    request.Timeout       = (int)TimeSpan.FromSeconds(2).TotalMilliseconds;
                    request.SetTracingHeaders(clientContext);
                    var stream = request.GetRequestStream();
                    stream.Write(bytes, 0, bytes.Length);
                    stream.Close();
                    var response = request.GetResponse();

                    clientContext.RecordTimepoint(Timepoint.ClientReceive);
                    synchronizer.ServerEndProcessQuerySignal.Wait();
                }
            }
            catch (Exception e)
            {
                Console.Out.WriteLine("Unhandled client exception: {0}", e);
            }
            finally
            {
                synchronizer.ClientEndQuerySignal.Set();
            }
        }
示例#11
0
        public static void ClientServerCase()
        {
            const string url      = "http://localhost:12346/";
            var          listener = new HttpListener();

            listener.Prefixes.Add(url);
            listener.Start();

            var serverTask = Task.Factory.StartNew(() =>
            {
                var context = listener.GetContext();
                var request = context.Request;
                string traceId, contextId;
                bool?isActive;
                RequestExtensions.ExtractFromHttpHeaders(request.Headers, out traceId, out contextId, out isActive);
                using (var serverContext = Trace.ContinueContext(traceId, contextId, isActive ?? false, isRoot: false))
                {
                    serverContext.RecordTimepoint(Timepoint.ServerReceive);
                    context.Response.Close();
                    serverContext.RecordTimepoint(Timepoint.ServerSend);
                }
            });

            using (var clientContext = Trace.CreateRootContext("Client"))
            {
                clientContext.RecordTimepoint(Timepoint.ClientSend);
                clientContext.RecordAnnotation(Annotation.RequestUrl, url);
                var clientRequest = (HttpWebRequest)WebRequest.Create(url);
                clientRequest.SetTracingHeaders(clientContext);
                clientRequest.GetResponse();
                serverTask.Wait();
                clientContext.RecordTimepoint(Timepoint.ClientReceive);
            }

            listener.Close();
        }
示例#12
0
 public void CreateRootContext_should_return_real_context_when_current_context_is_sampled()
 {
     using (var traceContext = Trace.CreateRootContext("Test"))
         traceContext.Should().NotBeSameAs(NoOpTraceContext.Instance);
 }
示例#13
0
 public void CreateRootContext_should_return_NoOp_instance_when_current_context_is_not_sampled()
 {
     using (var traceContext = Trace.CreateRootContext("Test", activate: false))
         traceContext.Should().BeSameAs(NoOpTraceContext.Instance);
 }
示例#14
0
 public void CreateRootContext_should_be_empty_after_stop()
 {
     Trace.Stop();
     Trace.CreateRootContext("Test").Should().BeSameAs(NoOpTraceContext.Instance);
 }
示例#15
0
 public void CreateContext_should_initialize_if_needed_with_disabled_config()
 {
     Trace.Stop();
     using (var context = Trace.CreateRootContext("Test"))
         context.Should().BeSameAs(NoOpTraceContext.Instance);
 }
示例#16
0
 public void CreateChildContext_should_generate_context_id(string contextId)
 {
     using (var rootContext = Trace.CreateRootContext("Test"))
         using (var childContext = Trace.CreateChildContext("Child", contextId))
             childContext.TraceId.Should().NotBeNullOrEmpty();
 }