Skip to content

Commit

Permalink
Provide resource_uri label for all metrics (#562)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomkerkhove authored May 20, 2019
1 parent a35a4fc commit d6ec76a
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ public ContainerInstanceScraper(AzureMetadata azureMetadata, AzureMonitorClient
{
}

protected override async Task<double> ScrapeResourceAsync(string subscriptionId, string resourceGroupName, ContainerInstanceMetricDefinition metricDefinition, AggregationType aggregationType, TimeSpan aggregationInterval)
protected override async Task<ScrapeResult> 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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ public ContainerRegistryScraper(AzureMetadata azureMetadata, AzureMonitorClient
{
}

protected override async Task<double> ScrapeResourceAsync(string subscriptionId, string resourceGroupName, ContainerRegistryMetricDefinition metricDefinition, AggregationType aggregationType, TimeSpan aggregationInterval)
protected override async Task<ScrapeResult> 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);
}
}
}
4 changes: 2 additions & 2 deletions src/Promitor.Core.Scraping/ResourceTypes/CosmosDbScraper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ public CosmosDbScraper(AzureMetadata azureMetadata, AzureMonitorClient azureMoni
{
}

protected override async Task<double> ScrapeResourceAsync(string subscriptionId, string resourceGroupName, CosmosDbMetricDefinition metricDefinition, AggregationType aggregationType, TimeSpan aggregationInterval)
protected override async Task<ScrapeResult> 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);
}
}
}
4 changes: 2 additions & 2 deletions src/Promitor.Core.Scraping/ResourceTypes/GenericScraper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ public GenericScraper(AzureMetadata azureMetadata, AzureMonitorClient azureMonit
{
}

protected override async Task<double> ScrapeResourceAsync(string subscriptionId, string resourceGroupName, GenericAzureMetricDefinition metricDefinition, AggregationType aggregationType, TimeSpan aggregationInterval)
protected override async Task<ScrapeResult> 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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ public NetworkInterfaceScraper(AzureMetadata azureMetadata, AzureMonitorClient a
{
}

protected override async Task<double> ScrapeResourceAsync(string subscriptionId, string resourceGroupName, NetworkInterfaceMetricDefinition metricDefinition, AggregationType aggregationType, TimeSpan aggregationInterval)
protected override async Task<ScrapeResult> 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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ public ServiceBusQueueScraper(AzureMetadata azureMetadata, AzureMonitorClient az
{
}

protected override async Task<double> ScrapeResourceAsync(string subscriptionId, string resourceGroupName, ServiceBusQueueMetricDefinition metricDefinition, AggregationType aggregationType, TimeSpan aggregationInterval)
protected override async Task<ScrapeResult> ScrapeResourceAsync(string subscriptionId, string resourceGroupName, ServiceBusQueueMetricDefinition metricDefinition, AggregationType aggregationType, TimeSpan aggregationInterval)
{
var resourceUri = string.Format(ResourceUriTemplate, subscriptionId, resourceGroupName, metricDefinition.Namespace);

var filter = $"EntityName eq '{metricDefinition.QueueName}'";
var metricName = metricDefinition.AzureMetricConfiguration.MetricName;
var foundMetricValue = await AzureMonitorClient.QueryMetricAsync(metricName, aggregationType, aggregationInterval, resourceUri, filter);

return foundMetricValue;
return new ScrapeResult(resourceUri, foundMetricValue);
}
}
}
13 changes: 10 additions & 3 deletions src/Promitor.Core.Scraping/ResourceTypes/StorageQueueScraper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,38 @@ namespace Promitor.Core.Scraping.ResourceTypes
{
public class StorageQueueScraper : Scraper<StorageQueueMetricDefinition>
{
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)
{
_azureStorageQueueClient = new AzureStorageQueueClient(logger);
}

protected override async Task<double> ScrapeResourceAsync(string subscriptionId, string resourceGroupName, StorageQueueMetricDefinition metricDefinition, AggregationType aggregationType, TimeSpan aggregationInterval)
protected override async Task<ScrapeResult> 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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ public VirtualMachineScraper(AzureMetadata azureMetadata, AzureMonitorClient azu
{
}

protected override async Task<double> ScrapeResourceAsync(string subscriptionId, string resourceGroupName, VirtualMachineMetricDefinition metricDefinition, AggregationType aggregationType, TimeSpan aggregationInterval)
protected override async Task<ScrapeResult> 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);
}
}
}
30 changes: 30 additions & 0 deletions src/Promitor.Core.Scraping/ScrapeResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using GuardNet;

namespace Promitor.Core.Scraping
{
public class ScrapeResult
{
/// <summary>
/// Constructor
/// </summary>
/// <param name="resourceUri">Uri of the resource that was scraped</param>
/// <param name="metricValue">Value of the metric that was found</param>
public ScrapeResult(string resourceUri, double metricValue)
{
Guard.NotNullOrEmpty(resourceUri, nameof(resourceUri));

ResourceUri = resourceUri;
MetricValue = metricValue;
}

/// <summary>
/// Uri of the resource that was scraped
/// </summary>
public string ResourceUri { get; }

/// <summary>
/// Value of the metric that was found
/// </summary>
public double MetricValue { get; }
}
}
10 changes: 5 additions & 5 deletions src/Promitor.Core.Scraping/Scraper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -102,6 +102,6 @@ public async Task ScrapeAsync(MetricDefinition metricDefinition)
/// <param name="aggregationType">Aggregation for the metric to use</param>
/// <param name="aggregationInterval">Interval that is used to aggregate metrics</param>
///
protected abstract Task<double> ScrapeResourceAsync(string subscriptionId, string resourceGroupName, TMetricDefinition metricDefinition, AggregationType aggregationType, TimeSpan aggregationInterval);
protected abstract Task<ScrapeResult> ScrapeResourceAsync(string subscriptionId, string resourceGroupName, TMetricDefinition metricDefinition, AggregationType aggregationType, TimeSpan aggregationInterval);
}
}

0 comments on commit d6ec76a

Please sign in to comment.