Exemplo n.º 1
0
        public async Task Can_Send_Request_Telemetry(int statusCode, bool expectedSuccess)
        {
            // given
            var channel = new MockTelemetryChannel();

            var response = Mock.Of <IOwinResponse>(r => r.StatusCode == statusCode);

            var context = new MockOwinContextBuilder()
                          .WithResponse(response)
                          .Build();

            var configuration = new TelemetryConfigurationBuilder()
                                .WithChannel(channel)
                                .Build();

            var sut = new HttpRequestTrackingMiddleware(new NoopMiddleware(), configuration);

            // when
            await sut.Invoke(context);

            // then
            var telemetry = channel.SentTelemetries.First() as RequestTelemetry;

            telemetry.Success.Should().Be(expectedSuccess);
        }
Exemplo n.º 2
0
            private static void ConfigureApplicationInsights(IAppBuilder app)
            {
                Channel = new MockTelemetryChannel();

                var config = new TelemetryConfigurationBuilder()
                             .WithChannel(Channel)
                             .WithTelemetryInitializer(new OperationIdTelemetryInitializer())
                             .Build();

                DependencyTrackingTelemetryModule = new DependencyTrackingTelemetryModule();
                DependencyTrackingTelemetryModule.Initialize(config);

                app.UseApplicationInsights(
                    new RequestTrackingConfiguration
                {
                    TelemetryConfiguration         = config,
                    GetAdditionalContextProperties = async ctx =>
                    {
                        // do some async stuff
                        await Task.Delay(100).ConfigureAwait(false);
                        return(Enumerable.Empty <KeyValuePair <string, string> >());
                    }
                },
                    new OperationIdContextMiddlewareConfiguration
                {
                    OperationIdFactory = IdFactory.FromHeader(Consts.OperationIdHeaderName)
                }
                    );
            }
Exemplo n.º 3
0
        public async Task Should_Skip_Request_Telemetry_When_Filtered_Out()
        {
            // given
            var channel = new MockTelemetryChannel();

            var request = Mock.Of <IOwinRequest>(r =>
                                                 r.Method == "GET" &&
                                                 r.Path == new PathString("/path") &&
                                                 r.Uri == new Uri("http://google.com/path")
                                                 );

            var response = Mock.Of <IOwinResponse>(r => r.StatusCode == 200);

            var context = new MockOwinContextBuilder()
                          .WithRequest(request)
                          .WithResponse(response)
                          .Build();

            var configuration = new TelemetryConfigurationBuilder()
                                .WithChannel(channel)
                                .Build();


            var sut = new OperationIdContextMiddleware(
                new HttpRequestTrackingMiddleware(
                    new NoopMiddleware(), configuration, (req, resp) => false),
                new OperationIdContextMiddlewareConfiguration());

            // when
            await sut.Invoke(context);

            // then
            channel.SentTelemetries.Count.Should().Be(0);
        }
Exemplo n.º 4
0
        public async Task Should_Add_Properties_To_Request_Telemetry_Context_When_They_Are_Provided()
        {
            // given
            var channel = new MockTelemetryChannel();

            var request = Mock.Of <IOwinRequest>(r =>
                                                 r.Method == "GET" &&
                                                 r.Path == new PathString("/path") &&
                                                 r.Uri == new Uri("http://google.com/path")
                                                 );

            var response = Mock.Of <IOwinResponse>(r => r.StatusCode == 200);

            var context = new MockOwinContextBuilder()
                          .WithRequest(request)
                          .WithResponse(response)
                          .Build();

            var configuration = new TelemetryConfigurationBuilder()
                                .WithChannel(channel)
                                .Build();


            var sut = new OperationIdContextMiddleware(
                new HttpRequestTrackingMiddleware(
                    new NoopMiddleware(), configuration, getContextProperties: (req, res) => new[]
            {
                new KeyValuePair <string, string>("key1", "val1"),
                new KeyValuePair <string, string>("key2", "val2"),
            }),
                new OperationIdContextMiddlewareConfiguration());

            // when
            await sut.Invoke(context);

            // then
            channel.SentTelemetries.Count.Should().Be(1);

            var telemetry = channel.SentTelemetries.First() as RequestTelemetry;

            telemetry.Should().NotBeNull();

            telemetry.HttpMethod.Should().Be("GET");
            telemetry.Name.Should().Be("GET /path");
            telemetry.Context.Operation.Name.Should().Be("GET /path");
            telemetry.Id.Should().NotBeNullOrEmpty();
            telemetry.Success.Should().BeTrue();
            telemetry.Url.Should().Be(new Uri("http://google.com/path"));
            telemetry.StartTime.Date.Should().Be(DateTimeOffset.Now.Date);
            telemetry.Context.Properties.Should().Contain(new[]
            {
                new KeyValuePair <string, string>("key1", "val1"),
                new KeyValuePair <string, string>("key2", "val2"),
            });
        }
            private static void ConfigureApplicationInsights(IAppBuilder app)
            {
                Channel = new MockTelemetryChannel();

                var telemetryConfig = new TelemetryConfigurationBuilder()
                                      .WithChannel(Channel)
                                      .WithTelemetryInitializer(new OperationIdTelemetryInitializer())
                                      .Build();

                DependencyTrackingTelemetryModule = new DependencyTrackingTelemetryModule();
                DependencyTrackingTelemetryModule.Initialize(telemetryConfig);
                app.UseApplicationInsights(telemetryConfiguration: telemetryConfig);
            }
Exemplo n.º 6
0
        public static TelemetryClient Initialize()
        {
            var mockTelemetryChannel = new MockTelemetryChannel();
            var mockTelemetryConfig  = new TelemetryConfiguration
            {
                TelemetryChannel   = mockTelemetryChannel,
                InstrumentationKey = Guid.NewGuid().ToString(),
            };

            var mockTelemetryClient = new TelemetryClient(mockTelemetryConfig);

            return(mockTelemetryClient);
        }
        private TelemetryClient InitializeMockTelemetryChannel()
        {
            // Application Insights TelemetryClient doesn't have an interface (and is sealed) hence implementation of a class that implements a mock
            MockTelemetryChannel   mockTelemetryChannel       = new MockTelemetryChannel();
            TelemetryConfiguration mockTelemetryConfiguration = new TelemetryConfiguration
            {
                TelemetryChannel   = mockTelemetryChannel,
                InstrumentationKey = Guid.NewGuid().ToString(),
            };

            TelemetryClient mockTelemetryClient = new TelemetryClient(mockTelemetryConfiguration);

            return(mockTelemetryClient);
        }
Exemplo n.º 8
0
        public async Task Can_Send_Request_Telemetry()
        {
            // given
            var channel = new MockTelemetryChannel();

            var request = Mock.Of <IOwinRequest>(r =>
                                                 r.Method == "GET" &&
                                                 r.Path == new PathString("/path") &&
                                                 r.Uri == new Uri("http://google.com/path")
                                                 );

            var response = Mock.Of <IOwinResponse>(r => r.StatusCode == 200);

            var context = new MockOwinContextBuilder()
                          .WithRequest(request)
                          .WithResponse(response)
                          .Build();

            var configuration = new TelemetryConfigurationBuilder()
                                .WithChannel(channel)
                                .Build();


            var sut = new OperationIdContextMiddleware(
                new HttpRequestTrackingMiddleware(
                    new NoopMiddleware(), configuration),
                new OperationIdContextMiddlewareConfiguration());

            // when
            await sut.Invoke(context);

            // then
            channel.SentTelemetries.Count.Should().Be(1);

            var telemetry = channel.SentTelemetries.First() as RequestTelemetry;

            telemetry.Should().NotBeNull();

            telemetry.HttpMethod.Should().Be("GET");
            telemetry.Name.Should().Be("GET /path");
            telemetry.Context.Operation.Name.Should().Be("GET /path");
            telemetry.Id.Should().NotBeNullOrEmpty();
            telemetry.Success.Should().BeTrue();
            telemetry.Url.Should().Be(new Uri("http://google.com/path"));
            telemetry.StartTime.Date.Should().Be(DateTimeOffset.Now.Date);
        }
            private static void ConfigureApplicationInsights(IAppBuilder app)
            {
                Channel = new MockTelemetryChannel();

                var config = new TelemetryConfigurationBuilder()
                             .WithChannel(Channel)
                             .WithTelemetryInitializer(new OperationIdTelemetryInitializer())
                             .Build();

                DependencyTrackingTelemetryModule = new DependencyTrackingTelemetryModule();
                DependencyTrackingTelemetryModule.Initialize(config);

                app.UseApplicationInsights(
                    middlewareConfiguration:
                    new OperationIdContextMiddlewareConfiguration {
                    ShouldTryGetIdFromHeader = true
                },
                    telemetryConfiguration: config);
            }
Exemplo n.º 10
0
        public async Task Can_Pass_Request_Details_For_Filtering()
        {
            // given
            var channel = new MockTelemetryChannel();

            var request = Mock.Of <IOwinRequest>(r =>
                                                 r.Method == "GET" &&
                                                 r.Path == new PathString("/path") &&
                                                 r.Uri == new Uri("http://google.com/path")
                                                 );

            var response = Mock.Of <IOwinResponse>(r => r.StatusCode == 200);

            var context = new MockOwinContextBuilder()
                          .WithRequest(request)
                          .WithResponse(response)
                          .Build();

            var configuration = new TelemetryConfigurationBuilder()
                                .WithChannel(channel)
                                .Build();
            IOwinRequest  filteredRequest  = null;
            IOwinResponse filteredResponse = null;

            var sut = new OperationIdContextMiddleware(
                new HttpRequestTrackingMiddleware(
                    new NoopMiddleware(), configuration, (req, resp) =>
            {
                filteredRequest  = req;
                filteredResponse = resp;
                return(false);
            }),
                new OperationIdContextMiddlewareConfiguration());

            // when
            await sut.Invoke(context);

            // then
            filteredRequest.ShouldBeEquivalentTo(request);
            filteredResponse.ShouldBeEquivalentTo(response);
        }