static async Task <int> MainAsync() { try { Logger.LogInformation("Validate Metrics Main() started."); (CancellationTokenSource cts, ManualResetEventSlim completed, Option <object> handler) = ShutdownHandler.Init(TimeSpan.FromSeconds(5), Logger); IConfiguration configuration = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("config/appsettings.json", optional: true) .AddEnvironmentVariables() .Build(); using (ModuleClient moduleClient = await ModuleClient.CreateFromEnvironmentAsync()) using (MetricsScraper scraper = new MetricsScraper(new List <string> { "http://edgeHub:9600/metrics", "http://edgeAgent:9600/metrics" })) { await moduleClient.OpenAsync(); await moduleClient.SetMethodHandlerAsync( "ValidateMetrics", async (MethodRequest methodRequest, object _) => { Logger.LogInformation("Validating metrics"); TestReporter testReporter = new TestReporter("Metrics Validation"); List <TestBase> tests = new List <TestBase> { new ValidateNumberOfMessagesSent(testReporter, scraper, moduleClient), new ValidateDocumentedMetrics(testReporter, scraper, moduleClient), new ValidateHostRanges(testReporter, scraper, moduleClient), }; using (testReporter.MeasureDuration()) { await Task.WhenAll(tests.Select(test => test.Start(cts.Token))); } return(new MethodResponse(Encoding.UTF8.GetBytes(testReporter.ReportResults()), (int)HttpStatusCode.OK)); }, null); Logger.LogInformation("Ready to validate metrics"); await cts.Token.WhenCanceled(); } completed.Set(); handler.ForEach(h => GC.KeepAlive(h)); Logger.LogInformation("Validate Metrics Main() finished."); } catch (Exception e) { Logger.LogError(e.ToString()); } return(0); }
static async Task <int> MainAsync() { (CancellationTokenSource cts, ManualResetEventSlim completed, Option <object> handler) = ShutdownHandler.Init(TimeSpan.FromSeconds(5), Logger); Logger.LogInformation($"Starting metrics collector with the following settings:\r\n{Settings.Current}"); MqttTransportSettings mqttSetting = new MqttTransportSettings(TransportType.Mqtt_Tcp_Only); ITransportSettings[] transportSettings = { mqttSetting }; ModuleClient moduleClient = null; try { moduleClient = await ModuleClient.CreateFromEnvironmentAsync(transportSettings); Dictionary <string, string> additionalTags = await GetAdditionalTagsFromTwin(moduleClient); MetricsScraper scraper = new MetricsScraper(Settings.Current.Endpoints); IMetricsPublisher publisher; if (Settings.Current.UploadTarget == UploadTarget.AzureLogAnalytics) { publisher = new LogAnalyticsUpload(Settings.Current.LogAnalyticsWorkspaceId, Settings.Current.LogAnalyticsWorkspaceKey, Settings.Current.LogAnalyticsLogType); } else { publisher = new EventHubMetricsUpload(moduleClient); } using (MetricsScrapeAndUpload metricsScrapeAndUpload = new MetricsScrapeAndUpload(scraper, publisher, additionalTags)) { TimeSpan scrapeAndUploadInterval = TimeSpan.FromSeconds(Settings.Current.ScrapeFrequencySecs); metricsScrapeAndUpload.Start(scrapeAndUploadInterval); await cts.Token.WhenCanceled(); } } catch (Exception e) { Logger.LogError(e, "Error occurred during metrics collection setup."); } finally { moduleClient?.Dispose(); } completed.Set(); handler.ForEach(h => GC.KeepAlive(h)); Logger.LogInformation("MetricsCollector Main() finished."); return(0); }
static async Task <int> MainAsync() { try { Logger.LogInformation("Validate Metrics Main() started."); (CancellationTokenSource cts, ManualResetEventSlim completed, Option <object> handler) = ShutdownHandler.Init(TimeSpan.FromSeconds(5), Logger); IConfiguration configuration = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("config/appsettings.json", optional: true) .AddEnvironmentVariables() .Build(); var transportType = configuration.GetValue("ClientTransportType", Microsoft.Azure.Devices.Client.TransportType.Mqtt); Logger.LogInformation("Make Client"); using (ModuleClient moduleClient = await ModuleUtil.CreateModuleClientAsync( transportType, new ClientOptions(), ModuleUtil.DefaultTimeoutErrorDetectionStrategy, ModuleUtil.DefaultTransientRetryStrategy, Logger)) using (MetricsScraper scraper = new MetricsScraper(new List <string> { "http://edgeHub:9600/metrics", "http://edgeAgent:9600/metrics" })) { Logger.LogInformation("Open Async"); await moduleClient.OpenAsync(); Logger.LogInformation("Set method handler"); await moduleClient.SetMethodHandlerAsync( "ValidateMetrics", async (MethodRequest methodRequest, object _) => { Logger.LogInformation("Validating metrics"); TestReporter testReporter = new TestReporter("Metrics Validation"); List <TestBase> tests = new List <TestBase> { new ValidateMessages(testReporter, scraper, moduleClient, transportType), new ValidateDocumentedMetrics(testReporter, scraper, moduleClient), // new ValidateHostRanges(testReporter, scraper, moduleClient), }; using (testReporter.MeasureDuration()) { await Task.WhenAll(tests.Select(test => test.Start(cts.Token))); } var result = new MethodResponse(Encoding.UTF8.GetBytes(testReporter.ReportResults()), (int)HttpStatusCode.OK); Logger.LogInformation($"Finished validating metrics. Result size: {result.Result.Length}"); return(result); }, null); moduleClient.SetConnectionStatusChangesHandler((status, reason) => Logger.LogWarning($"Module to Edge Hub connection changed Status: {status} Reason: {reason}")); Logger.LogInformation("Ready to validate metrics"); await cts.Token.WhenCanceled(); } completed.Set(); handler.ForEach(h => GC.KeepAlive(h)); Logger.LogInformation("Validate Metrics Main() finished."); } catch (Exception e) { Logger.LogError(e.ToString()); } return(0); }