From cbf04aa206390105e61813f7a56fa448951cb215 Mon Sep 17 00:00:00 2001 From: Tom Kerkhove Date: Mon, 20 May 2019 19:57:21 +0200 Subject: [PATCH] Provide resource_uri label for all metrics --- .../ResourceTypes/ContainerInstanceScraper.cs | 4 +-- .../ResourceTypes/ContainerRegistryScraper.cs | 4 +-- .../ResourceTypes/CosmosDbScraper.cs | 4 +-- .../ResourceTypes/GenericScraper.cs | 4 +-- .../ResourceTypes/NetworkInterfaceScraper.cs | 4 +-- .../ResourceTypes/ServiceBusQueueScraper.cs | 4 +-- .../ResourceTypes/StorageQueueScraper.cs | 13 ++++++-- .../ResourceTypes/VirtualMachineScraper.cs | 4 +-- src/Promitor.Core.Scraping/ScrapeResult.cs | 30 +++++++++++++++++++ src/Promitor.Core.Scraping/Scraper.cs | 10 +++---- 10 files changed, 59 insertions(+), 22 deletions(-) create mode 100644 src/Promitor.Core.Scraping/ScrapeResult.cs diff --git a/src/Promitor.Core.Scraping/ResourceTypes/ContainerInstanceScraper.cs b/src/Promitor.Core.Scraping/ResourceTypes/ContainerInstanceScraper.cs index edb69b705..78d14f9eb 100644 --- a/src/Promitor.Core.Scraping/ResourceTypes/ContainerInstanceScraper.cs +++ b/src/Promitor.Core.Scraping/ResourceTypes/ContainerInstanceScraper.cs @@ -18,14 +18,14 @@ public ContainerInstanceScraper(AzureMetadata azureMetadata, AzureMonitorClient { } - protected override async Task ScrapeResourceAsync(string subscriptionId, string resourceGroupName, ContainerInstanceMetricDefinition metricDefinition, AggregationType aggregationType, TimeSpan aggregationInterval) + protected override async Task ScrapeResourceAsync(string subscriptionId, string resourceGroupName, ContainerInstanceMetricDefinition metricDefinition, AggregationType aggregationType, TimeSpan aggregationInterval) { var resourceUri = string.Format(ResourceUriTemplate, AzureMetadata.SubscriptionId, AzureMetadata.ResourceGroupName, metricDefinition.ContainerGroup); var metricName = metricDefinition.AzureMetricConfiguration.MetricName; var foundMetricValue = await AzureMonitorClient.QueryMetricAsync(metricName, aggregationType, aggregationInterval, resourceUri); - return foundMetricValue; + return new ScrapeResult(resourceUri, foundMetricValue); } } } \ No newline at end of file diff --git a/src/Promitor.Core.Scraping/ResourceTypes/ContainerRegistryScraper.cs b/src/Promitor.Core.Scraping/ResourceTypes/ContainerRegistryScraper.cs index 5a422b59a..7b8ac7161 100644 --- a/src/Promitor.Core.Scraping/ResourceTypes/ContainerRegistryScraper.cs +++ b/src/Promitor.Core.Scraping/ResourceTypes/ContainerRegistryScraper.cs @@ -18,14 +18,14 @@ public ContainerRegistryScraper(AzureMetadata azureMetadata, AzureMonitorClient { } - protected override async Task ScrapeResourceAsync(string subscriptionId, string resourceGroupName, ContainerRegistryMetricDefinition metricDefinition, AggregationType aggregationType, TimeSpan aggregationInterval) + protected override async Task ScrapeResourceAsync(string subscriptionId, string resourceGroupName, ContainerRegistryMetricDefinition metricDefinition, AggregationType aggregationType, TimeSpan aggregationInterval) { var resourceUri = string.Format(ResourceUriTemplate, subscriptionId, resourceGroupName, metricDefinition.RegistryName); var metricName = metricDefinition.AzureMetricConfiguration.MetricName; var foundMetricValue = await AzureMonitorClient.QueryMetricAsync(metricName, aggregationType, aggregationInterval, resourceUri); - return foundMetricValue; + return new ScrapeResult(resourceUri, foundMetricValue); } } } \ No newline at end of file diff --git a/src/Promitor.Core.Scraping/ResourceTypes/CosmosDbScraper.cs b/src/Promitor.Core.Scraping/ResourceTypes/CosmosDbScraper.cs index 247031db1..b1d07fc98 100644 --- a/src/Promitor.Core.Scraping/ResourceTypes/CosmosDbScraper.cs +++ b/src/Promitor.Core.Scraping/ResourceTypes/CosmosDbScraper.cs @@ -18,14 +18,14 @@ public CosmosDbScraper(AzureMetadata azureMetadata, AzureMonitorClient azureMoni { } - protected override async Task ScrapeResourceAsync(string subscriptionId, string resourceGroupName, CosmosDbMetricDefinition metricDefinition, AggregationType aggregationType, TimeSpan aggregationInterval) + protected override async Task ScrapeResourceAsync(string subscriptionId, string resourceGroupName, CosmosDbMetricDefinition metricDefinition, AggregationType aggregationType, TimeSpan aggregationInterval) { var resourceUri = string.Format(ResourceUriTemplate, subscriptionId, resourceGroupName, metricDefinition.DbName); var metricName = metricDefinition.AzureMetricConfiguration.MetricName; var foundMetricValue = await AzureMonitorClient.QueryMetricAsync(metricName, aggregationType, aggregationInterval, resourceUri); - return foundMetricValue; + return new ScrapeResult(resourceUri, foundMetricValue); } } } diff --git a/src/Promitor.Core.Scraping/ResourceTypes/GenericScraper.cs b/src/Promitor.Core.Scraping/ResourceTypes/GenericScraper.cs index de431222f..529cfb36b 100644 --- a/src/Promitor.Core.Scraping/ResourceTypes/GenericScraper.cs +++ b/src/Promitor.Core.Scraping/ResourceTypes/GenericScraper.cs @@ -18,13 +18,13 @@ public GenericScraper(AzureMetadata azureMetadata, AzureMonitorClient azureMonit { } - protected override async Task ScrapeResourceAsync(string subscriptionId, string resourceGroupName, GenericAzureMetricDefinition metricDefinition, AggregationType aggregationType, TimeSpan aggregationInterval) + protected override async Task ScrapeResourceAsync(string subscriptionId, string resourceGroupName, GenericAzureMetricDefinition metricDefinition, AggregationType aggregationType, TimeSpan aggregationInterval) { var resourceUri = string.Format(ResourceUriTemplate, subscriptionId, resourceGroupName, metricDefinition.ResourceUri); var metricName = metricDefinition.AzureMetricConfiguration.MetricName; var foundMetricValue = await AzureMonitorClient.QueryMetricAsync(metricName, aggregationType, aggregationInterval, resourceUri, metricDefinition.Filter); - return foundMetricValue; + return new ScrapeResult(resourceUri, foundMetricValue); } } } \ No newline at end of file diff --git a/src/Promitor.Core.Scraping/ResourceTypes/NetworkInterfaceScraper.cs b/src/Promitor.Core.Scraping/ResourceTypes/NetworkInterfaceScraper.cs index 3b62f6e4c..32b8f6ab2 100644 --- a/src/Promitor.Core.Scraping/ResourceTypes/NetworkInterfaceScraper.cs +++ b/src/Promitor.Core.Scraping/ResourceTypes/NetworkInterfaceScraper.cs @@ -18,14 +18,14 @@ public NetworkInterfaceScraper(AzureMetadata azureMetadata, AzureMonitorClient a { } - protected override async Task ScrapeResourceAsync(string subscriptionId, string resourceGroupName, NetworkInterfaceMetricDefinition metricDefinition, AggregationType aggregationType, TimeSpan aggregationInterval) + protected override async Task ScrapeResourceAsync(string subscriptionId, string resourceGroupName, NetworkInterfaceMetricDefinition metricDefinition, AggregationType aggregationType, TimeSpan aggregationInterval) { var resourceUri = string.Format(ResourceUriTemplate, AzureMetadata.SubscriptionId, AzureMetadata.ResourceGroupName, metricDefinition.NetworkInterfaceName); var metricName = metricDefinition.AzureMetricConfiguration.MetricName; var foundMetricValue = await AzureMonitorClient.QueryMetricAsync(metricName, aggregationType, aggregationInterval, resourceUri); - return foundMetricValue; + return new ScrapeResult(resourceUri, foundMetricValue); } } } \ No newline at end of file diff --git a/src/Promitor.Core.Scraping/ResourceTypes/ServiceBusQueueScraper.cs b/src/Promitor.Core.Scraping/ResourceTypes/ServiceBusQueueScraper.cs index 13d466d03..d8026fa45 100644 --- a/src/Promitor.Core.Scraping/ResourceTypes/ServiceBusQueueScraper.cs +++ b/src/Promitor.Core.Scraping/ResourceTypes/ServiceBusQueueScraper.cs @@ -18,7 +18,7 @@ public ServiceBusQueueScraper(AzureMetadata azureMetadata, AzureMonitorClient az { } - protected override async Task ScrapeResourceAsync(string subscriptionId, string resourceGroupName, ServiceBusQueueMetricDefinition metricDefinition, AggregationType aggregationType, TimeSpan aggregationInterval) + protected override async Task ScrapeResourceAsync(string subscriptionId, string resourceGroupName, ServiceBusQueueMetricDefinition metricDefinition, AggregationType aggregationType, TimeSpan aggregationInterval) { var resourceUri = string.Format(ResourceUriTemplate, subscriptionId, resourceGroupName, metricDefinition.Namespace); @@ -26,7 +26,7 @@ protected override async Task ScrapeResourceAsync(string subscriptionId, var metricName = metricDefinition.AzureMetricConfiguration.MetricName; var foundMetricValue = await AzureMonitorClient.QueryMetricAsync(metricName, aggregationType, aggregationInterval, resourceUri, filter); - return foundMetricValue; + return new ScrapeResult(resourceUri, foundMetricValue); } } } \ No newline at end of file diff --git a/src/Promitor.Core.Scraping/ResourceTypes/StorageQueueScraper.cs b/src/Promitor.Core.Scraping/ResourceTypes/StorageQueueScraper.cs index 88566a75f..0dfce0747 100644 --- a/src/Promitor.Core.Scraping/ResourceTypes/StorageQueueScraper.cs +++ b/src/Promitor.Core.Scraping/ResourceTypes/StorageQueueScraper.cs @@ -13,6 +13,7 @@ namespace Promitor.Core.Scraping.ResourceTypes { public class StorageQueueScraper : Scraper { + private const string ResourceUriTemplate = "subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Storage/storageAccounts/{2}/queueServices"; private readonly AzureStorageQueueClient _azureStorageQueueClient; public StorageQueueScraper(AzureMetadata azureMetadata, AzureMonitorClient azureMonitorClient, ILogger logger, IExceptionTracker exceptionTracker) : base(azureMetadata, azureMonitorClient, logger, exceptionTracker) @@ -20,24 +21,30 @@ public StorageQueueScraper(AzureMetadata azureMetadata, AzureMonitorClient azure _azureStorageQueueClient = new AzureStorageQueueClient(logger); } - protected override async Task ScrapeResourceAsync(string subscriptionId, string resourceGroupName, StorageQueueMetricDefinition metricDefinition, AggregationType aggregationType, TimeSpan aggregationInterval) + protected override async Task ScrapeResourceAsync(string subscriptionId, string resourceGroupName, StorageQueueMetricDefinition metricDefinition, AggregationType aggregationType, TimeSpan aggregationInterval) { Guard.NotNull(metricDefinition, nameof(metricDefinition)); Guard.NotNull(metricDefinition.AzureMetricConfiguration, nameof(metricDefinition.AzureMetricConfiguration)); Guard.NotNull(metricDefinition.SasToken, nameof(metricDefinition.SasToken)); Guard.NotNullOrEmpty(metricDefinition.AzureMetricConfiguration.MetricName, nameof(metricDefinition.AzureMetricConfiguration.MetricName)); + var resourceUri = string.Format(ResourceUriTemplate, subscriptionId, resourceGroupName, metricDefinition.AccountName); var sasToken = metricDefinition.SasToken.GetSecretValue(); + double foundMetricValue; switch (metricDefinition.AzureMetricConfiguration.MetricName.ToLowerInvariant()) { case AzureStorageConstants.Queues.Metrics.TimeSpentInQueue: - return await _azureStorageQueueClient.GetQueueMessageTimeSpentInQueueAsync(metricDefinition.AccountName, metricDefinition.QueueName, sasToken); + foundMetricValue = await _azureStorageQueueClient.GetQueueMessageTimeSpentInQueueAsync(metricDefinition.AccountName, metricDefinition.QueueName, sasToken); + break; case AzureStorageConstants.Queues.Metrics.MessageCount: - return await _azureStorageQueueClient.GetQueueMessageCountAsync(metricDefinition.AccountName, metricDefinition.QueueName, sasToken); + foundMetricValue = await _azureStorageQueueClient.GetQueueMessageCountAsync(metricDefinition.AccountName, metricDefinition.QueueName, sasToken); + break; default: throw new InvalidMetricNameException(metricDefinition.AzureMetricConfiguration.MetricName, metricDefinition.ResourceType.ToString()); } + + return new ScrapeResult(resourceUri, foundMetricValue); } } } \ No newline at end of file diff --git a/src/Promitor.Core.Scraping/ResourceTypes/VirtualMachineScraper.cs b/src/Promitor.Core.Scraping/ResourceTypes/VirtualMachineScraper.cs index 97bea90a3..611746721 100644 --- a/src/Promitor.Core.Scraping/ResourceTypes/VirtualMachineScraper.cs +++ b/src/Promitor.Core.Scraping/ResourceTypes/VirtualMachineScraper.cs @@ -18,14 +18,14 @@ public VirtualMachineScraper(AzureMetadata azureMetadata, AzureMonitorClient azu { } - protected override async Task ScrapeResourceAsync(string subscriptionId, string resourceGroupName, VirtualMachineMetricDefinition metricDefinition, AggregationType aggregationType, TimeSpan aggregationInterval) + protected override async Task ScrapeResourceAsync(string subscriptionId, string resourceGroupName, VirtualMachineMetricDefinition metricDefinition, AggregationType aggregationType, TimeSpan aggregationInterval) { var resourceUri = string.Format(ResourceUriTemplate, AzureMetadata.SubscriptionId, AzureMetadata.ResourceGroupName, metricDefinition.VirtualMachineName); var metricName = metricDefinition.AzureMetricConfiguration.MetricName; var foundMetricValue = await AzureMonitorClient.QueryMetricAsync(metricName, aggregationType, aggregationInterval, resourceUri); - return foundMetricValue; + return new ScrapeResult(resourceUri, foundMetricValue); } } } \ No newline at end of file diff --git a/src/Promitor.Core.Scraping/ScrapeResult.cs b/src/Promitor.Core.Scraping/ScrapeResult.cs new file mode 100644 index 000000000..afc8c24e6 --- /dev/null +++ b/src/Promitor.Core.Scraping/ScrapeResult.cs @@ -0,0 +1,30 @@ +using GuardNet; + +namespace Promitor.Core.Scraping +{ + public class ScrapeResult + { + /// + /// Constructor + /// + /// Uri of the resource that was scraped + /// Value of the metric that was found + public ScrapeResult(string resourceUri, double metricValue) + { + Guard.NotNullOrEmpty(resourceUri, nameof(resourceUri)); + + ResourceUri = resourceUri; + MetricValue = metricValue; + } + + /// + /// Uri of the resource that was scraped + /// + public string ResourceUri { get; } + + /// + /// Value of the metric that was found + /// + public double MetricValue { get; } + } +} \ No newline at end of file diff --git a/src/Promitor.Core.Scraping/Scraper.cs b/src/Promitor.Core.Scraping/Scraper.cs index 261307bff..0ff52d425 100644 --- a/src/Promitor.Core.Scraping/Scraper.cs +++ b/src/Promitor.Core.Scraping/Scraper.cs @@ -78,14 +78,14 @@ public async Task ScrapeAsync(MetricDefinition metricDefinition) var aggregationInterval = metricDefinition.AzureMetricConfiguration.Aggregation.Interval; var aggregationType = metricDefinition.AzureMetricConfiguration.Aggregation.Type; var resourceGroupName = string.IsNullOrEmpty(metricDefinition.ResourceGroupName) ? AzureMetadata.ResourceGroupName : metricDefinition.ResourceGroupName; - var foundMetricValue = await ScrapeResourceAsync(AzureMetadata.SubscriptionId, resourceGroupName, castedMetricDefinition, aggregationType, aggregationInterval.Value); + var scrapedMetricResult = await ScrapeResourceAsync(AzureMetadata.SubscriptionId, resourceGroupName, castedMetricDefinition, aggregationType, aggregationInterval.Value); - _logger.LogInformation("Found value '{MetricValue}' for metric '{MetricName}' with aggregation interval '{AggregationInterval}'", foundMetricValue, metricDefinition.Name, aggregationInterval); + _logger.LogInformation("Found value '{MetricValue}' for metric '{MetricName}' with aggregation interval '{AggregationInterval}'", scrapedMetricResult, metricDefinition.Name, aggregationInterval); var metricsTimestampFeatureFlag = FeatureFlag.IsActive(FeatureFlag.Names.MetricsTimestamp, defaultFlagState: true); - var gauge = Metrics.CreateGauge(metricDefinition.Name, metricDefinition.Description, includeTimestamp: metricsTimestampFeatureFlag); - gauge.Set(foundMetricValue); + var gauge = Metrics.CreateGauge(metricDefinition.Name, metricDefinition.Description, includeTimestamp: metricsTimestampFeatureFlag, labelNames:"resource_uri"); + gauge.WithLabels(scrapedMetricResult.ResourceUri).Set(scrapedMetricResult.MetricValue); } catch (Exception exception) { @@ -102,6 +102,6 @@ public async Task ScrapeAsync(MetricDefinition metricDefinition) /// Aggregation for the metric to use /// Interval that is used to aggregate metrics /// - protected abstract Task ScrapeResourceAsync(string subscriptionId, string resourceGroupName, TMetricDefinition metricDefinition, AggregationType aggregationType, TimeSpan aggregationInterval); + protected abstract Task ScrapeResourceAsync(string subscriptionId, string resourceGroupName, TMetricDefinition metricDefinition, AggregationType aggregationType, TimeSpan aggregationInterval); } }