public TestRunner(ILogger log, AvailabilityTest test) { this.log = log; //HttpClient setup this.client = new HttpClient { Timeout = TimeSpan.FromSeconds(30) }; this.client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0; MinionBot)"); //Telemetry client setup this.telemetryConfiguration = new TelemetryConfiguration(); this.telemetryClient = new TelemetryClient(telemetryConfiguration); }
/// <summary> /// Executes web tests for the Config /// </summary> /// <param name="log"></param> /// <param name="test"></param> /// <returns></returns> public async Task RunAvailabilityTest(ILogger log, AvailabilityTest test) { // If your resource is in a region like Azure Government or Azure China, change the endpoint address accordingly. // Visit https://docs.microsoft.com/azure/azure-monitor/app/custom-endpoints#regions-that-require-endpoint-modification for more details. string EndpointAddress = "https://dc.services.visualstudio.com/v2/track"; telemetryConfiguration.InstrumentationKey = test.APPINSIGHTS_INSTRUMENTATIONKEY; telemetryConfiguration.TelemetryChannel = new InMemoryChannel { EndpointAddress = EndpointAddress }; foreach (EndPoint e in test.Endpoints) { await RunAvailbiltyTestAsync(log, client, test, e, telemetryClient).ConfigureAwait(false); } client = null; telemetryConfiguration = null; telemetryClient = null; log.LogInformation($"Completed tests for {test.ApplicationName}"); }
/// <summary> /// Executes an availability test for a Url /// </summary> /// <param name="log"></param> /// <param name="testName"></param> /// <param name="uri"></param> /// <returns></returns> private async static Task RunAvailbiltyTestAsync(ILogger log, HttpClient client, AvailabilityTest test, EndPoint endpoint, TelemetryClient telemetryClient) { if (null == test) { Exception ex = new ArgumentOutOfRangeException("test", "The 'test' object is null."); log.LogError($"Invalid Test Settings : {ex.Message}"); throw ex; } if (null == telemetryClient) { Exception ex = new ArgumentOutOfRangeException("telemetryClient", "The 'telemetryClient' object is null."); log.LogError($"Invalid Test Settings : {ex.Message}"); throw ex; } if (null == endpoint) { Exception ex = new ArgumentOutOfRangeException("telemetryClient", "The 'telemetryClient' object is null."); log.LogError($"Invalid Test Settings : {ex.Message}"); throw ex; } if (String.IsNullOrEmpty(endpoint.Name)) { Exception ex = new ArgumentOutOfRangeException("Name", "No 'Name' was provided in the config."); log.LogError($"Invalid Test Settings : {ex.Message}"); throw ex; } Uri goodUri = null; Uri.TryCreate(endpoint.PageUrl, UriKind.Absolute, out goodUri); if (null == goodUri) { Exception ex = new ArgumentOutOfRangeException("uri", $"The provided uri {endpoint.PageUrl} is invalid."); log.LogError($"Invalid Test Settings : {ex.Message}"); throw ex; } //setup the telemetry string operationId = Guid.NewGuid().ToString("N"); var availability = new AvailabilityTelemetry { Id = operationId, Name = $"{test.ApplicationName} : {endpoint.Name}", RunLocation = System.Environment.MachineName, Success = false }; //start the timer var stopwatch = new Stopwatch(); stopwatch.Start(); //try it try { await ExecuteWebGet(log, availability, client, goodUri).ConfigureAwait(false); } catch (HttpRequestException ex) { //grab the inner exception if the Request fails outright availability.Message = ex.InnerException.Message; var exceptionTelemetry = new ExceptionTelemetry(ex); exceptionTelemetry.Context.Operation.Id = operationId; exceptionTelemetry.Properties.Add("TestName", endpoint.Name); exceptionTelemetry.Properties.Add("TestLocation", System.Environment.MachineName); telemetryClient.TrackException(exceptionTelemetry); } catch (Exception ex) { availability.Message = ex.Message; var exceptionTelemetry = new ExceptionTelemetry(ex); exceptionTelemetry.Context.Operation.Id = operationId; exceptionTelemetry.Properties.Add("TestName", endpoint.Name); exceptionTelemetry.Properties.Add("TestLocation", System.Environment.MachineName); telemetryClient.TrackException(exceptionTelemetry); } finally { stopwatch.Stop(); availability.Duration = stopwatch.Elapsed; availability.Timestamp = DateTimeOffset.UtcNow; telemetryClient.TrackAvailability(availability); // call flush to ensure telemetry is sent telemetryClient.Flush(); } }