A telemetry initializer that will gather Azure Role Environment context information.
Inheritance: ITelemetryInitializer
        public void AzureRoleEnvironmentTelemetryInitializerDoesNotOverrideRoleName()
        {
            var telemetryItem = new EventTelemetry();

            AzureRoleEnvironmentContextReader.Instance = null;
            AzureRoleEnvironmentTelemetryInitializer initializer = new AzureRoleEnvironmentTelemetryInitializer();

            telemetryItem.Context.Cloud.RoleName = "Test";
            initializer.Initialize(telemetryItem);

            Assert.Equal("Test", telemetryItem.Context.Cloud.RoleName);
        }
        public void AzureRoleEnvironmentTelemetryInitializerDoesNotOverrideRoleInstance()
        {
            var telemetryItem = new EventTelemetry();
            AzureRoleEnvironmentTelemetryInitializer initializer = new AzureRoleEnvironmentTelemetryInitializer();
            AzureRoleEnvironmentContextReader.BaseDirectory = ServiceRuntimeHelper.TestWithServiceRuntimePath;
            AzureRoleEnvironmentContextReader.Instance = null;
            ServiceRuntimeHelper.IsAvailable = true;

            telemetryItem.Context.Cloud.RoleInstance = "Test";
            initializer.Initialize(telemetryItem);

            Assert.Equal("Test", telemetryItem.Context.Cloud.RoleInstance);
        }
        public void AzureRoleEnvironmentTelemetryInitializerSetsTelemetryContextPropertiesWhenRoleEnvironmentIsNotAvailable()
        {
            var telemetryItem = new EventTelemetry();
            AzureRoleEnvironmentTelemetryInitializer initializer = new AzureRoleEnvironmentTelemetryInitializer();
            AzureRoleEnvironmentContextReader.BaseDirectory = ServiceRuntimeHelper.TestWithServiceRuntimePath;
            AzureRoleEnvironmentContextReader.Instance = null;

            ServiceRuntimeHelper.IsAvailable = false;
            initializer.Initialize(telemetryItem);
            ServiceRuntimeHelper.IsAvailable = true;

            Assert.Null(telemetryItem.Context.Cloud.RoleName);
            Assert.Null(telemetryItem.Context.Cloud.RoleInstance);
        }
Beispiel #4
0
        public void AzureRoleEnvironmentTelemetryInitializerDoesNotOverrideRoleInstance()
        {
            var telemetryItem = new EventTelemetry();
            AzureRoleEnvironmentTelemetryInitializer initializer = new AzureRoleEnvironmentTelemetryInitializer();

            AzureRoleEnvironmentContextReader.BaseDirectory = ServiceRuntimeHelper.TestWithServiceRuntimePath;
            AzureRoleEnvironmentContextReader.Instance      = null;
            ServiceRuntimeHelper.IsAvailable = true;

            telemetryItem.Context.Cloud.RoleInstance = "Test";
            initializer.Initialize(telemetryItem);

            Assert.Equal("Test", telemetryItem.Context.Cloud.RoleInstance);
        }
Beispiel #5
0
        public void AzureRoleEnvironmentTelemetryInitializerSetsTelemetryContextPropertiesWhenRoleEnvironmentIsNotAvailable()
        {
            var telemetryItem = new EventTelemetry();
            AzureRoleEnvironmentTelemetryInitializer initializer = new AzureRoleEnvironmentTelemetryInitializer();

            AzureRoleEnvironmentContextReader.BaseDirectory = ServiceRuntimeHelper.TestWithServiceRuntimePath;
            AzureRoleEnvironmentContextReader.Instance      = null;

            ServiceRuntimeHelper.IsAvailable = false;
            initializer.Initialize(telemetryItem);
            ServiceRuntimeHelper.IsAvailable = true;

            Assert.Null(telemetryItem.Context.Cloud.RoleName);
            Assert.Null(telemetryItem.Context.Cloud.RoleInstance);
        }
Beispiel #6
0
        public void AzureRoleEnvironmentTelemetryInitializerSetsTelemetryContextPropertiesToNullWhenNotRunningInsideAzureCloudService()
        {
            // This test asssumes that it is not running inside a cloud service.
            // Its Ok even if Azure ServiceRunTime dlls are in the GAC, as IsAvailable() will return false, and hence
            // no context will be further attempted to be read.
            var telemetryItem = new EventTelemetry();

            AzureRoleEnvironmentContextReader.Instance           = null;
            AzureRoleEnvironmentContextReader.AssemblyLoaderType = null;
            AzureRoleEnvironmentTelemetryInitializer initializer = new AzureRoleEnvironmentTelemetryInitializer();

            initializer.Initialize(telemetryItem);

            Assert.Null(telemetryItem.Context.Cloud.RoleName);
            Assert.Null(telemetryItem.Context.Cloud.RoleInstance);
            Assert.Null(telemetryItem.Context.GetInternalContext().NodeName);
        }
        public void AzureRoleEnvironmentTelemetryInitializerSetsTelemetryContextPropertiesWhenRoleEnvironmentIsAvailable()
        {
            var telemetryItem = new EventTelemetry();
            var initializer = new AzureRoleEnvironmentTelemetryInitializer();
            AzureRoleEnvironmentContextReader.BaseDirectory = ServiceRuntimeHelper.TestWithServiceRuntimePath;
            AzureRoleEnvironmentContextReader.Instance = null;

            ServiceRuntimeHelper.IsAvailable = true;
            initializer.Initialize(telemetryItem);

            string expectedRoleInstanceName = string.Format(
                                    CultureInfo.InvariantCulture,
                                    TestRoleInstance.IdFormat,
                                    ServiceRuntimeHelper.RoleName,
                                    ServiceRuntimeHelper.RoleInstanceOrdinal);

            Assert.Equal(ServiceRuntimeHelper.RoleName, telemetryItem.Context.Cloud.RoleName);
            Assert.Equal(expectedRoleInstanceName, telemetryItem.Context.Cloud.RoleInstance);
        }
Beispiel #8
0
        public void AzureRoleEnvironmentTelemetryInitializerSetsTelemetryContextPropertiesWhenRoleEnvironmentIsAvailable()
        {
            var telemetryItem = new EventTelemetry();
            var initializer   = new AzureRoleEnvironmentTelemetryInitializer();

            AzureRoleEnvironmentContextReader.BaseDirectory = ServiceRuntimeHelper.TestWithServiceRuntimePath;
            AzureRoleEnvironmentContextReader.Instance      = null;

            ServiceRuntimeHelper.IsAvailable = true;
            initializer.Initialize(telemetryItem);

            string expectedRoleInstanceName = string.Format(
                CultureInfo.InvariantCulture,
                TestRoleInstance.IdFormat,
                ServiceRuntimeHelper.RoleName,
                ServiceRuntimeHelper.RoleInstanceOrdinal);

            Assert.Equal(ServiceRuntimeHelper.RoleName, telemetryItem.Context.Cloud.RoleName);
            Assert.Equal(expectedRoleInstanceName, telemetryItem.Context.Cloud.RoleInstance);
        }
Beispiel #9
0
        public void AzureRoleEnvironmentTelemetryInitializerDoNotPopulateContextIfRunningAzureWebApp()
        {
            try
            {
                // Set the ENV variable so as to trick app is running as Azure WebApp
                Environment.SetEnvironmentVariable("WEBSITE_SITE_NAME", "TestRoleName.AzureWebSites.net");

                // Initialize telemetry using AzureRoleEnvironmentTelemetryInitializer
                var telemetryItem = new EventTelemetry();
                AzureRoleEnvironmentTelemetryInitializer initializer = new AzureRoleEnvironmentTelemetryInitializer();
                initializer.Initialize(telemetryItem);

                // As app is running as Azure WebApp, AzureRoleEnvironmentTelemetryInitializer will not populate any context.
                Assert.Null(telemetryItem.Context.Cloud.RoleName);
                Assert.Null(telemetryItem.Context.Cloud.RoleInstance);
                Assert.Null(telemetryItem.Context.GetInternalContext().NodeName);
            }
            finally
            {
                Environment.SetEnvironmentVariable("WEBSITE_SITE_NAME", null);
            }
        }
        public void AzureRoleEnvironmentTelemetryInitializerLoadDllToSeparateAppDomain()
        {
            // A random dll which is not already loaded to the current AppDomain but dropped into bin folder.
            string dllPath = Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, "Microsoft.ApplicationInsights.Log4NetAppender.dll");

            Assert.True(File.Exists(dllPath));

            try
            {
                // Publish the dll to GAC to give a chance for  AzureRoleEnvironmentTelemetryInitializer to load it to a new AppDomain
                new System.EnterpriseServices.Internal.Publish().GacInstall(dllPath);

                // Validate that the dll is not loaded to test AppDomaion to begin with.
                var retrievedAssembly = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(item => string.Equals(item.GetName().Name, "Microsoft.ApplicationInsights.Log4NetAppender", StringComparison.OrdinalIgnoreCase));
                Assert.Null(retrievedAssembly);

                // TestAssemblyLoader will load a random assembly (Microsoft.ApplicationInsights.Log4NetAppender.dll) and populate TestRoleName, TestRoleInstanceId into the fields.
                AzureRoleEnvironmentContextReader.AssemblyLoaderType = typeof(TestAzureServiceRuntimeAssemblyLoader);
                AzureRoleEnvironmentContextReader.Instance           = null;

                // Create initializer - this will internally create separate appdomain and load assembly into it.
                AzureRoleEnvironmentTelemetryInitializer initializer = new AzureRoleEnvironmentTelemetryInitializer();

                // Validate that the dll is still not loaded to current appdomain.
                retrievedAssembly = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(item => string.Equals(item.GetName().Name, "Microsoft.ApplicationInsights.Log4NetAppender", StringComparison.OrdinalIgnoreCase));
                Assert.Null(retrievedAssembly);

                // Validate that initializer has populated expected context properties. (set by TestAssemblyLoader)
                var telemetryItem = new EventTelemetry();
                initializer.Initialize(telemetryItem);
                Assert.Equal("TestRoleName", telemetryItem.Context.Cloud.RoleName);
                Assert.Equal("TestRoleInstanceId", telemetryItem.Context.Cloud.RoleInstance);
            }
            finally
            {
                new System.EnterpriseServices.Internal.Publish().GacRemove(dllPath);
            }
        }