public async Task ExecuteAsync(CancellationToken cancellationToken) { Logger.LogInformation("Scraping resource collection {ResourceDiscoveryGroup} - {Timestamp}", ResourceDiscoveryGroupName, DateTimeOffset.UtcNow); try { var discoveredResources = await _resourceDiscoveryRepository.GetResourceDiscoveryGroupAsync(ResourceDiscoveryGroupName); Logger.LogInformation("Discovered {ResourceCount} resources for resource collection {ResourceDiscoveryGroup}.", discoveredResources?.Count ?? 0, ResourceDiscoveryGroupName); if (discoveredResources == null) { Logger.LogWarning("Discovered no resources for resource collection {ResourceDiscoveryGroup}.", ResourceDiscoveryGroupName); return; } List <Task> scrapeTasks = new List <Task>(); foreach (var discoveredResource in discoveredResources) { Logger.LogDebug($"Scraping discovered resource {discoveredResource}"); var azureMonitorClient = _azureMonitorClientFactory.CreateIfNotExists(_azureMetadata.Cloud, _azureMetadata.TenantId, discoveredResource.SubscriptionId, _metricSinkWriter, _runtimeMetricCollector, _configuration, _azureMonitorLoggingConfiguration, _loggerFactory); // Scrape resource var scrapeTask = ScrapeResourceAsync(discoveredResource, azureMonitorClient); scrapeTasks.Add(scrapeTask); } await Task.WhenAll(scrapeTasks); } catch (Exception exception) { Logger.LogCritical(exception, "Failed to scrape resource collection {ResourceDiscoveryGroup}: {Exception}", ResourceDiscoveryGroupName, exception.Message); } }
private async Task GetDiscoveryGroupScrapeDefinitions(string resourceDiscoveryGroupName, MetricDefinition metricDefinition, ConcurrentBag <ScrapeDefinition <IAzureResourceDefinition> > scrapeDefinitions) { // this runs in a separate thread, must trap exceptions try { Logger.LogInformation("Scraping resource collection {ResourceDiscoveryGroup}.", resourceDiscoveryGroupName); var discoveredResources = await _resourceDiscoveryRepository.GetResourceDiscoveryGroupAsync(resourceDiscoveryGroupName); if (discoveredResources == null) { Logger.LogWarning("Discovered no resources for resource collection {ResourceDiscoveryGroup}.", resourceDiscoveryGroupName); return; } Logger.LogInformation("Discovered {ResourceCount} resources for resource collection {ResourceDiscoveryGroup}.", discoveredResources.Count, resourceDiscoveryGroupName); foreach (var discoveredResource in discoveredResources) { Logger.LogDebug("Discovered resource {DiscoveredResource}.", discoveredResource); var scrapeDefinition = metricDefinition.CreateScrapeDefinition(discoveredResource, _metricsDeclaration.AzureMetadata); scrapeDefinitions.Add(scrapeDefinition); } } catch (Exception ex) { Logger.LogError(ex, "Failed to discover resources for group {GroupName}.", resourceDiscoveryGroupName); } }