Skip to content

Commit

Permalink
consolidate some logging
Browse files Browse the repository at this point in the history
  • Loading branch information
hkfgo committed Sep 26, 2024
1 parent 11d765c commit 5d38684
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,10 @@ private async Task ScrapeMetrics(IEnumerable<ScrapeDefinition<IAzureResourceDefi
{
var tasks = new List<Task>();
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) {
Expand All @@ -266,7 +267,7 @@ private async Task ScrapeMetrics(IEnumerable<ScrapeDefinition<IAzureResourceDefi
Logger.LogInformation("Logging individual definitions batch: Azure Metric {AzureMetricName} and resource type {ResourceType}.", azureMetricName, resourceType);
foreach (ScrapeDefinition<IAzureResourceDefinition> 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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
using System;
using System.Collections.Generic;
using GuardNet;
using Promitor.Core.Contracts;

namespace Promitor.Core.Scraping.Configuration.Model.Metrics
{
/// <summary>
/// Defines properties of a batch of scrape definitions
/// </summary>
public class ScrapeDefinitionBatchProperties : IEquatable<ScrapeDefinitionBatchProperties>
{
/// <param name="azureMetricConfiguration">Configuration about the Azure Monitor metric to scrape</param>
/// <param name="logAnalyticsConfiguration">Configuration about the LogAnalytics resource to scrape</param>
/// <param name="prometheusMetricDefinition">The details of the prometheus metric that will be created.</param>
/// <param name="scraping">The scraping model.</param>
/// <param name="resourceType">Resource type of the batch</param>
/// <param name="subscriptionId">Specify a subscription to scrape that defers from the default subscription.</param>
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;
}

/// <summary>
/// Configuration about the Azure Monitor metric to scrape
/// </summary>
public AzureMetricConfiguration AzureMetricConfiguration { get; }

/// <summary>
/// Configuration about the Azure Monitor log analytics resource to scrape
/// </summary>
public LogAnalyticsConfiguration LogAnalyticsConfiguration { get; }

/// <summary>
/// The details of the prometheus metric that will be created.
/// </summary>
public PrometheusMetricDefinition PrometheusMetricDefinition { get; }

/// <summary>
/// The scraping model.
/// </summary>
public Scraping Scraping { get; }

/// <summary>
/// The Azure subscription to get the metric from.
/// </summary>
public string SubscriptionId { get; }

/// <summary>
/// The Azure resource type shared by all scrape definitions in the batch
/// </summary>
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();
}

/// <summary>
/// Builds a namespaced string key to satisfy batch restrictions, in the format of
/// (AzureMetricAndDimensionsAndFilter)_(SubscriptionId)_(ResourceType)_(AggregationInterval>)
/// </summary>
public string BuildBatchHashKey()
{
return string.Join("_", new List<string> {AzureMetricConfiguration.ToUniqueStringRepresentation(), SubscriptionId, ResourceType.ToString(), GetAggregationInterval().ToString()});
}

/// <summary>
/// Equality comparison override in case of hash collision
/// </summary>
public bool Equals(ScrapeDefinitionBatchProperties obj)
{
if (obj is null || !(obj is ScrapeDefinitionBatchProperties))

Check warning on line 98 in src/Promitor.Core.Scraping/Configuration/Model/Metrics/ScrapeDefinitionBatchProperties.cs

View workflow job for this annotation

GitHub Actions / Code Quality (R#)

"[ConvertTypeCheckToNullCheck] Use null check instead of a type check succeeding on any not-null value" on /home/runner/work/promitor/promitor/src/Promitor.Core.Scraping/Configuration/Model/Metrics/ScrapeDefinitionBatchProperties.cs(98,4134)
return false;

ScrapeDefinitionBatchProperties other = obj;
return ResourceType == other.ResourceType && AzureMetricConfiguration.ToUniqueStringRepresentation() == other.AzureMetricConfiguration.ToUniqueStringRepresentation() && SubscriptionId == other.SubscriptionId && GetAggregationInterval().Equals(other.GetAggregationInterval());
}
}
}

0 comments on commit 5d38684

Please sign in to comment.