public void HttpExceptionsContentIsTruncatedByRichTelemetry()
        {
            Environment.SetEnvironmentVariable("ENVIRONMENT", "XUnit");
            ServiceCollection services = new ServiceCollection();
            var channel = new FakeChannel();

            services.AddSingleton <ITelemetryChannel>(channel);
            ServiceHost.ConfigureDefaultServices(services);


            var provider = services.BuildServiceProvider();

            var    client                 = provider.GetRequiredService <TelemetryClient>();
            string bigContent             = "Test content" + new string('*', 10000);
            var    httpOperationException = new HttpOperationException
            {
                Response = new HttpResponseMessageWrapper(
                    new HttpResponseMessage((HttpStatusCode)418),
                    bigContent),
            };

            client.TrackException(httpOperationException);
            client.Flush();

            var exceptionTelemetries = channel.Telemetry.OfType <ExceptionTelemetry>().ToList();

            Assert.Single(exceptionTelemetries);
            var exceptionTelemetry = exceptionTelemetries[0];

            Assert.True(exceptionTelemetry.Properties.TryGetValue("responseText", out var responseText));
            Assert.StartsWith("Test content", responseText);
            Assert.True(responseText.Length < bigContent.Length, "responseText.Length < bigContent.Length");
        }
        public void SetsDependencyData()
        {
            var telemetryChannel = new FakeChannel();
            var config           = new TelemetryConfiguration("00000000-0000-0000-0000-000000000001", telemetryChannel);
            var client           = new TelemetryClient(config);

            StatelessServiceContext ctx = MockBuilder.StatelessServiceContext();

            var gen  = new ProxyGenerator();
            var impl = (IFakeService)gen.CreateInterfaceProxyWithTargetInterface(
                typeof(IFakeService),
                new Type[0],
                Mock.Of <IFakeService>(),
                new LoggingServiceProxyInterceptor(client, ctx, "other://uri.test"));

            impl.TestServiceMethod();
            client.Flush();
            List <DependencyTelemetry> dependencyTelemetries =
                telemetryChannel.Telemetry.OfType <DependencyTelemetry>().ToList();

            dependencyTelemetries.Should().ContainSingle();
            DependencyTelemetry dependencyTelemetry = dependencyTelemetries[0];

            (dependencyTelemetry.Success ?? true).Should().BeTrue();
            dependencyTelemetry.Type.Should().Be("ServiceFabricRemoting");
            dependencyTelemetry.Target.Should().Be("other://uri.test");
            dependencyTelemetry.Data.Should().StartWith("other://uri.test");
            dependencyTelemetry.Data.Should().Contain(nameof(IFakeService));
            dependencyTelemetry.Data.Should().Contain(nameof(IFakeService.TestServiceMethod));

            telemetryChannel.Telemetry.OfType <ExceptionTelemetry>().Should().BeEmpty();
        }
Exemplo n.º 3
0
        public void LogsCorrectUrl()
        {
            var telemetryChannel = new FakeChannel();
            var config           = new TelemetryConfiguration("00000000-0000-0000-0000-000000000001", telemetryChannel);
            var client           = new TelemetryClient(config);

            StatelessServiceContext ctx = MockBuilder.StatelessServiceContext();

            var gen  = new ProxyGenerator();
            var impl = (IFakeService)gen.CreateInterfaceProxyWithTargetInterface(
                typeof(IFakeService),
                new Type[0],
                Mock.Of <IFakeService>(),
                new LoggingServiceInterceptor(ctx, client));

            impl.TestServiceMethod();
            client.Flush();
            RequestTelemetry requestTelemetry = telemetryChannel.Telemetry.OfType <RequestTelemetry>().FirstOrDefault();

            requestTelemetry.Should().NotBeNull();
            (requestTelemetry.Success ?? true).Should().BeTrue();
            requestTelemetry.Url.AbsoluteUri.Should().StartWith(ctx.ServiceName.AbsoluteUri);
            requestTelemetry.Url.AbsoluteUri.Should().Contain(nameof(IFakeService));
            telemetryChannel.Telemetry.OfType <ExceptionTelemetry>().Should().BeEmpty();
        }
Exemplo n.º 4
0
        public void HttpExceptionsAreAugmentedByRichTelemetry()
        {
            Environment.SetEnvironmentVariable("ENVIRONMENT", "XUnit");
            ServiceCollection services = new ServiceCollection();
            var channel = new FakeChannel();

            services.AddSingleton <ITelemetryChannel>(channel);
            ServiceHost.ConfigureDefaultServices(services);


            var provider = services.BuildServiceProvider();

            var client = provider.GetRequiredService <TelemetryClient>();
            var httpOperationException = new HttpOperationException
            {
                Response = new HttpResponseMessageWrapper(
                    new HttpResponseMessage((HttpStatusCode)418),
                    "Test content"),
            };

            client.TrackException(httpOperationException);
            client.Flush();

            var exceptionTelemetries = channel.Telemetry.OfType <ExceptionTelemetry>().ToList();

            exceptionTelemetries.Should().ContainSingle();
            var exceptionTelemetry = exceptionTelemetries[0];

            exceptionTelemetry.Properties.TryGetValue("statusCode", out var statusCodeTest).Should().BeTrue();
            statusCodeTest.Should().Be("418");
            exceptionTelemetry.Properties.TryGetValue("responseText", out var responseText).Should().BeTrue();
            responseText.Should().Be("Test content");
        }
        public void SetsDependencyData()
        {
            var telemetryChannel = new FakeChannel();
            var config           = new TelemetryConfiguration("00000000-0000-0000-0000-000000000001", telemetryChannel);
            var client           = new TelemetryClient(config);

            StatelessServiceContext ctx = MockBuilder.StatelessServiceContext();

            var gen  = new ProxyGenerator();
            var impl = (IFakeService)gen.CreateInterfaceProxyWithTargetInterface(
                typeof(IFakeService),
                new Type[0],
                Mock.Of <IFakeService>(),
                new LoggingServiceProxyInterceptor(client, ctx, "other://uri.test"));

            impl.TestServiceMethod();
            client.Flush();
            List <DependencyTelemetry> dependencyTelemetries =
                telemetryChannel.Telemetry.OfType <DependencyTelemetry>().ToList();

            Assert.Single(dependencyTelemetries);
            DependencyTelemetry dependencyTelemetry = dependencyTelemetries[0];

            Assert.True(dependencyTelemetry.Success ?? true);
            Assert.Equal("ServiceFabricRemoting", dependencyTelemetry.Type);
            Assert.Equal("other://uri.test", dependencyTelemetry.Target);
            Assert.StartsWith("other://uri.test", dependencyTelemetry.Data);
            Assert.Contains(nameof(IFakeService), dependencyTelemetry.Data, StringComparison.OrdinalIgnoreCase);
            Assert.Contains(nameof(IFakeService.TestServiceMethod),
                            dependencyTelemetry.Data,
                            StringComparison.OrdinalIgnoreCase);

            Assert.Empty(telemetryChannel.Telemetry.OfType <ExceptionTelemetry>());
        }
Exemplo n.º 6
0
        public void LogsCorrectUrl()
        {
            var telemetryChannel = new FakeChannel();
            var config           = new TelemetryConfiguration("00000000-0000-0000-0000-000000000001", telemetryChannel);
            var client           = new TelemetryClient(config);

            StatelessServiceContext ctx = MockBuilder.StatelessServiceContext();

            var gen  = new ProxyGenerator();
            var impl = (IFakeService)gen.CreateInterfaceProxyWithTargetInterface(
                typeof(IFakeService),
                new Type[0],
                Mock.Of <IFakeService>(),
                new LoggingServiceInterceptor(ctx, client));

            impl.TestServiceMethod();
            client.Flush();
            RequestTelemetry requestTelemetry = telemetryChannel.Telemetry.OfType <RequestTelemetry>().FirstOrDefault();

            Assert.NotNull(requestTelemetry);
            Assert.True(requestTelemetry.Success ?? true);
            Assert.StartsWith(ctx.ServiceName.AbsoluteUri, requestTelemetry.Url.AbsoluteUri, StringComparison.OrdinalIgnoreCase);
            Assert.Contains(nameof(IFakeService), requestTelemetry.Url.AbsoluteUri, StringComparison.OrdinalIgnoreCase);
            Assert.Empty(telemetryChannel.Telemetry.OfType <ExceptionTelemetry>());
        }
        public void InterceptCreatesScope()
        {
            var telemetryChannel = new FakeChannel();
            var config           = new TelemetryConfiguration("00000000-0000-0000-0000-000000000001", telemetryChannel);
            var client           = new TelemetryClient(config);


            Thing innerThing = null;

            var collection = new ServiceCollection();

            collection.AddSingleton(client);
            collection.AddScoped <IFakeService, FakeService>();
            MockBuilder.RegisterStatelessServiceContext(collection);
            collection.AddScoped <Thing>();
            collection.AddSingleton <Action <Thing> >(t => innerThing = t);
            ServiceProvider provider = collection.BuildServiceProvider();

            var outerThing = provider.GetRequiredService <Thing>();

            var gen  = new ProxyGenerator();
            var impl = (IFakeService)gen.CreateInterfaceProxyWithTargetInterface(
                typeof(IFakeService),
                new Type[0],
                (object)null,
                new InvokeInNewScopeInterceptor <IFakeService>(provider));

            impl.TestServiceMethod().Should().Be(FakeService.RetValue);
            FakeService.Calls.Should().Be(1);
            innerThing.Should().NotBeNull();

            // It's supposed to be a new scope, so it should have gotten a different thing
            innerThing.Should().NotBe(outerThing);

            client.Flush();
            telemetryChannel.Telemetry.OfType <EventTelemetry>().Count(e => e.Name == "TestEvent").Should().Be(1);
            List <RequestTelemetry> requestTelemetries =
                telemetryChannel.Telemetry.OfType <RequestTelemetry>().ToList();

            requestTelemetries.Should().ContainSingle();
            RequestTelemetry requestTelemetry = requestTelemetries[0];

            requestTelemetry.Name.Should().NotBeNull();
            (requestTelemetry.Success ?? true).Should().BeTrue();
            requestTelemetry.Name.Should().Contain("IFakeService");
            requestTelemetry.Name.Should().ContainEquivalentOf("service://TestName");

            telemetryChannel.Telemetry.OfType <ExceptionTelemetry>().Should().BeEmpty();
        }
        public void ComplexAggregateExceptionIsReported()
        {
            var telemetryChannel = new FakeChannel();
            var config           = new TelemetryConfiguration("00000000-0000-0000-0000-000000000001", telemetryChannel);
            var client           = new TelemetryClient(config);

            StatelessServiceContext ctx = MockBuilder.StatelessServiceContext();

            var service = new Mock <IFakeService>();

            service.Setup(s => s.TestServiceMethod())
            .Throws(new AggregateException(new InvalidOperationException("Test exception text"),
                                           new InvalidOperationException("Another test exception text")));

            var gen  = new ProxyGenerator();
            var impl = (IFakeService)gen.CreateInterfaceProxyWithTargetInterface(
                typeof(IFakeService),
                new Type[0],
                service.Object,
                new LoggingServiceProxyInterceptor(client, ctx, "other://uri.test"));

            var invocationException = Assert.Throws <AggregateException>(() => impl.TestServiceMethod());

            client.Flush();
            List <DependencyTelemetry> dependencyTelemetries =
                telemetryChannel.Telemetry.OfType <DependencyTelemetry>().ToList();

            Assert.Single(dependencyTelemetries);
            DependencyTelemetry dependencyTelemetry = dependencyTelemetries[0];

            Assert.False(dependencyTelemetry.Success);
            Assert.Equal("ServiceFabricRemoting", dependencyTelemetry.Type);
            Assert.Equal("other://uri.test", dependencyTelemetry.Target);
            Assert.StartsWith("other://uri.test", dependencyTelemetry.Data);
            Assert.Contains(nameof(IFakeService), dependencyTelemetry.Data, StringComparison.OrdinalIgnoreCase);
            Assert.Contains(nameof(IFakeService.TestServiceMethod),
                            dependencyTelemetry.Data,
                            StringComparison.OrdinalIgnoreCase);

            List <ExceptionTelemetry> exceptionTelemetries =
                telemetryChannel.Telemetry.OfType <ExceptionTelemetry>().ToList();

            Assert.Single(exceptionTelemetries);
            ExceptionTelemetry exceptionTelemetry = exceptionTelemetries[0];

            Assert.Same(invocationException, exceptionTelemetry.Exception);
        }
        public void ComplexAggregateExceptionIsReported()
        {
            var telemetryChannel = new FakeChannel();
            var config           = new TelemetryConfiguration("00000000-0000-0000-0000-000000000001", telemetryChannel);
            var client           = new TelemetryClient(config);

            StatelessServiceContext ctx = MockBuilder.StatelessServiceContext();

            var service = new Mock <IFakeService>();

            service.Setup(s => s.TestServiceMethod())
            .Throws(new AggregateException(new InvalidOperationException("Test exception text"),
                                           new InvalidOperationException("Another test exception text")));

            var gen  = new ProxyGenerator();
            var impl = (IFakeService)gen.CreateInterfaceProxyWithTargetInterface(
                typeof(IFakeService),
                new Type[0],
                service.Object,
                new LoggingServiceProxyInterceptor(client, ctx, "other://uri.test"));

            var invocationException = (((Func <object>)(() => impl.TestServiceMethod()))).Should().Throw <AggregateException>().Which;

            client.Flush();
            List <DependencyTelemetry> dependencyTelemetries =
                telemetryChannel.Telemetry.OfType <DependencyTelemetry>().ToList();

            dependencyTelemetries.Should().ContainSingle();
            DependencyTelemetry dependencyTelemetry = dependencyTelemetries[0];

            dependencyTelemetry.Success.Should().BeFalse();
            dependencyTelemetry.Type.Should().Be("ServiceFabricRemoting");
            dependencyTelemetry.Target.Should().Be("other://uri.test");
            dependencyTelemetry.Data.Should().StartWith("other://uri.test");
            dependencyTelemetry.Data.Should().Contain(nameof(IFakeService));
            dependencyTelemetry.Data.Should().Contain(nameof(IFakeService.TestServiceMethod));

            List <ExceptionTelemetry> exceptionTelemetries =
                telemetryChannel.Telemetry.OfType <ExceptionTelemetry>().ToList();

            exceptionTelemetries.Should().ContainSingle();
            ExceptionTelemetry exceptionTelemetry = exceptionTelemetries[0];

            exceptionTelemetry.Exception.Should().BeSameAs(invocationException);
        }
        public void InterceptCatchesExceptions()
        {
            var telemetryChannel = new FakeChannel();
            var config           = new TelemetryConfiguration("00000000-0000-0000-0000-000000000001", telemetryChannel);
            var client           = new TelemetryClient(config);


            var service = new Mock <IFakeService>();

            service.Setup(s => s.TestServiceMethod()).Throws(new InvalidOperationException("Test Exception Text"));

            var collection = new ServiceCollection();

            collection.AddSingleton(client);
            MockBuilder.RegisterStatelessServiceContext(collection);
            collection.AddSingleton(service.Object);
            ServiceProvider provider = collection.BuildServiceProvider();

            var gen  = new ProxyGenerator();
            var impl = (IFakeService)gen.CreateInterfaceProxyWithTargetInterface(
                typeof(IFakeService),
                new Type[0],
                (object)null,
                new InvokeInNewScopeInterceptor <IFakeService>(provider));

            var ex = (((Func <object>)(() => impl.TestServiceMethod()))).Should().Throw <InvalidOperationException>().Which;

            ex.Message.Should().Be("Test Exception Text");

            client.Flush();

            RequestTelemetry requestTelemetry = telemetryChannel.Telemetry.OfType <RequestTelemetry>().FirstOrDefault();

            requestTelemetry.Should().NotBeNull();
            requestTelemetry.Success.Should().BeFalse();
            ExceptionTelemetry exceptionTelemetry = telemetryChannel.Telemetry.OfType <ExceptionTelemetry>().FirstOrDefault();

            exceptionTelemetry.Should().NotBeNull();
            exceptionTelemetry.Exception.Should().BeSameAs(ex);
        }
Exemplo n.º 11
0
        public void ExceptionLogsFailedRequest()
        {
            var telemetryChannel = new FakeChannel();
            var config           = new TelemetryConfiguration("00000000-0000-0000-0000-000000000001", telemetryChannel);
            var client           = new TelemetryClient(config);

            StatelessServiceContext ctx = MockBuilder.StatelessServiceContext();

            Mock <IFakeService> fakeService = new Mock <IFakeService>();

            fakeService.Setup(s => s.TestServiceMethod()).Throws(new InvalidOperationException("Test Exception Text"));

            var gen  = new ProxyGenerator();
            var impl = (IFakeService)gen.CreateInterfaceProxyWithTargetInterface(
                typeof(IFakeService),
                new Type[0],
                fakeService.Object,
                new LoggingServiceInterceptor(ctx, client));

            var ex = (((Func <object>)(() => impl.TestServiceMethod()))).Should().Throw <InvalidOperationException>().Which;

            ex.Message.Should().Be("Test Exception Text");

            client.Flush();
            List <RequestTelemetry> requestTelemetries =
                telemetryChannel.Telemetry.OfType <RequestTelemetry>().ToList();

            requestTelemetries.Should().ContainSingle();
            RequestTelemetry requestTelemetry = requestTelemetries[0];

            requestTelemetry.Success.Should().BeFalse();
            ExceptionTelemetry exceptionTelemetry = telemetryChannel.Telemetry.OfType <ExceptionTelemetry>().FirstOrDefault();

            exceptionTelemetry.Should().NotBeNull();
            exceptionTelemetry.Exception.Should().BeSameAs(ex);
        }
Exemplo n.º 12
0
        public void ExceptionLogsFailedRequest()
        {
            var telemetryChannel = new FakeChannel();
            var config           = new TelemetryConfiguration("00000000-0000-0000-0000-000000000001", telemetryChannel);
            var client           = new TelemetryClient(config);

            StatelessServiceContext ctx = MockBuilder.StatelessServiceContext();

            Mock <IFakeService> fakeService = new Mock <IFakeService>();

            fakeService.Setup(s => s.TestServiceMethod()).Throws(new InvalidOperationException("Test Exception Text"));

            var gen  = new ProxyGenerator();
            var impl = (IFakeService)gen.CreateInterfaceProxyWithTargetInterface(
                typeof(IFakeService),
                new Type[0],
                fakeService.Object,
                new LoggingServiceInterceptor(ctx, client));

            var ex = Assert.ThrowsAny <InvalidOperationException>(() => impl.TestServiceMethod());

            Assert.Equal("Test Exception Text", ex.Message);

            client.Flush();
            List <RequestTelemetry> requestTelemetries =
                telemetryChannel.Telemetry.OfType <RequestTelemetry>().ToList();

            Assert.Single(requestTelemetries);
            RequestTelemetry requestTelemetry = requestTelemetries[0];

            Assert.False(requestTelemetry.Success);
            ExceptionTelemetry exceptionTelemetry = telemetryChannel.Telemetry.OfType <ExceptionTelemetry>().FirstOrDefault();

            Assert.NotNull(exceptionTelemetry);
            Assert.Same(ex, exceptionTelemetry.Exception);
        }