public void TestMixedTelemetryItemsReceived() { using (var server = new InProcessServer(assemblyName)) { var httpClient = new HttpClient(); var task = httpClient.GetAsync(server.BaseHost + "/Mixed"); task.Wait(TestTimeoutMs); var request = server.BackChannel.Buffer.OfType<RequestTelemetry>().Single(); var eventTelemetry = server.BackChannel.Buffer.OfType<EventTelemetry>().Single(); var metricTelemetry = server.BackChannel.Buffer.OfType<MetricTelemetry>().Single(); var traceTelemetry = server.BackChannel.Buffer.OfType<TraceTelemetry>().Single(); #if NET451 var dependencyTelemetry = server.BackChannel.Buffer.OfType<DependencyTelemetry>().Single(); Assert.NotNull(dependencyTelemetry); #endif Assert.True(server.BackChannel.Buffer.Count >= 4); Assert.NotNull(request); Assert.NotNull(eventTelemetry); Assert.NotNull(metricTelemetry); Assert.NotNull(traceTelemetry); } }
public void TestBasicExceptionPropertiesAfterRequestingControllerThatThrows() { using (var server = new InProcessServer(assemblyName)) { var expectedExceptionTelemetry = new ExceptionTelemetry(); expectedExceptionTelemetry.HandledAt = ExceptionHandledAt.Platform; expectedExceptionTelemetry.Exception = new InvalidOperationException(); this.ValidateBasicException(server, "/api/exception", expectedExceptionTelemetry); } }
public void TestBasicDependencyPropertiesAfterRequestingBasicPage() { using (var server = new InProcessServer(assemblyName)) { const string RequestPath = "/api/values"; var expectedDependencyTelemetry = new DependencyTelemetry(); expectedDependencyTelemetry.Name = server.BaseHost + RequestPath; expectedDependencyTelemetry.ResultCode = "200"; expectedDependencyTelemetry.Success = true; this.ValidateBasicDependency(server, RequestPath, expectedDependencyTelemetry); } }
public void ValidateBasicException(InProcessServer server, string requestPath, ExceptionTelemetry expected) { DateTimeOffset testStart = DateTimeOffset.Now; var httpClient = new HttpClient(); var task = httpClient.GetAsync(server.BaseHost + requestPath); task.Wait(TestTimeoutMs); var result = task.Result; var actual = server.BackChannel.Buffer.OfType<ExceptionTelemetry>().Single(); Assert.Equal(expected.Exception.GetType(), actual.Exception.GetType()); Assert.NotEmpty(actual.Exception.StackTrace); Assert.Equal(actual.HandledAt, actual.HandledAt); Assert.NotEmpty(actual.Context.Operation.Name); Assert.NotEmpty(actual.Context.Operation.Id); }
public void TestBasicRequestPropertiesAfterRequestingWebApiShimRoute() { using (var server = new InProcessServer(assemblyName)) { const string RequestPath = "/api/values/1"; var expectedRequestTelemetry = new RequestTelemetry(); expectedRequestTelemetry.HttpMethod = "GET"; expectedRequestTelemetry.Name = "GET Values/Get [id]"; expectedRequestTelemetry.ResponseCode = "200"; expectedRequestTelemetry.Success = true; expectedRequestTelemetry.Url = new System.Uri(server.BaseHost + RequestPath); this.ValidateBasicRequest(server, RequestPath, expectedRequestTelemetry); } }
public void TestBasicRequestPropertiesAfterRequestingControllerThatThrows() { using (var server = new InProcessServer(assemblyName)) { const string RequestPath = "/Home/Exception"; var expectedRequestTelemetry = new RequestTelemetry(); expectedRequestTelemetry.HttpMethod = "GET"; expectedRequestTelemetry.Name = "GET Home/Exception"; expectedRequestTelemetry.ResponseCode = "500"; expectedRequestTelemetry.Success = false; expectedRequestTelemetry.Url = new System.Uri(server.BaseHost + RequestPath); this.ValidateBasicRequest(server, "/Home/Exception", expectedRequestTelemetry); } }
public void TestIfPerformanceCountersAreCollected() { using (var server = new InProcessServer(assemblyName)) { const string RequestPath = "/"; var httpClient = new HttpClient(); var task = httpClient.GetAsync(server.BaseHost + RequestPath); task.Wait(TestTimeoutMs); var result = task.Result; Thread.Sleep(70000); var actual = server.BackChannel.Buffer.OfType<PerformanceCounterTelemetry>().Distinct(); Assert.True(actual.Count() > 0); } }
public void ValidateBasicDependency(InProcessServer server, string requestPath, DependencyTelemetry expected) { DateTimeOffset testStart = DateTimeOffset.Now; var timer = Stopwatch.StartNew(); var httpClient = new HttpClient(); var task = httpClient.GetAsync(server.BaseHost + requestPath); task.Wait(TestTimeoutMs); var result = task.Result; var actual = server.BackChannel.Buffer.OfType<DependencyTelemetry>().Single(); timer.Stop(); Assert.Equal(expected.Name, actual.Name); Assert.Equal(expected.Success, actual.Success); Assert.Equal(expected.ResultCode, actual.ResultCode); }
public void TestBasicRequestPropertiesAfterRequestingActionWithParameter() { using (var server = new InProcessServer(assemblyName)) { const string RequestPath = "/Home/About/5"; var expectedRequestTelemetry = new RequestTelemetry(); expectedRequestTelemetry.HttpMethod = "GET"; expectedRequestTelemetry.Name = "GET Home/About [id]"; expectedRequestTelemetry.ResponseCode = "200"; expectedRequestTelemetry.Success = true; expectedRequestTelemetry.Url = new System.Uri(server.BaseHost + RequestPath); this.ValidateBasicRequest(server, RequestPath, expectedRequestTelemetry); } }
public void TestBasicRequestPropertiesAfterRequestingNotExistingController() { using (var server = new InProcessServer(assemblyName)) { const string RequestPath = "/api/notexistingcontroller"; var expectedRequestTelemetry = new RequestTelemetry(); expectedRequestTelemetry.HttpMethod = "GET"; expectedRequestTelemetry.Name = "GET /api/notexistingcontroller"; expectedRequestTelemetry.ResponseCode = "404"; expectedRequestTelemetry.Success = false; expectedRequestTelemetry.Url = new System.Uri(server.BaseHost + RequestPath); this.ValidateBasicRequest(server, RequestPath, expectedRequestTelemetry); } }
public void TestBasicRequestPropertiesAfterRequestingControllerThatThrows() { using (var server = new InProcessServer(assemblyName)) { const string RequestPath = "/Home/Exception"; var expectedRequestTelemetry = new RequestTelemetry(); expectedRequestTelemetry.HttpMethod = "GET"; // Request name is tracked incorretly in case of errors right now, tracked by https://github.com/Microsoft/ApplicationInsights-aspnet5/issues/91 expectedRequestTelemetry.Name = "GET Home/Error"; expectedRequestTelemetry.ResponseCode = "500"; expectedRequestTelemetry.Success = false; expectedRequestTelemetry.Url = new System.Uri(server.BaseHost + RequestPath); this.ValidateBasicRequest(server, "/Home/Exception", expectedRequestTelemetry); } }
public void TestBasicRequestPropertiesAfterRequestingControllerThatThrows() { using (var server = new InProcessServer(assemblyName)) { const string RequestPath = "/api/exception"; var expectedRequestTelemetry = new RequestTelemetry(); expectedRequestTelemetry.HttpMethod = "GET"; expectedRequestTelemetry.Name = "GET Exception/Get"; //TODO: default template of Web API applicaiton doesn't have error handling middleware //that will set appropriate status code expectedRequestTelemetry.ResponseCode = "200"; expectedRequestTelemetry.Success = false; expectedRequestTelemetry.Url = new System.Uri(server.BaseHost + RequestPath); this.ValidateBasicRequest(server, RequestPath, expectedRequestTelemetry); } }
public void ValidateBasicRequest(InProcessServer server, string requestPath, RequestTelemetry expected) { DateTimeOffset testStart = DateTimeOffset.Now; var timer = Stopwatch.StartNew(); var httpClient = new HttpClient(); var task = httpClient.GetAsync(server.BaseHost + requestPath); task.Wait(TestTimeoutMs); var result = task.Result; var actual = server.BackChannel.Buffer.OfType<RequestTelemetry>().Single(); timer.Stop(); Assert.Equal(expected.ResponseCode, actual.ResponseCode); Assert.Equal(expected.Name, actual.Name); Assert.Equal(expected.Success, actual.Success); Assert.Equal(expected.Url, actual.Url); Assert.InRange<DateTimeOffset>(actual.Timestamp, testStart, DateTimeOffset.Now); Assert.True(actual.Duration < timer.Elapsed, "duration"); Assert.Equal(expected.HttpMethod, actual.HttpMethod); }
public void TestMixedTelemetryItemsReceived() { using (var server = new InProcessServer(assemblyName)) { var httpClient = new HttpClient(); var task = httpClient.GetAsync(server.BaseHost + "/Home/Contact"); task.Wait(TestTimeoutMs); var request = server.BackChannel.Buffer.OfType<RequestTelemetry>().Single(); var eventTelemetry = server.BackChannel.Buffer.OfType<EventTelemetry>().Single(); var metricTelemetry = server.BackChannel.Buffer.OfType<MetricTelemetry>().Single(); var traceTelemetry = server.BackChannel.Buffer.OfType<TraceTelemetry>().Single(); Assert.Equal(4, server.BackChannel.Buffer.Count); Assert.NotNull(request); Assert.NotNull(eventTelemetry); Assert.NotNull(metricTelemetry); Assert.NotNull(traceTelemetry); } }
public void ValidatePerformanceCountersAreCollected(string assemblyName, Func <IWebHostBuilder, IWebHostBuilder> configureHost = null) { using (var server = new InProcessServer(assemblyName, configureHost)) { // Reconfigure the PerformanceCollectorModule timer. Type perfModuleType = typeof(PerformanceCollectorModule); PerformanceCollectorModule perfModule = (PerformanceCollectorModule)server.ApplicationServices.GetServices <ITelemetryModule>().FirstOrDefault(m => m.GetType() == perfModuleType); FieldInfo timerField = perfModuleType.GetField("timer", BindingFlags.NonPublic | BindingFlags.Instance); var timer = timerField.GetValue(perfModule); timerField.FieldType.InvokeMember("ScheduleNextTick", BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance, null, timer, new object[] { TimeSpan.FromMilliseconds(10) }); DateTime timeout = DateTime.UtcNow.AddMilliseconds(TestTimeoutMs); int numberOfCountersSent = 0; do { Thread.Sleep(1000); numberOfCountersSent += server.BackChannel.Buffer.OfType <MetricTelemetry>().Distinct().Count(); } while (numberOfCountersSent == 0 && DateTime.UtcNow < timeout); Assert.True(numberOfCountersSent > 0); } }
public void ValidateBasicException(InProcessServer server, string requestPath, ExceptionTelemetry expected) { var httpClientHandler = new HttpClientHandler(); httpClientHandler.UseDefaultCredentials = true; Task <HttpResponseMessage> task; using (var httpClient = new HttpClient(httpClientHandler, true)) { task = httpClient.GetAsync(server.BaseHost + requestPath); task.Wait(TestTimeoutMs); } var result = task.Result; server.Dispose(); var actual = server.BackChannel.Buffer.OfType <ExceptionTelemetry>().Single(); Assert.Equal(expected.Exception.GetType(), actual.Exception.GetType()); Assert.NotEmpty(actual.Exception.StackTrace); Assert.NotEmpty(actual.Context.Operation.Name); Assert.NotEmpty(actual.Context.Operation.Id); }
public TelemetryItem <RequestData> ValidateRequestWithHeaders(InProcessServer server, string requestPath, Dictionary <string, string> requestHeaders, RequestTelemetry expected, bool expectRequestContextInResponse = true) { // Subtract 50 milliseconds to hack around strange behavior on build server where the RequestTelemetry.Timestamp is somehow sometimes earlier than now by a few milliseconds. expected.Timestamp = DateTimeOffset.Now.Subtract(TimeSpan.FromMilliseconds(50)); Stopwatch timer = Stopwatch.StartNew(); var response = this.ExecuteRequest(server.BaseHost + requestPath, requestHeaders); var actual = server.Listener.ReceiveItemsOfType <TelemetryItem <RequestData> >(1, TestListenerTimeoutInMs); timer.Stop(); this.DebugTelemetryItems(actual); this.output.WriteLine("Response headers: " + string.Join(",", response.Headers.Select(kvp => $"{kvp.Key} = {kvp.Value.First()}"))); var item = actual.OfType <TelemetryItem <RequestData> >().FirstOrDefault(); Assert.NotNull(item); var data = ((TelemetryItem <RequestData>)item).data.baseData; Assert.Equal(expected.ResponseCode, data.responseCode); Assert.Equal(expected.Name, data.name); Assert.Equal(expected.Success, data.success); Assert.Equal(expected.Url, new Uri(data.url)); Assert.Equal(expectRequestContextInResponse, response.Headers.Contains("Request-Context")); if (expectRequestContextInResponse) { Assert.True(response.Headers.TryGetValues("Request-Context", out var appIds)); Assert.Equal($"appId={InProcessServer.AppId}", appIds.Single()); } output.WriteLine("actual.Duration: " + data.duration); output.WriteLine("timer.Elapsed: " + timer.Elapsed); Assert.True(TimeSpan.Parse(data.duration) < timer.Elapsed.Add(TimeSpan.FromMilliseconds(20)), "duration"); return(item); }
public void ValidateBasicDependency(string assemblyName, string requestPath) { using (InProcessServer server = new InProcessServer(assemblyName)) { DependencyTelemetry expected = new DependencyTelemetry(); expected.Name = server.BaseHost + requestPath; expected.ResultCode = "200"; expected.Success = true; DateTimeOffset testStart = DateTimeOffset.Now; var timer = Stopwatch.StartNew(); var httpClient = new HttpClient(); var task = httpClient.GetAsync(server.BaseHost + requestPath); task.Wait(TestTimeoutMs); var result = task.Result; var actual = server.BackChannel.Buffer.OfType <DependencyTelemetry>().Single(); timer.Stop(); Assert.Equal(expected.Name, actual.Name); Assert.Equal(expected.Success, actual.Success); Assert.Equal(expected.ResultCode, actual.ResultCode); } }
public void ValidatePerformanceCountersAreCollected(string assemblyName) { using (var server = new InProcessServer(assemblyName)) { // Reconfigure the PerformanceCollectorModule timer. Type perfModuleType = typeof(PerformanceCollectorModule); PerformanceCollectorModule perfModule = (PerformanceCollectorModule)server.ApplicationServices.GetServices<ITelemetryModule>().FirstOrDefault(m => m.GetType() == perfModuleType); FieldInfo timerField = perfModuleType.GetField("timer", BindingFlags.NonPublic | BindingFlags.Instance); var timer = timerField.GetValue(perfModule); timerField.FieldType.InvokeMember("ScheduleNextTick", BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance, null, timer, new object[] { TimeSpan.FromMilliseconds(10) }); DateTime timeout = DateTime.Now.AddSeconds(10); int numberOfCountersSent = 0; do { Thread.Sleep(1000); numberOfCountersSent = server.BackChannel.Buffer.OfType<PerformanceCounterTelemetry>().Distinct().Count(); } while (numberOfCountersSent == 0 && DateTime.Now < timeout); Assert.True(numberOfCountersSent > 0); } }
public TelemetryItem <RequestData> ValidateBasicRequest(InProcessServer server, string requestPath, RequestTelemetry expected, bool expectRequestContextInResponse = true) { return(ValidateRequestWithHeaders(server, requestPath, null, expected, expectRequestContextInResponse)); }
public (TelemetryItem <RequestData>, TelemetryItem <RemoteDependencyData>) ValidateBasicDependency(InProcessServer server, string requestPath, DependencyTelemetry expected) { var response = this.ExecuteRequest(server.BaseHost + requestPath); var actual = server.Listener.ReceiveItems(TestListenerTimeoutInMs); this.DebugTelemetryItems(actual); var dependencyTelemetry = actual.OfType <TelemetryItem <RemoteDependencyData> >().FirstOrDefault(); Assert.NotNull(dependencyTelemetry); var dependencyData = ((TelemetryItem <RemoteDependencyData>)dependencyTelemetry).data.baseData; Assert.Equal(expected.Data, dependencyData.data); Assert.Equal(expected.ResultCode, dependencyData.resultCode); Assert.Equal(expected.Success, dependencyData.success); var requestTelemetry = actual.OfType <TelemetryItem <RequestData> >().FirstOrDefault(); Assert.NotNull(requestTelemetry); Assert.Equal(requestTelemetry.tags["ai.operation.id"], dependencyTelemetry.tags["ai.operation.id"]); Assert.Contains(dependencyTelemetry.data.baseData.id, requestTelemetry.tags["ai.operation.parentId"]); return(requestTelemetry, dependencyTelemetry); }