From 5d3868473f23c113834c416b4d5b68fbee04842f Mon Sep 17 00:00:00 2001 From: xchen Date: Wed, 25 Sep 2024 17:54:24 -0700 Subject: [PATCH] consolidate some logging --- .../Scheduling/ResourcesScrapingJob.cs | 7 +- .../ScrapeDefinitionBatchProperties.cs | 105 ++++++++++++++++++ 2 files changed, 109 insertions(+), 3 deletions(-) create mode 100644 src/Promitor.Core.Scraping/Configuration/Model/Metrics/ScrapeDefinitionBatchProperties.cs diff --git a/src/Promitor.Agents.Scraper/Scheduling/ResourcesScrapingJob.cs b/src/Promitor.Agents.Scraper/Scheduling/ResourcesScrapingJob.cs index 1e5a7d834..f01c2b2cf 100644 --- a/src/Promitor.Agents.Scraper/Scheduling/ResourcesScrapingJob.cs +++ b/src/Promitor.Agents.Scraper/Scheduling/ResourcesScrapingJob.cs @@ -254,9 +254,10 @@ private async Task ScrapeMetrics(IEnumerable(); var batchScrapingEnabled = this._azureMonitorIntegrationConfiguration.Value.MetricsBatching?.Enabled ?? false; - Logger.LogInformation("Parsed batch config: {Enabled}, {BatchSize}", this._azureMonitorIntegrationConfiguration.Value.MetricsBatching.Enabled, this._azureMonitorIntegrationConfiguration.Value.MetricsBatching.MaxBatchSize); - Logger.LogInformation("Parsed SDK runtime config {Enabled}", this._azureMonitorIntegrationConfiguration.Value.UseAzureMonitorSdk); if (batchScrapingEnabled) { + Logger.LogInformation("Promitor Scraper with operate in batch scraping mode, with max batch size {BatchSize}", this._azureMonitorIntegrationConfiguration.Value.MetricsBatching.MaxBatchSize); + Logger.LogWarning(""); + var batchScrapeDefinitions = AzureResourceDefinitionBatching.GroupScrapeDefinitions(scrapeDefinitions, this._azureMonitorIntegrationConfiguration.Value.MetricsBatching.MaxBatchSize); foreach(var batchScrapeDefinition in batchScrapeDefinitions) { @@ -266,7 +267,7 @@ private async Task ScrapeMetrics(IEnumerable definition in batchScrapeDefinition.ScrapeDefinitions) { - Logger.LogInformation("ResourceID: {ResoureceID}, ResourceGroup: {ResourceGroup}, Prometheus metric name: {MetricName}, Batch Key: {BatchKey}", definition.Resource.ResourceName, definition.ResourceGroupName, definition.PrometheusMetricDefinition.Name, definition.BuildScrapingBatchInfo().BuildBatchHashKey()); + Logger.LogInformation("ResourceID: {ResourceID}, ResourceGroup: {ResourceGroup}, Prometheus metric name: {MetricName}, Batch Key: {BatchKey}", definition.Resource.ResourceName, definition.ResourceGroupName, definition.PrometheusMetricDefinition.Name, definition.BuildScrapingBatchInfo().BuildBatchHashKey()); } await ScheduleLimitedConcurrencyAsyncTask(tasks, () => ScrapeMetricBatched(batchScrapeDefinition), cancellationToken); } diff --git a/src/Promitor.Core.Scraping/Configuration/Model/Metrics/ScrapeDefinitionBatchProperties.cs b/src/Promitor.Core.Scraping/Configuration/Model/Metrics/ScrapeDefinitionBatchProperties.cs new file mode 100644 index 000000000..b3f8868bf --- /dev/null +++ b/src/Promitor.Core.Scraping/Configuration/Model/Metrics/ScrapeDefinitionBatchProperties.cs @@ -0,0 +1,105 @@ +using System; +using System.Collections.Generic; +using GuardNet; +using Promitor.Core.Contracts; + +namespace Promitor.Core.Scraping.Configuration.Model.Metrics +{ + /// + /// Defines properties of a batch of scrape definitions + /// + public class ScrapeDefinitionBatchProperties : IEquatable + { + /// Configuration about the Azure Monitor metric to scrape + /// Configuration about the LogAnalytics resource to scrape + /// The details of the prometheus metric that will be created. + /// The scraping model. + /// Resource type of the batch + /// Specify a subscription to scrape that defers from the default subscription. + public ScrapeDefinitionBatchProperties( + AzureMetricConfiguration azureMetricConfiguration, + LogAnalyticsConfiguration logAnalyticsConfiguration, + PrometheusMetricDefinition prometheusMetricDefinition, + ResourceType resourceType, + Scraping scraping, + string subscriptionId) + { + Guard.NotNull(azureMetricConfiguration, nameof(azureMetricConfiguration)); + Guard.NotNull(prometheusMetricDefinition, nameof(prometheusMetricDefinition)); + Guard.NotNull(scraping, nameof(scraping)); + Guard.NotNull(subscriptionId, nameof(subscriptionId)); + + AzureMetricConfiguration = azureMetricConfiguration; + LogAnalyticsConfiguration = logAnalyticsConfiguration; + PrometheusMetricDefinition = prometheusMetricDefinition; + Scraping = scraping; + SubscriptionId = subscriptionId; + ResourceType = resourceType; + } + + /// + /// Configuration about the Azure Monitor metric to scrape + /// + public AzureMetricConfiguration AzureMetricConfiguration { get; } + + /// + /// Configuration about the Azure Monitor log analytics resource to scrape + /// + public LogAnalyticsConfiguration LogAnalyticsConfiguration { get; } + + /// + /// The details of the prometheus metric that will be created. + /// + public PrometheusMetricDefinition PrometheusMetricDefinition { get; } + + /// + /// The scraping model. + /// + public Scraping Scraping { get; } + + /// + /// The Azure subscription to get the metric from. + /// + public string SubscriptionId { get; } + + /// + /// The Azure resource type shared by all scrape definitions in the batch + /// + public ResourceType ResourceType { get; } + + public TimeSpan? GetAggregationInterval() + { + if (ResourceType == ResourceType.LogAnalytics) + { + return LogAnalyticsConfiguration?.Aggregation?.Interval; + } + return AzureMetricConfiguration?.Aggregation?.Interval; + } + + public override int GetHashCode() + { + return this.BuildBatchHashKey().GetHashCode(); + } + + /// + /// Builds a namespaced string key to satisfy batch restrictions, in the format of + /// (AzureMetricAndDimensionsAndFilter)_(SubscriptionId)_(ResourceType)_(AggregationInterval>) + /// + public string BuildBatchHashKey() + { + return string.Join("_", new List {AzureMetricConfiguration.ToUniqueStringRepresentation(), SubscriptionId, ResourceType.ToString(), GetAggregationInterval().ToString()}); + } + + /// + /// Equality comparison override in case of hash collision + /// + public bool Equals(ScrapeDefinitionBatchProperties obj) + { + if (obj is null || !(obj is ScrapeDefinitionBatchProperties)) + return false; + + ScrapeDefinitionBatchProperties other = obj; + return ResourceType == other.ResourceType && AzureMetricConfiguration.ToUniqueStringRepresentation() == other.AzureMetricConfiguration.ToUniqueStringRepresentation() && SubscriptionId == other.SubscriptionId && GetAggregationInterval().Equals(other.GetAggregationInterval()); + } + } +} \ No newline at end of file