Example #1
0
        public async Task When_SendingAsyncRespondsWithException_Expect_SetTagError()
        {
            // Arrange
            ServiceCollection services = new ServiceCollection();

            services.AddScoped <ITracer, MockTracer>();

            IServiceProvider serviceProvider = services.BuildServiceProvider();
            ITracer          tracer          = serviceProvider.GetRequiredService <ITracer>();

            IHttpContextAccessor httpContextAccessor = new HttpContextAccessor
            {
                HttpContext = new DefaultHttpContext {
                    RequestServices = serviceProvider
                },
            };

            Exception exception = new Exception();

            OpenTracingSpanTaggingHttpMessageHandler handler = new OpenTracingSpanTaggingHttpMessageHandler(httpContextAccessor)
            {
                InnerHandler = new MockExceptionThrowingInnerHandler(exception),
            };

            Exception result;

            // Act
            using (tracer.BuildSpan("Unit Test").StartActive())
                using (HttpClient client = new HttpClient(handler))
                {
                    result = await Record.ExceptionAsync(() => client.GetAsync(new Uri("http://localhost")));
                }

            // Assert
            MockTracer mockTracer = Assert.IsType <MockTracer>(tracer);
            MockSpan   mockSpan   = Assert.Single(mockTracer.FinishedSpans());

            Assert.True(mockSpan.Tags.ContainsKey("error"));
            Assert.True(Assert.IsType <bool>(mockSpan.Tags["error"]));

            Assert.NotNull(result);
            Assert.Equal(exception, result);
        }
Example #2
0
        public async Task When_SendingAsyncRespondsWithStatusCode_Expect_SetTag(int statusCode, bool expectedError)
        {
            // Arrange
            ServiceCollection services = new ServiceCollection();

            services.AddScoped <ITracer, MockTracer>();

            IServiceProvider serviceProvider = services.BuildServiceProvider();
            ITracer          tracer          = serviceProvider.GetRequiredService <ITracer>();

            IHttpContextAccessor httpContextAccessor = new HttpContextAccessor
            {
                HttpContext = new DefaultHttpContext {
                    RequestServices = serviceProvider
                },
            };

            OpenTracingSpanTaggingHttpMessageHandler handler = new OpenTracingSpanTaggingHttpMessageHandler(httpContextAccessor)
            {
                InnerHandler = new MockResponseReturningInnerHandler(statusCode),
            };

            // Act
            using (tracer.BuildSpan("Unit Test").StartActive())
                using (HttpClient client = new HttpClient(handler))
                {
                    await client.GetAsync(new Uri("http://localhost"));
                }

            // Assert
            MockTracer mockTracer = Assert.IsType <MockTracer>(tracer);
            MockSpan   mockSpan   = Assert.Single(mockTracer.FinishedSpans());

            if (mockSpan.Tags.ContainsKey("error"))
            {
                Assert.Equal(expectedError, mockSpan.Tags["error"]);
            }

            Assert.Equal(expectedError, mockSpan.Tags.ContainsKey("error"));
        }
Example #3
0
        public async Task When_SendingAsyncRespondsWithStatusCodeWithNullActiveSpan_Expect_NoSpans()
        {
            // Arrange
            ServiceCollection services = new ServiceCollection();

            services.AddScoped <ITracer, MockTracer>();

            IServiceProvider serviceProvider = services.BuildServiceProvider();
            ITracer          tracer          = serviceProvider.GetRequiredService <ITracer>();

            IHttpContextAccessor httpContextAccessor = new HttpContextAccessor
            {
                HttpContext = new DefaultHttpContext {
                    RequestServices = serviceProvider
                },
            };

            OpenTracingSpanTaggingHttpMessageHandler handler = new OpenTracingSpanTaggingHttpMessageHandler(httpContextAccessor)
            {
                InnerHandler = new MockResponseReturningInnerHandler(500),
            };

            Exception result;

            // Act
            using (HttpClient client = new HttpClient(handler))
            {
                result = await Record.ExceptionAsync(() => client.GetAsync(new Uri("http://localhost")));
            }

            // Assert
            MockTracer mockTracer = Assert.IsType <MockTracer>(tracer);

            Assert.Empty(mockTracer.FinishedSpans());
            Assert.Null(result);
        }