public void InitializeSortsParameters()
        {
            var actionContext = new ActionContext();

            actionContext.RouteData = new RouteData();
            actionContext.RouteData.Values.Add("controller", "account");
            actionContext.RouteData.Values.Add("action", "login");
            actionContext.RouteData.Values.Add("parameterZ", "myName1");
            actionContext.RouteData.Values.Add("parameterA", "myName2");
            actionContext.RouteData.Values.Add("parameterN", "myName1");

            var contextAccessor = HttpContextAccessorHelper.CreateHttpContextAccessor(new RequestTelemetry(), actionContext);

            var telemetryListener = new DiagnosticListener(TestListenerName);

            using (var listener = CreateHostingListener(false))
            {
                telemetryListener.Subscribe(listener);
                telemetryListener.Write("Microsoft.AspNetCore.Mvc.BeforeAction",
                                        new { httpContext = contextAccessor.HttpContext, routeData = actionContext.RouteData });
            }
            var telemetry = contextAccessor.HttpContext.Features.Get <RequestTelemetry>();

            Assert.Equal("GET account/login [parameterA/parameterN/parameterZ]", telemetry.Name);
        }
        public void InitializeSortsParameters()
        {
            var actionContext = new ActionContext();

            actionContext.RouteData = new RouteData();
            actionContext.RouteData.Values.Add("controller", "account");
            actionContext.RouteData.Values.Add("action", "login");
            actionContext.RouteData.Values.Add("parameterZ", "myName1");
            actionContext.RouteData.Values.Add("parameterA", "myName2");
            actionContext.RouteData.Values.Add("parameterN", "myName1");

            var contextAccessor = HttpContextAccessorHelper.CreateHttpContextAccessor(new RequestTelemetry(), actionContext);

            var telemetryListener = new DiagnosticListener(TestListenerName);
            var initializer       = new OperationNameTelemetryInitializer(contextAccessor, telemetryListener);

            telemetryListener.Write(OperationNameTelemetryInitializer.BeforeActionNotificationName,
                                    new { httpContext = contextAccessor.HttpContext, routeData = actionContext.RouteData });

            var telemetry = new EventTelemetry();

            initializer.Initialize(telemetry);

            Assert.Equal("GET account/login [parameterA/parameterN/parameterZ]", telemetry.Context.Operation.Name);
        }
        public void InitializeDoesNotThrowIfHostNameHeaderIsNull()
        {
            var requestTelemetry = new RequestTelemetry();
            var contextAccessor  = HttpContextAccessorHelper.CreateHttpContextAccessor(requestTelemetry);

            var initializer = new AzureAppServiceRoleNameFromHostNameHeaderInitializer(contextAccessor);

            initializer.Initialize(requestTelemetry);
        }
        public void InitializeSetsRequestNameToMethodAndPath()
        {
            var telemetry       = new RequestTelemetry();
            var contextAccessor = HttpContextAccessorHelper.CreateHttpContextAccessor(telemetry, null);

            var initializer = new OperationNameTelemetryInitializer(contextAccessor, new DiagnosticListener(TestListenerName));

            initializer.Initialize(telemetry);

            Assert.Equal("GET /Test", telemetry.Name);
        }
Example #5
0
        public void InitializeDoesNotThrowIfRemoteIpAddressIsNull()
        {
            var requestTelemetry = new RequestTelemetry();
            var contextAccessor  = HttpContextAccessorHelper.CreateHttpContextAccessor(requestTelemetry);

            contextAccessor.HttpContext.Features.Set <IHttpConnectionFeature>(new HttpConnectionFeature());

            var initializer = new ClientIpHeaderTelemetryInitializer(contextAccessor);

            initializer.Initialize(requestTelemetry);
        }
        public void ContextInitializerDoesNotOverrideMachineName()
        {
            var contextAccessor  = HttpContextAccessorHelper.CreateHttpContextAccessor(new RequestTelemetry(), null);
            var source           = new DomainNameRoleInstanceTelemetryInitializer(contextAccessor);
            var requestTelemetry = new RequestTelemetry();

            requestTelemetry.Context.Cloud.RoleInstance = "Test";

            source.Initialize(requestTelemetry);

            Assert.Equal("Test", requestTelemetry.Context.Cloud.RoleInstance);
        }
Example #7
0
        public void InitializeSetsTelemetryOperationNameToMethodAndPath()
        {
            var contextAccessor = HttpContextAccessorHelper.CreateHttpContextAccessor(new RequestTelemetry(), null);

            var initializer = new OperationNameTelemetryInitializer(contextAccessor);

            var telemetry = new EventTelemetry();

            initializer.Initialize(telemetry);

            Assert.Equal("GET /Test", telemetry.Context.Operation.Name);
        }
        public void InitializeSetsUserAgentFromHeader()
        {
            var requestTelemetry = new RequestTelemetry();
            var contextAccessor  = HttpContextAccessorHelper.CreateHttpContextAccessor(requestTelemetry);

            contextAccessor.HttpContext.Request.Headers.Add("User-Agent", new[] { "test" });
            var initializer = new UserAgentTelemetryInitializer(contextAccessor);

            initializer.Initialize(requestTelemetry);

            Assert.Equal("test", requestTelemetry.Context.User.UserAgent);
        }
Example #9
0
        public void InitializeSetsTelemetryOperationIdToRequestId()
        {
            var telemetry        = new EventTelemetry();
            var requestTelemetry = new RequestTelemetry();
            var contextAccessor  = HttpContextAccessorHelper.CreateHttpContextAccessor(requestTelemetry);

            var initializer = new OperationIdTelemetryInitializer(contextAccessor);

            initializer.Initialize(telemetry);

            Assert.Equal(requestTelemetry.Id, telemetry.Context.Operation.Id);
        }
        public void InitializeSetsUserFromCookie()
        {
            var requestTelemetry = new RequestTelemetry();
            var contextAccessor  = HttpContextAccessorHelper.CreateHttpContextAccessor(requestTelemetry);

            contextAccessor.HttpContext.Request.Headers["Cookie"] = "ai_user=test";
            var initializer = new WebUserTelemetryInitializer(contextAccessor);

            initializer.Initialize(requestTelemetry);

            Assert.Equal("test", requestTelemetry.Context.User.Id);
        }
Example #11
0
        public void InitializeSetsSessionFromCookie()
        {
            var requestTelemetry = new RequestTelemetry();
            var contextAccessor  = HttpContextAccessorHelper.CreateHttpContextAccessor(requestTelemetry);

            contextAccessor.HttpContext.Request.Headers["Cookie"] = "ai_session=test|2015-04-10T17:11:38.378Z|2015-04-10T17:11:39.180Z";
            var initializer = new WebSessionTelemetryInitializer(contextAccessor);

            initializer.Initialize(requestTelemetry);

            Assert.Equal("test", requestTelemetry.Context.Session.Id);
        }
        public void InitializeDoesNotNotThrowOnMalformedAcquisitionDate()
        {
            var requestTelemetry = new RequestTelemetry();
            var contextAccessor  = HttpContextAccessorHelper.CreateHttpContextAccessor(requestTelemetry);

            contextAccessor.HttpContext.Request.Headers["Cookie"] = "ai_user=test|malformeddate";
            var initializer = new WebUserTelemetryInitializer(contextAccessor);

            initializer.Initialize(requestTelemetry);
            Assert.Equal(null, requestTelemetry.Context.User.Id);
            Assert.Equal(false, requestTelemetry.Context.User.AcquisitionDate.HasValue);
        }
Example #13
0
        public void InitializeDoesNotOverrideOperationIdProvidedInline()
        {
            var telemetry = new EventTelemetry();

            telemetry.Context.Operation.Id = "123";
            var contextAccessor = HttpContextAccessorHelper.CreateHttpContextAccessor();

            var initializer = new OperationIdTelemetryInitializer(contextAccessor);

            initializer.Initialize(telemetry);

            Assert.Equal("123", telemetry.Context.Operation.Id);
        }
Example #14
0
        public void InitializeSetsIPFromStandardHeader()
        {
            var requestTelemetry = new RequestTelemetry();
            var contextAccessor  = HttpContextAccessorHelper.CreateHttpContextAccessor(requestTelemetry);

            contextAccessor.HttpContext.Request.Headers.Add("X-Forwarded-For", new string[] { "127.0.0.3" });

            var initializer = new ClientIpHeaderTelemetryInitializer(contextAccessor);

            initializer.Initialize(requestTelemetry);

            Assert.Equal("127.0.0.3", requestTelemetry.Context.Location.Ip);
        }
        public void InitializeSetsUserFromCookie()
        {
            var requestTelemetry = new RequestTelemetry();
            var contextAccessor  = HttpContextAccessorHelper.CreateHttpContextAccessor(requestTelemetry);

            contextAccessor.HttpContext.Request.Headers["Cookie"] = "ai_user=test|2015-04-09T21:51:59.993Z";
            var initializer = new WebUserTelemetryInitializer(contextAccessor);

            initializer.Initialize(requestTelemetry);

            Assert.Equal("test", requestTelemetry.Context.User.Id);
            Assert.Equal(DateTimeOffset.Parse("2015-04-09T21:51:59.993Z", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal), requestTelemetry.Context.User.AcquisitionDate.Value);
        }
Example #16
0
        public void InitializeSetsRequestContextOperationNameToRequestName()
        {
            var telemetry = new RequestTelemetry();

            telemetry.Name = "POST /Test";

            var contextAccessor = HttpContextAccessorHelper.CreateHttpContextAccessor(telemetry);
            var initializer     = new OperationNameTelemetryInitializer(contextAccessor);

            initializer.Initialize(telemetry);

            Assert.Equal("POST /Test", telemetry.Context.Operation.Name);
        }
Example #17
0
        public void InitializeDoesNotHaveSyntheticTrafficSpecified()
        {
            var requestTelemetry = new RequestTelemetry();
            var contextAccessor  = HttpContextAccessorHelper.CreateHttpContextAccessor(requestTelemetry);

            requestTelemetry.Context.Operation.SyntheticSource = "some value";

            var initializer = new SyntheticTelemetryInitializer(contextAccessor);

            initializer.Initialize(requestTelemetry);

            Assert.Equal("some value", requestTelemetry.Context.Operation.SyntheticSource);
        }
Example #18
0
        public void InitializeDoesNotOverrideOperationNameProvidedInline()
        {
            var contextAccessor = HttpContextAccessorHelper.CreateHttpContextAccessor(new RequestTelemetry(), null);

            var initializer = new OperationNameTelemetryInitializer(contextAccessor);

            var telemetry = new EventTelemetry();

            telemetry.Context.Operation.Name = "Name";
            initializer.Initialize(telemetry);

            Assert.Equal("Name", telemetry.Context.Operation.Name);
        }
        public void InitializeSetsRoleNameFromHostNameWithAzureWebsites()
        {
            var requestTelemetry = new RequestTelemetry();
            var contextAccessor  = HttpContextAccessorHelper.CreateHttpContextAccessor(requestTelemetry);

            contextAccessor.HttpContext.Request.Headers.Add("WAS-DEFAULT-HOSTNAME", new string[] { "appserviceslottest-ppe.azurewebsites.net" });

            var initializer = new AzureAppServiceRoleNameFromHostNameHeaderInitializer(contextAccessor);

            initializer.Initialize(requestTelemetry);

            Assert.Equal("appserviceslottest-ppe", requestTelemetry.Context.Cloud.RoleName);
        }
        public void InitializeDoesNotOverrideUserProvidedInline()
        {
            var requestTelemetry = new RequestTelemetry();

            requestTelemetry.Context.User.Id = "Inline";
            var contextAccessor = HttpContextAccessorHelper.CreateHttpContextAccessor(requestTelemetry);

            contextAccessor.HttpContext.Request.Headers["Cookie"] = "ai_user=test|2015-04-09T21:51:59.993Z";
            var initializer = new WebUserTelemetryInitializer(contextAccessor);

            initializer.Initialize(requestTelemetry);

            Assert.Equal("Inline", requestTelemetry.Context.User.Id);
        }
        public void InitializeDoesNotOverrideUserAgentProvidedInline()
        {
            var requestTelemetry = new RequestTelemetry();

            requestTelemetry.Context.User.UserAgent = "Inline";
            var contextAccessor = HttpContextAccessorHelper.CreateHttpContextAccessor(requestTelemetry);

            contextAccessor.HttpContext.Request.Headers.Add("User-Agent", new[] { "test" });
            var initializer = new UserAgentTelemetryInitializer(contextAccessor);

            initializer.Initialize(requestTelemetry);

            Assert.Equal("Inline", requestTelemetry.Context.User.UserAgent);
        }
Example #22
0
        public void InitializeRequestDoesHaveRunIdHeaderButNoLocationHeader()
        {
            var requestTelemetry = new RequestTelemetry();
            var contextAccessor  = HttpContextAccessorHelper.CreateHttpContextAccessor(requestTelemetry);

            requestTelemetry.Context.Operation.SyntheticSource = null;
            contextAccessor.HttpContext.Request.Headers.Remove("SyntheticTest-Location");
            contextAccessor.HttpContext.Request.Headers.Add("SyntheticTest-RunId", new string[] { "runId" });

            var initializer = new SyntheticTelemetryInitializer(contextAccessor);

            initializer.Initialize(requestTelemetry);

            Assert.Null(requestTelemetry.Context.Operation.SyntheticSource);
        }
Example #23
0
        public void InitializeSetSyntheticTrafficToDesiredValue()
        {
            var requestTelemetry = new RequestTelemetry();
            var contextAccessor  = HttpContextAccessorHelper.CreateHttpContextAccessor(requestTelemetry);

            requestTelemetry.Context.Operation.SyntheticSource = null;
            contextAccessor.HttpContext.Request.Headers.Add("SyntheticTest-Location", new string[] { "location" });
            contextAccessor.HttpContext.Request.Headers.Add("SyntheticTest-RunId", new string[] { "runId" });

            var initializer = new SyntheticTelemetryInitializer(contextAccessor);

            initializer.Initialize(requestTelemetry);

            Assert.Equal("Application Insights Availability Monitoring", requestTelemetry.Context.Operation.SyntheticSource);
        }
Example #24
0
        public void InitializeSetsIPFromCustomHeader()
        {
            var requestTelemetry = new RequestTelemetry();
            var contextAccessor  = HttpContextAccessorHelper.CreateHttpContextAccessor(requestTelemetry);

            contextAccessor.HttpContext.Request.Headers.Add("HEADER", new string[] { "127.0.0.3;127.0.0.4" });

            var initializer = new ClientIpHeaderTelemetryInitializer(contextAccessor);

            initializer.HeaderNames.Add("HEADER");
            initializer.HeaderValueSeparators = ",;";

            initializer.Initialize(requestTelemetry);

            Assert.Equal("127.0.0.3", requestTelemetry.Context.Location.Ip);
        }
        public void InitializeDoesNotOverrideRoleName()
        {
            var requestTelemetry = new RequestTelemetry();

            requestTelemetry.Context.Cloud.RoleName = "ExistingRoleName";

            var contextAccessor = HttpContextAccessorHelper.CreateHttpContextAccessor(requestTelemetry);

            contextAccessor.HttpContext.Request.Headers.Add("WAS-DEFAULT-HOSTNAME", new string[] { "MyAppServiceProd" });

            var initializer = new AzureAppServiceRoleNameFromHostNameHeaderInitializer(contextAccessor);

            initializer.Initialize(requestTelemetry);

            Assert.Equal("ExistingRoleName", requestTelemetry.Context.Cloud.RoleName);
        }
Example #26
0
        public void InitializeDoesNotOverrideIPProvidedInline()
        {
            var requestTelemetry = new RequestTelemetry();

            requestTelemetry.Context.Location.Ip = "127.0.0.4";

            var contextAccessor = HttpContextAccessorHelper.CreateHttpContextAccessor(requestTelemetry);

            contextAccessor.HttpContext.Request.Headers.Add("X-Forwarded-For", new string[] { "127.0.0.3" });

            var initializer = new ClientIpHeaderTelemetryInitializer(contextAccessor);

            initializer.Initialize(requestTelemetry);

            Assert.Equal("127.0.0.4", requestTelemetry.Context.Location.Ip);
        }
        public void InitializeSavesRoleNameIntoRequestFromHostNameHeader()
        {
            var requestTelemetry = new RequestTelemetry();
            var contextAccessor  = HttpContextAccessorHelper.CreateHttpContextAccessor(requestTelemetry);

            contextAccessor.HttpContext.Request.Headers.Add("WAS-DEFAULT-HOSTNAME", new string[] { "MyAppServiceProd" });

            var initializer = new AzureAppServiceRoleNameFromHostNameHeaderInitializer(contextAccessor);

            var evt = new EventTelemetry();

            initializer.Initialize(evt);

            Assert.Equal("MyAppServiceProd", evt.Context.Cloud.RoleName);
            Assert.Equal("MyAppServiceProd", requestTelemetry.Context.Cloud.RoleName);
        }
Example #28
0
        public void AzureWebAppRoleEnvironmentTelemetryInitializerEmptyVariable()
        {
            Environment.SetEnvironmentVariable("WEBSITE_SITE_NAME", null);
            Environment.SetEnvironmentVariable("WEBSITE_HOSTNAME", null);

            var telemetryItem = new EventTelemetry();

            var context     = HttpContextAccessorHelper.CreateHttpContextAccessor(new RequestTelemetry(), null);
            var initializer = new AzureWebAppRoleEnvironmentTelemetryInitializer(context);

            initializer.Initialize(telemetryItem);

            Assert.Null(telemetryItem.Context.Cloud.RoleName);
            Assert.Null(telemetryItem.Context.Cloud.RoleInstance);
            Assert.Null(telemetryItem.Context.GetInternalContext().NodeName);
        }
Example #29
0
        public void InitializeRequestValidateLocationAndUserAreSetCorrectly()
        {
            var requestTelemetry = new RequestTelemetry();
            var contextAccessor  = HttpContextAccessorHelper.CreateHttpContextAccessor(requestTelemetry);
            var desiredLocation  = "location_runId";

            requestTelemetry.Context.Operation.SyntheticSource = null;
            contextAccessor.HttpContext.Request.Headers.Add("SyntheticTest-Location", new string[] { "location" });
            contextAccessor.HttpContext.Request.Headers.Add("SyntheticTest-RunId", new string[] { "runId" });

            var initializer = new SyntheticTelemetryInitializer(contextAccessor);

            initializer.Initialize(requestTelemetry);

            Assert.Equal(desiredLocation, requestTelemetry.Context.User.Id);
            Assert.Equal("runId", requestTelemetry.Context.Session.Id);
        }
        public void InitializeSetsRoleNameFromRequestTelemetryIfPresent()
        {
            var requestTelemetry = new RequestTelemetry();

            requestTelemetry.Context.Cloud.RoleName = "RoleNameOnRequest";
            var contextAccessor = HttpContextAccessorHelper.CreateHttpContextAccessor(requestTelemetry);

            contextAccessor.HttpContext.Request.Headers.Add("WAS-DEFAULT-HOSTNAME", new string[] { "RoleNameOnHeader" });

            var initializer = new AzureAppServiceRoleNameFromHostNameHeaderInitializer(contextAccessor);

            var evt = new EventTelemetry();

            initializer.Initialize(evt);

            Assert.Equal("RoleNameOnRequest", evt.Context.Cloud.RoleName);
        }