示例#1
0
        public async Task Invoke_Trace()
        {
            var accessor = new HttpContextAccessor {
                HttpContext = new DefaultHttpContext()
            };
            var context    = CreateHttpContext();
            var tracerMock = CreateIManagedTracerMock(context);

            var delegateMock = new Mock <RequestDelegate>();

            delegateMock.Setup(d => d(context)).Returns(Task.CompletedTask);

            Func <TraceHeaderContext, IManagedTracer> fakeFactory = f => tracerMock.Object;

            Assert.Equal(NullManagedTracer.Instance, ContextTracerManager.GetCurrentTracer(accessor));

            var middleware = new CloudTraceMiddleware(delegateMock.Object, fakeFactory, accessor);
            await middleware.Invoke(context, _traceHeaderContext);

            Assert.Equal(tracerMock.Object, ContextTracerManager.GetCurrentTracer(accessor));

            Assert.True(context.Response.Headers.ContainsKey(TraceHeaderContext.TraceHeader));
            Assert.Equal(_traceHeaderContext.ToString(), context.Response.Headers[TraceHeaderContext.TraceHeader]);

            delegateMock.VerifyAll();
            tracerMock.VerifyAll();
        }
示例#2
0
        public async Task Invoke_TraceException()
        {
            var accessor = new HttpContextAccessor {
                HttpContext = new DefaultHttpContext()
            };
            var context    = CreateHttpContext();
            var tracerMock = CreateIManagedTracerMock(context);

            tracerMock.Setup(t => t.SetStackTrace(It.IsAny <StackTrace>()));

            var delegateMock = new Mock <RequestDelegate>();

            delegateMock.Setup(d => d(context)).Throws(new DivideByZeroException());

            Func <TraceHeaderContext, IManagedTracer> fakeFactory = f => tracerMock.Object;

            var middleware = new CloudTraceMiddleware(delegateMock.Object, fakeFactory, accessor);
            await Assert.ThrowsAsync <DivideByZeroException>(
                () => middleware.Invoke(context, _traceHeaderContext));

            Assert.True(context.Response.Headers.ContainsKey(TraceHeaderContext.TraceHeader));
            Assert.Equal(_traceHeaderContext.ToString(), context.Response.Headers[TraceHeaderContext.TraceHeader]);

            delegateMock.VerifyAll();
            tracerMock.VerifyAll();
        }
        public async Task Invoke_TraceException()
        {
            var accessor = new HttpContextAccessor {
                HttpContext = new DefaultHttpContext()
            };
            var context    = CreateHttpContext();
            var tracerMock = CreateIManagedTracerMock(context);

            tracerMock.Setup(t => t.SetStackTrace(It.IsAny <StackTrace>()));

            var delegateMock = new Mock <RequestDelegate>();

            delegateMock.Setup(d => d(context)).Throws(new Exception());

            var tracerFactoryMock = new Mock <IManagedTracerFactory>();

            tracerFactoryMock.Setup(f => f.CreateTracer(_traceHeaderContext)).Returns(tracerMock.Object);

            var middleware = new CloudTraceMiddleware(delegateMock.Object, tracerFactoryMock.Object, accessor);
            await Assert.ThrowsAsync <Exception>(
                () => middleware.Invoke(context, _traceHeaderContext));

            delegateMock.VerifyAll();
            tracerMock.VerifyAll();
        }
示例#4
0
        public async Task Invoke_Trace()
        {
            var context    = CreateHttpContext();
            var tracerMock = CreateIManagedTracerMock(context);

            var delegateMock = new Mock <RequestDelegate>();

            delegateMock.Setup(d => d(context)).Returns(Task.CompletedTask);

            Func <TraceHeaderContext, IManagedTracer> fakeFactory = f => tracerMock.Object;

            Assert.Equal(NullManagedTracer.Instance, ContextTracerManager.GetCurrentTracer());

            var middleware = new CloudTraceMiddleware(delegateMock.Object, fakeFactory, new DefaultCloudTraceNameProvider());
            await middleware.Invoke(context, _traceHeaderContext);

            // Since the current tracer is AsyncLocal<>, it will be back to the default after awaiting the middleware invoke
            Assert.Equal(NullManagedTracer.Instance, ContextTracerManager.GetCurrentTracer());

            Assert.True(context.Response.Headers.ContainsKey(TraceHeaderContext.TraceHeader));
            Assert.Equal(_traceHeaderContext.ToString(), context.Response.Headers[TraceHeaderContext.TraceHeader]);

            delegateMock.VerifyAll();
            tracerMock.VerifyAll();
        }
        public async Task Invoke_Trace()
        {
            var accessor = new HttpContextAccessor {
                HttpContext = new DefaultHttpContext()
            };
            var context    = CreateHttpContext();
            var tracerMock = CreateIManagedTracerMock(context);

            var delegateMock = new Mock <RequestDelegate>();

            delegateMock.Setup(d => d(context)).Returns(Task.CompletedTask);

            var tracerFactoryMock = new Mock <IManagedTracerFactory>();

            tracerFactoryMock.Setup(f => f.CreateTracer(_traceHeaderContext)).Returns(tracerMock.Object);

            Assert.Equal(NullManagedTracer.Instance, ContextTracerManager.GetCurrentTracer(accessor));

            var middleware = new CloudTraceMiddleware(delegateMock.Object, tracerFactoryMock.Object, accessor);
            await middleware.Invoke(context, _traceHeaderContext);

            Assert.Equal(tracerMock.Object, ContextTracerManager.GetCurrentTracer(accessor));

            delegateMock.VerifyAll();
            tracerMock.VerifyAll();
        }
示例#6
0
        public async Task Invoke_TraceException()
        {
            var context = new DefaultHttpContext();
            var request = new DefaultHttpRequest(context);

            request.Path = new PathString("/api/trace");

            var delegateMock = new Mock <RequestDelegate>();

            delegateMock.Setup(d => d(context)).Throws(new Exception());

            var tracerMock = new Mock <IManagedTracer>();

            tracerMock.Setup(t => t.GetCurrentTraceId()).Returns("trace-id");
            tracerMock.Setup(t => t.StartSpan(request.Path, null));
            tracerMock.Setup(t => t.AnnotateSpan(It.IsAny <Dictionary <string, string> >()));
            tracerMock.Setup(t => t.SetStackTrace(It.IsAny <StackTrace>()));
            tracerMock.Setup(t => t.EndSpan());

            var middleware = new CloudTraceMiddleware(delegateMock.Object);
            await Assert.ThrowsAsync <Exception>(
                () => middleware.Invoke(context, tracerMock.Object));

            delegateMock.VerifyAll();
            tracerMock.VerifyAll();
        }
示例#7
0
        public async Task Invoke_NoTrace()
        {
            var context      = new DefaultHttpContext();
            var delegateMock = new Mock <RequestDelegate>();
            var tracerMock   = new Mock <IManagedTracer>();

            tracerMock.Setup(t => t.GetCurrentTraceId()).Returns((string)null);

            var middleware = new CloudTraceMiddleware(delegateMock.Object);
            await middleware.Invoke(context, tracerMock.Object);

            delegateMock.Verify(d => d(context), Times.Once());
            tracerMock.Verify(t => t.StartSpan(It.IsAny <string>(), null), Times.Never());
            tracerMock.Verify(t => t.EndSpan(), Times.Never());
        }
        public async Task Invoke_NoTrace()
        {
            var context      = new DefaultHttpContext();
            var delegateMock = new Mock <RequestDelegate>();
            var tracerMock   = new Mock <IManagedTracer>();

            Func <ITraceContext, IManagedTracer> fakeFactory = f => tracerMock.Object;

            var middleware = new CloudTraceMiddleware(delegateMock.Object, fakeFactory, new DefaultCloudTraceNameProvider());
            await middleware.Invoke(context, _traceContext, TraceDecisionPredicate.Default, CustomTraceContextPropagator);

            // Since the current tracer is AsyncLocal<>, it will be back to the default after awaiting the middleware invoke
            Assert.Equal(NullManagedTracer.Instance, ContextTracerManager.GetCurrentTracer());

            AssertNoTraceContext(context.Response);

            delegateMock.Verify(d => d(context), Times.Once());
            tracerMock.Verify(t => t.StartSpan(It.IsAny <string>(), null), Times.Never());
        }
示例#9
0
        public async Task Invoke_NoTrace()
        {
            var context      = new DefaultHttpContext();
            var delegateMock = new Mock <RequestDelegate>();
            var tracerMock   = new Mock <IManagedTracer>();

            Func <TraceHeaderContext, IManagedTracer> fakeFactory = f => tracerMock.Object;

            var middleware = new CloudTraceMiddleware(delegateMock.Object, fakeFactory);
            await middleware.Invoke(context, _traceHeaderContext);

            // Since the current tracer is AsyncLocal<>, it will be back to the default after awaiting the middleware invoke
            Assert.Equal(NullManagedTracer.Instance, ContextTracerManager.GetCurrentTracer());

            Assert.False(context.Response.Headers.ContainsKey(TraceHeaderContext.TraceHeader));

            delegateMock.Verify(d => d(context), Times.Once());
            tracerMock.Verify(t => t.StartSpan(It.IsAny <string>(), null), Times.Never());
        }
示例#10
0
        public async Task Invoke_NoTrace()
        {
            var accessor = new HttpContextAccessor {
                HttpContext = new DefaultHttpContext()
            };
            var context      = new DefaultHttpContext();
            var delegateMock = new Mock <RequestDelegate>();
            var tracerMock   = new Mock <IManagedTracer>();

            Func <TraceHeaderContext, IManagedTracer> fakeFactory = f => tracerMock.Object;

            var middleware = new CloudTraceMiddleware(delegateMock.Object, fakeFactory, accessor);
            await middleware.Invoke(context, _traceHeaderContext);

            Assert.Equal(tracerMock.Object, ContextTracerManager.GetCurrentTracer(accessor));
            Assert.False(context.Response.Headers.ContainsKey(TraceHeaderContext.TraceHeader));

            delegateMock.Verify(d => d(context), Times.Once());
            tracerMock.Verify(t => t.StartSpan(It.IsAny <string>(), null), Times.Never());
        }
        public async Task Invoke_TraceException()
        {
            var context    = CreateHttpContext();
            var tracerMock = CreateIManagedTracerMock(context);

            tracerMock.Setup(t => t.SetStackTrace(It.IsAny <StackTrace>()));

            var delegateMock = new Mock <RequestDelegate>();

            delegateMock.Setup(d => d(context)).Throws(new DivideByZeroException());

            Func <ITraceContext, IManagedTracer> fakeFactory = f => tracerMock.Object;

            var middleware = new CloudTraceMiddleware(delegateMock.Object, fakeFactory, new DefaultCloudTraceNameProvider());
            await Assert.ThrowsAsync <DivideByZeroException>(
                () => middleware.Invoke(context, _traceContext, TraceDecisionPredicate.Default, CustomTraceContextPropagator));

            AssertCustomTraceContext(context.Response);

            delegateMock.VerifyAll();
            tracerMock.VerifyAll();
        }
        public async Task Invoke_NoTrace()
        {
            var accessor = new HttpContextAccessor {
                HttpContext = new DefaultHttpContext()
            };
            var context      = new DefaultHttpContext();
            var delegateMock = new Mock <RequestDelegate>();
            var tracerMock   = new Mock <IManagedTracer>();

            var tracerFactoryMock = new Mock <IManagedTracerFactory>();

            tracerFactoryMock.Setup(f => f.CreateTracer(_traceHeaderContext)).Returns(tracerMock.Object);

            var middleware = new CloudTraceMiddleware(delegateMock.Object, tracerFactoryMock.Object, accessor);
            await middleware.Invoke(context, _traceHeaderContext);

            Assert.Equal(tracerMock.Object, ContextTracerManager.GetCurrentTracer(accessor));

            delegateMock.Verify(d => d(context), Times.Once());
            tracerMock.Verify(t => t.StartSpan(It.IsAny <string>(), null), Times.Never());
            tracerMock.Verify(t => t.EndSpan(), Times.Never());
        }
        public async Task Invoke_Trace()
        {
            var context    = CreateHttpContext();
            var tracerMock = CreateIManagedTracerMock(context);

            var delegateMock = new Mock <RequestDelegate>();

            delegateMock.Setup(d => d(context)).Returns(Task.CompletedTask);

            Func <ITraceContext, IManagedTracer> fakeFactory = f => tracerMock.Object;

            Assert.Equal(NullManagedTracer.Instance, ContextTracerManager.GetCurrentTracer());

            var middleware = new CloudTraceMiddleware(delegateMock.Object, fakeFactory, new DefaultCloudTraceNameProvider());
            await middleware.Invoke(context, _traceContext, TraceDecisionPredicate.Default, CustomTraceContextPropagator);

            // Since the current tracer is AsyncLocal<>, it will be back to the default after awaiting the middleware invoke
            Assert.Equal(NullManagedTracer.Instance, ContextTracerManager.GetCurrentTracer());

            AssertCustomTraceContext(context.Response);

            delegateMock.VerifyAll();
            tracerMock.VerifyAll();
        }