Exemplo n.º 1
0
            public static void ConfigureApplicationInsightsTelemetryModuleWorks()
            {
                //ARRANGE
                var services = ApplicationInsightsExtensionsTests.GetServiceCollectionWithContextAccessor();

                services.AddSingleton <ITelemetryModule, TestTelemetryModule>();

                //ACT
                services.ConfigureTelemetryModule <TestTelemetryModule>
                    (module => module.CustomProperty = "mycustomvalue");
                services.AddApplicationInsightsTelemetry(new ConfigurationBuilder().Build());
                IServiceProvider serviceProvider = services.BuildServiceProvider();

                // Requesting TelemetryConfiguration from services trigger constructing the TelemetryConfiguration
                // which in turn trigger configuration of all modules.
                var telemetryConfiguration = serviceProvider.GetTelemetryConfiguration();

                //VALIDATE
                var modules             = serviceProvider.GetServices <ITelemetryModule>();
                var testTelemetryModule = modules.OfType <TestTelemetryModule>().Single();

                //The module should be initialized and configured as instructed.
                Assert.NotNull(testTelemetryModule);
                Assert.Equal("mycustomvalue", testTelemetryModule.CustomProperty);
                Assert.True(testTelemetryModule.IsInitialized);
            }
Exemplo n.º 2
0
            public static void AppApplicationInsightsTelemetryFromApplicationInsightsServiceOptionsCopiesAllSettings()
            {
                ServiceCollection services = ApplicationInsightsExtensionsTests.GetServiceCollectionWithContextAccessor();
                ApplicationInsightsServiceOptions options = new ApplicationInsightsServiceOptions()
                {
                    ApplicationVersion     = "test",
                    DeveloperMode          = true,
                    EnableAdaptiveSampling = false,
                    EnableAuthenticationTrackingJavaScript = false,
                    EnableDebugLogger            = true,
                    EnableQuickPulseMetricStream = false,
                    EndpointAddress    = "http://test",
                    InstrumentationKey = "test"
                };

                services.AddApplicationInsightsTelemetry(options);
                ApplicationInsightsServiceOptions servicesOptions = null;

                services.Configure((ApplicationInsightsServiceOptions o) =>
                {
                    servicesOptions = o;
                });

                IServiceProvider       serviceProvider        = services.BuildServiceProvider();
                TelemetryConfiguration telemetryConfiguration = serviceProvider.GetTelemetryConfiguration();

                Type optionsType = typeof(ApplicationInsightsServiceOptions);

                PropertyInfo[] properties = optionsType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
                Assert.True(properties.Length > 0);
                foreach (PropertyInfo property in properties)
                {
                    Assert.Equal(property.GetValue(options).ToString(), property.GetValue(servicesOptions).ToString());
                }
            }
        public static ServiceCollection CreateServicesAndAddApplicationinsightsTelemetry(string jsonPath, string channelEndPointAddress, ApplicationInsightsServiceOptions serviceOptions = null, bool addChannel = true)
        {
            var services = ApplicationInsightsExtensionsTests.GetServiceCollectionWithContextAccessor();

            if (addChannel)
            {
                services.AddSingleton <ITelemetryChannel>(new InMemoryChannel());
            }
            IConfigurationRoot config = null;

            if (jsonPath != null)
            {
                config = new ConfigurationBuilder().AddJsonFile(jsonPath).Build();
            }
            else if (channelEndPointAddress != null)
            {
                config = new ConfigurationBuilder().AddApplicationInsightsSettings(endpointAddress: channelEndPointAddress).Build();
            }
            else
            {
                config = new ConfigurationBuilder().Build();
            }

            services.AddApplicationInsightsTelemetry(config, serviceOptions);
            return(services);
        }
Exemplo n.º 4
0
            public static void VerifyNoExceptionWhenAppIdProviderNotFoundFoundInDI()
            {
                // ARRANGE
                var services = ApplicationInsightsExtensionsTests.GetServiceCollectionWithContextAccessor();
                var config   = new ConfigurationBuilder().AddApplicationInsightsSettings(endpointAddress: "http://localhost:1234/v2/track/").Build();

                services.AddApplicationInsightsTelemetry(config);

                for (var i = services.Count - 1; i >= 0; i--)
                {
                    var descriptor = services[i];
                    if (descriptor.ServiceType == typeof(IApplicationIdProvider))
                    {
                        services.RemoveAt(i);
                    }
                }


                IServiceProvider serviceProvider = services.BuildServiceProvider();

                var operationCorrelationTelemetryInitializer = serviceProvider.GetServices <ITelemetryInitializer>().FirstOrDefault(x => x.GetType()
                                                                                                                                    == typeof(ApplicationInsights.AspNetCore.TelemetryInitializers.OperationCorrelationTelemetryInitializer));

                Assert.NotNull(operationCorrelationTelemetryInitializer); // this verifies the instance was created without exception


                var hostingDiagnosticListener = serviceProvider.GetServices <IApplicationInsightDiagnosticListener>().FirstOrDefault(x => x.GetType()
                                                                                                                                     == typeof(HostingDiagnosticListener));

                Assert.NotNull(hostingDiagnosticListener); // this verifies the instance was created without exception
            }
Exemplo n.º 5
0
            public static void ConfigureApplicationInsightsTelemetryModuleDoesNotThrowIfModuleNotFound()
            {
                //ARRANGE
                var services = ApplicationInsightsExtensionsTests.GetServiceCollectionWithContextAccessor();

                // Intentionally NOT adding the module
                // services.AddSingleton<ITelemetryModule, TestTelemetryModule>();

                //ACT
                services.ConfigureTelemetryModule <TestTelemetryModule>
                    (module => module.CustomProperty = "mycustomvalue");
                services.AddApplicationInsightsTelemetry(new ConfigurationBuilder().Build());
                IServiceProvider serviceProvider = services.BuildServiceProvider();

                // Requesting TelemetryConfiguration from services trigger constructing the TelemetryConfiguration
                // which in turn trigger configuration of all modules.
                var telemetryConfiguration = serviceProvider.GetTelemetryConfiguration();

                //VALIDATE
                var modules             = serviceProvider.GetServices <ITelemetryModule>();
                var testTelemetryModule = modules.OfType <TestTelemetryModule>().FirstOrDefault();

                // No exceptions thrown here.
                Assert.Null(testTelemetryModule);
            }
Exemplo n.º 6
0
            public static void FallbacktoDefaultChannelWhenNoChannelFoundInDI()
            {
                // ARRANGE
                var services = ApplicationInsightsExtensionsTests.GetServiceCollectionWithContextAccessor();
                var config   = new ConfigurationBuilder().AddApplicationInsightsSettings(endpointAddress: "http://localhost:1234/v2/track/").Build();

                services.AddApplicationInsightsTelemetry(config);

                // Remove all ITelemetryChannel to simulate scenario where customer remove all channel from DI but forgot to add new one.
                // This should not crash application startup, and should fall back to default channel supplied by base sdk.
                for (var i = services.Count - 1; i >= 0; i--)
                {
                    var descriptor = services[i];
                    if (descriptor.ServiceType == typeof(ITelemetryChannel))
                    {
                        services.RemoveAt(i);
                    }
                }

                // VERIFY that default channel is configured when nothing is present in DI
                IServiceProvider serviceProvider = services.BuildServiceProvider();
                var telemetryConfiguration       = serviceProvider.GetTelemetryConfiguration();

                Assert.Equal(typeof(InMemoryChannel), telemetryConfiguration.TelemetryChannel.GetType());
            }
Exemplo n.º 7
0
            public static void AddApplicationInsightsTelemetryProcessorWithNonTelemetryProcessorTypeThrows()
            {
                var services = ApplicationInsightsExtensionsTests.GetServiceCollectionWithContextAccessor();

                Assert.Throws <ArgumentException>(() => services.AddApplicationInsightsTelemetryProcessor(typeof(string)));
                Assert.Throws <ArgumentException>(() => services.AddApplicationInsightsTelemetryProcessor(typeof(ITelemetryProcessor)));
            }
Exemplo n.º 8
0
            public static void ConfigureApplicationInsightsTelemetryModuleThrowsIfConfigureIsNull()
            {
                //ARRANGE
                var services = ApplicationInsightsExtensionsTests.GetServiceCollectionWithContextAccessor();

                services.AddSingleton <ITelemetryModule, TestTelemetryModule>();

                //ACT and VALIDATE
                Assert.Throws <ArgumentNullException>(() => services.ConfigureTelemetryModule <TestTelemetryModule>(null));
            }
Exemplo n.º 9
0
            public static void AddApplicationInsightsTelemetryProcessorWithImportingConstructor()
            {
                var services = ApplicationInsightsExtensionsTests.GetServiceCollectionWithContextAccessor();

                services.AddApplicationInsightsTelemetryProcessor <FakeTelemetryProcessorWithImportingConstructor>();
                services.AddApplicationInsightsTelemetry(new ConfigurationBuilder().Build());
                IServiceProvider serviceProvider = services.BuildServiceProvider();
                var telemetryConfiguration       = serviceProvider.GetTelemetryConfiguration();
                FakeTelemetryProcessorWithImportingConstructor telemetryProcessor = telemetryConfiguration.TelemetryProcessors.OfType <FakeTelemetryProcessorWithImportingConstructor>().FirstOrDefault();

                Assert.NotNull(telemetryProcessor);
                Assert.Same(serviceProvider.GetService <IHostingEnvironment>(), telemetryProcessor.HostingEnvironment);
            }
            public static void RegistersTelemetryConfigurationFactoryMethodThatReadsDeveloperModeFromSettings()
            {
                var services = ApplicationInsightsExtensionsTests.GetServiceCollectionWithContextAccessor();

                services.AddSingleton <ITelemetryChannel>(new InMemoryChannel());
                var config = new ConfigurationBuilder().AddApplicationInsightsSettings(developerMode: true).Build();

                services.AddApplicationInsightsTelemetry(config);

                IServiceProvider serviceProvider = services.BuildServiceProvider();
                var telemetryConfiguration       = serviceProvider.GetRequiredService <TelemetryConfiguration>();

                Assert.Equal(true, telemetryConfiguration.TelemetryChannel.DeveloperMode);
            }
            public static void DoesNotOverWriteExistingChannelInFullFramework()
            {
                var services = ApplicationInsightsExtensionsTests.GetServiceCollectionWithContextAccessor();

                services.AddSingleton <ITelemetryChannel, InMemoryChannel>();
                var config = new ConfigurationBuilder().AddApplicationInsightsSettings(endpointAddress: "http://localhost:1234/v2/track/").Build();

                services.AddApplicationInsightsTelemetry(config);

                IServiceProvider serviceProvider = services.BuildServiceProvider();
                var telemetryConfiguration       = serviceProvider.GetRequiredService <TelemetryConfiguration>();

                Assert.Equal(telemetryConfiguration.TelemetryChannel.GetType(), typeof(InMemoryChannel));
            }
            public static void RegistersTelemetryConfigurationFactoryMethodThatPopulatesItWithTelemetryChannelFromContainer()
            {
                var telemetryChannel = new FakeTelemetryChannel();
                var services         = ApplicationInsightsExtensionsTests.GetServiceCollectionWithContextAccessor();

                services.AddSingleton <ITelemetryChannel>(telemetryChannel);

                services.AddApplicationInsightsTelemetry(new ConfigurationBuilder().Build());

                IServiceProvider serviceProvider = services.BuildServiceProvider();
                var telemetryConfiguration       = serviceProvider.GetRequiredService <TelemetryConfiguration>();

                Assert.Same(telemetryChannel, telemetryConfiguration.TelemetryChannel);
            }
Exemplo n.º 13
0
            public static void RegistersTelemetryConfigurationFactoryMethodThatReadsInstrumentationKeyFromSettings()
            {
                var services = ApplicationInsightsExtensionsTests.GetServiceCollectionWithContextAccessor();

                services.AddSingleton <ITelemetryChannel>(new InMemoryChannel());
                var config = new ConfigurationBuilder().AddApplicationInsightsSettings(instrumentationKey: TestInstrumentationKey).Build();

                services.AddApplicationInsightsTelemetry(config);

                IServiceProvider serviceProvider = services.BuildServiceProvider();
                var telemetryConfiguration       = serviceProvider.GetTelemetryConfiguration();

                Assert.Equal(TestInstrumentationKey, telemetryConfiguration.InstrumentationKey);
            }
Exemplo n.º 14
0
            public static void VerifyUserCanOverrideAppIdProvider()
            {
                var services = ApplicationInsightsExtensionsTests.GetServiceCollectionWithContextAccessor();

                services.AddSingleton <IApplicationIdProvider, MockApplicationIdProvider>(); // assume user tries to define own implementation
                var config = new ConfigurationBuilder().AddApplicationInsightsSettings(endpointAddress: "http://localhost:1234/v2/track/").Build();

                services.AddApplicationInsightsTelemetry(config);

                IServiceProvider serviceProvider = services.BuildServiceProvider();
                var applicationIdProvider        = serviceProvider.GetRequiredService <IApplicationIdProvider>();

                Assert.Equal(typeof(MockApplicationIdProvider), applicationIdProvider.GetType());
            }
Exemplo n.º 15
0
            public static void RegistersTelemetryConfigurationFactoryMethodThatPopulatesItWithTelemetryProcessorFactoriesFromContainer()
            {
                var services = ApplicationInsightsExtensionsTests.GetServiceCollectionWithContextAccessor();

                services.AddSingleton <ITelemetryProcessorFactory>(new MockTelemetryProcessorFactory((next) => new FakeTelemetryProcessor(next)));

                services.AddApplicationInsightsTelemetry(new ConfigurationBuilder().Build());

                IServiceProvider serviceProvider          = services.BuildServiceProvider();
                var telemetryConfiguration                = serviceProvider.GetTelemetryConfiguration();
                FakeTelemetryProcessor telemetryProcessor = telemetryConfiguration.TelemetryProcessors.OfType <FakeTelemetryProcessor>().FirstOrDefault();

                Assert.NotNull(telemetryProcessor);
                Assert.True(telemetryProcessor.IsInitialized);
            }
Exemplo n.º 16
0
            public static void RegistersTelemetryConfigurationFactoryMethodThatPopulatesItWithTelemetryInitializersFromContainer()
            {
                var telemetryInitializer = new FakeTelemetryInitializer();
                var services             = ApplicationInsightsExtensionsTests.GetServiceCollectionWithContextAccessor();

                services.AddSingleton <ITelemetryInitializer>(telemetryInitializer);
                services.AddSingleton <ITelemetryChannel>(new InMemoryChannel());

                services.AddApplicationInsightsTelemetry(new ConfigurationBuilder().Build());

                IServiceProvider serviceProvider = services.BuildServiceProvider();
                var telemetryConfiguration       = serviceProvider.GetTelemetryConfiguration();

                Assert.Contains(telemetryInitializer, telemetryConfiguration.TelemetryInitializers);
            }
            public static void RegistersTelemetryConfigurationFactoryMethodThatReadsEndpointAddressFromEnvironment()
            {
                var services = ApplicationInsightsExtensionsTests.GetServiceCollectionWithContextAccessor();

                services.AddSingleton <ITelemetryChannel>(new InMemoryChannel());
                Environment.SetEnvironmentVariable("APPINSIGHTS_ENDPOINTADDRESS", "http://localhost:1234/v2/track/");
                var config = new ConfigurationBuilder().AddEnvironmentVariables().Build();

                try
                {
                    services.AddApplicationInsightsTelemetry(config);

                    IServiceProvider serviceProvider = services.BuildServiceProvider();
                    var telemetryConfiguration       = serviceProvider.GetRequiredService <TelemetryConfiguration>();
                    Assert.Equal("http://localhost:1234/v2/track/", telemetryConfiguration.TelemetryChannel.EndpointAddress);
                }
                finally
                {
                    Environment.SetEnvironmentVariable("APPINSIGHTS_ENDPOINTADDRESS", null);
                }
            }
            public static void RegistersTelemetryConfigurationFactoryMethodThatReadsDeveloperModeFromEnvironment()
            {
                var services = ApplicationInsightsExtensionsTests.GetServiceCollectionWithContextAccessor();

                services.AddSingleton <ITelemetryChannel>(new InMemoryChannel());
                Environment.SetEnvironmentVariable("APPINSIGHTS_DEVELOPER_MODE", "true");
                var config = new ConfigurationBuilder().AddEnvironmentVariables().Build();

                try
                {
                    services.AddApplicationInsightsTelemetry(config);

                    IServiceProvider serviceProvider = services.BuildServiceProvider();
                    var telemetryConfiguration       = serviceProvider.GetRequiredService <TelemetryConfiguration>();
                    Assert.Equal(true, telemetryConfiguration.TelemetryChannel.DeveloperMode);
                }
                finally
                {
                    Environment.SetEnvironmentVariable("APPINSIGHTS_DEVELOPER_MODE", null);
                }
            }
            public static void RegistersTelemetryConfigurationFactoryMethodThatReadsInstrumentationKeyFromEnvironment()
            {
                var services = ApplicationInsightsExtensionsTests.GetServiceCollectionWithContextAccessor();

                services.AddSingleton <ITelemetryChannel>(new InMemoryChannel());
                Environment.SetEnvironmentVariable("APPINSIGHTS_INSTRUMENTATIONKEY", TestInstrumentationKey);
                var config = new ConfigurationBuilder().AddEnvironmentVariables().Build();

                try
                {
                    services.AddApplicationInsightsTelemetry(config);

                    IServiceProvider serviceProvider = services.BuildServiceProvider();
                    var telemetryConfiguration       = serviceProvider.GetRequiredService <TelemetryConfiguration>();
                    Assert.Equal(TestInstrumentationKey, telemetryConfiguration.InstrumentationKey);
                }
                finally
                {
                    Environment.SetEnvironmentVariable("APPINSIGHTS_INSTRUMENTATIONKEY", null);
                }
            }
Exemplo n.º 20
0
        public static ServiceCollection CreateServicesAndAddApplicationinsightsTelemetry(string jsonPath, string channelEndPointAddress, Action <ApplicationInsightsServiceOptions> serviceOptions = null, bool addChannel = true)
        {
            var services = ApplicationInsightsExtensionsTests.GetServiceCollectionWithContextAccessor();

            if (addChannel)
            {
                services.AddSingleton <ITelemetryChannel>(new InMemoryChannel());
            }
            IConfigurationRoot config = null;

            if (jsonPath != null)
            {
                var jsonFullPath = Path.Combine(Directory.GetCurrentDirectory(), jsonPath);
                Console.WriteLine("json:" + jsonFullPath);
                Trace.WriteLine("json:" + jsonFullPath);
                try
                {
                    config = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile(jsonFullPath).Build();
                }
                catch (Exception)
                {
                    throw new Exception("Unable to build with json:" + jsonFullPath);
                }
            }
            else if (channelEndPointAddress != null)
            {
                config = new ConfigurationBuilder().AddApplicationInsightsSettings(endpointAddress: channelEndPointAddress).Build();
            }
            else
            {
                config = new ConfigurationBuilder().Build();
            }

            services.AddApplicationInsightsTelemetry(config);
            if (serviceOptions != null)
            {
                services.Configure(serviceOptions);
            }
            return(services);
        }