Skip to content

Commit

Permalink
Update runtime model to support multiple resources per metric (#666)
Browse files Browse the repository at this point in the history
* Alter MetricDefinition to allow multiple resources

I've updated `MetricDefinition` to support multiple resources so that we can define a single prometheus metric that is used to store the values for multiple Azure resources.

In order to do this, I've changed `MetricDefinition` so that it doesn't describe an Azure resource anymore, and created `AzureResourceDefinition` to be the base class for each resource type. I've also created a `PrometheusMetricDefinition` class to contain the details of the prometheus metric being defined.

In addition, I've added a new class, `ScrapeDefinition`, that describes a single Azure resource being scraped. This is used in the places where `MetricDefinition` would have been used previously as part of scraping.

* Convert MetricDefinition implementations

- Updated all the `MetricDefinition` implementations to inherit from `AzureResourceDefinition` instead.

* Update scrapers

I've updated the scraper implementations to work with the new MetricDefinition format. I couldn't come up with a neat way of making MetricDefinition generic without ending up with a nightmare, so for now what I've done is altered the signature of `ScrapeResourceAsync` to accept both the `ScrapeDefinition` object, as well as the `AzureResourceDefinition` cast to the correct type for the subclass.

* Update plumbing code

Updated a couple of the objects used to configure the scraping to handle multiple resources per metric.

* Update validation objects

- Removed the `MetricValidator` abstract class because it doesn't add anything now that we can't cast the `MetricDefinition` to a specific resource type.
- Updated the validators to validate all the resources in the metric definition.

* Update unit tests to handle new MetricDefinition

- Updating `MetricsDeclarationBuilder` to use v1 Builder objects instead of the model objects.
- Updating the tests to handle the fact that a `MetricDefinition` isn't a resource anymore.

* Rename AzureResourceDefinition subclasses

When I was making the initial set of changes, I didn't want to make too many changes at once, so I held off on renaming the subclasses of `AzureResourceDefinition`. This change renames them all from `{Type}MetricDefinition` to `{Type}ResourceDefinition` to make it clearer what they are.

* Populate MetricsDeclarationBuilder.Version

I realised that the Version property wasn't being populated on either the model object or the serialization object. I've removed it from the model object since it isn't versioned.

I've also updated the v1 ConfigurationSerializer to just set the version to `v1` since it only handles the v1 format.

* fixup! Populate MetricsDeclarationBuilder.Version

* fixup! Populate MetricsDeclarationBuilder.Version
  • Loading branch information
adamconnelly authored and tomkerkhove committed Aug 17, 2019
1 parent b437697 commit 6d67d5a
Show file tree
Hide file tree
Showing 73 changed files with 840 additions and 564 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace Promitor.Core.Scraping.Configuration.Model.Metrics
{
/// <summary>
/// Describes a resource in Azure that can be scraped. Inheriting classes can add whatever
/// additional information is required to scrape a particular Azure resource.
/// </summary>
public abstract class AzureResourceDefinition
{
protected AzureResourceDefinition(ResourceType resourceType, string resourceGroupName)
{
ResourceType = resourceType;
ResourceGroupName = resourceGroupName;
}

/// <summary>
/// Type of resource that is configured
/// </summary>
public ResourceType ResourceType { get; }

/// <summary>
/// Specify a resource group to scrape that defers from the default resource group.
/// This enables you to do multi-resource group scraping with one configuration file.
/// </summary>
public string ResourceGroupName { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,23 @@

namespace Promitor.Core.Scraping.Configuration.Model.Metrics
{
public abstract class MetricDefinition
public class MetricDefinition
{
protected MetricDefinition()
public MetricDefinition()
{
}

protected MetricDefinition(string name,
string description,
string resourceGroupName,
Dictionary<string, string> labels,
Scraping scraping, AzureMetricConfiguration azureMetricConfiguration)
public MetricDefinition(PrometheusMetricDefinition prometheusMetricDefinition,
Scraping scraping,
AzureMetricConfiguration azureMetricConfiguration,
ResourceType resourceType,
List<AzureResourceDefinition> resources)
{
AzureMetricConfiguration = azureMetricConfiguration;
Description = description;
Name = name;
ResourceGroupName = resourceGroupName;
Labels = labels;
PrometheusMetricDefinition = prometheusMetricDefinition;
Scraping = scraping;
ResourceType = resourceType;
Resources = resources;
}

/// <summary>
Expand All @@ -28,34 +27,39 @@ protected MetricDefinition(string name,
public AzureMetricConfiguration AzureMetricConfiguration { get; set; }

/// <summary>
/// Description concerning metric that will be made available in the scraping endpoint
/// The details of the prometheus metric that will be created.
/// </summary>
public string Description { get; set; }
public PrometheusMetricDefinition PrometheusMetricDefinition { get; set; }

/// <summary>
/// Name of the metric to use when exposing in the scraping endpoint
/// Gets or sets the scraping model.
/// </summary>
public string Name { get; set; }
public Scraping Scraping { get; set; } = new Scraping();

/// <summary>
/// Specify a resource group to scrape that defers from the default resource group.
/// This enables you to do multi-resource group scraping with one configuration file.
/// Type of resource that is configured
/// </summary>
public string ResourceGroupName { get; set; }
public ResourceType ResourceType { get; set; }

/// <summary>
/// Type of resource that is configured
/// Gets or sets the list of resources to scrape.
/// </summary>
public abstract ResourceType ResourceType { get; }
public List<AzureResourceDefinition> Resources { get; set; }

/// <summary>
/// Collection of custom labels to add to every metric
/// Creates a <see cref="ScrapeDefinition{TResourceDefinition}"/> object for the specified resource.
/// </summary>
public Dictionary<string, string> Labels { get; set; } = new Dictionary<string, string>();

/// <summary>
/// Gets or sets the scraping model.
/// </summary>
public Scraping Scraping { get; set; } = new Scraping();
/// <param name="resource">The resource to scrape.</param>
/// <param name="azureMetadata">The Azure global metadata.</param>
/// <returns>The scrape definition.</returns>
public ScrapeDefinition<AzureResourceDefinition> CreateScrapeDefinition(AzureResourceDefinition resource, AzureMetadata azureMetadata)
{
return new ScrapeDefinition<AzureResourceDefinition>(
AzureMetricConfiguration,
PrometheusMetricDefinition,
Scraping,
resource,
string.IsNullOrEmpty(resource.ResourceGroupName) ? azureMetadata.ResourceGroupName : resource.ResourceGroupName);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System.Collections.Generic;
using GuardNet;

namespace Promitor.Core.Scraping.Configuration.Model.Metrics
{
/// <summary>
/// Contains the details of the prometheus metric that will be created.
/// </summary>
public class PrometheusMetricDefinition
{
/// <summary>
/// Initializes a new instance of the <see cref="PrometheusMetricDefinition"/> class.
/// </summary>
/// <param name="name">Name of the metric to use when exposing in the scraping endpoint.</param>
/// <param name="description">Description concerning metric that will be made available in the scraping endpoint.</param>
/// <param name="labels">Collection of custom labels to add to every metric.</param>
public PrometheusMetricDefinition(string name, string description, Dictionary<string, string> labels)
{
Guard.NotNull(labels, nameof(labels));

Name = name;
Description = description;
Labels = labels;
}

/// <summary>
/// Name of the metric to use when exposing in the scraping endpoint
/// </summary>
public string Name { get; }

/// <summary>
/// Description concerning metric that will be made available in the scraping endpoint
/// </summary>
public string Description { get; }

/// <summary>
/// Collection of custom labels to add to every metric
/// </summary>
public Dictionary<string, string> Labels { get; }
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Promitor.Core.Scraping.Configuration.Model.Metrics.ResourceTypes
{
public class ContainerInstanceResourceDefinition : AzureResourceDefinition
{
public ContainerInstanceResourceDefinition(string resourceGroupName, string containerGroup)
: base(ResourceType.ContainerInstance, resourceGroupName)
{
ContainerGroup = containerGroup;
}

public string ContainerGroup { get; }
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Promitor.Core.Scraping.Configuration.Model.Metrics.ResourceTypes
{
public class ContainerRegistryResourceDefinition : AzureResourceDefinition
{
public ContainerRegistryResourceDefinition(string resourceGroupName, string registryName)
: base(ResourceType.ContainerRegistry, resourceGroupName)
{
RegistryName = registryName;
}

public string RegistryName { get; }
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Promitor.Core.Scraping.Configuration.Model.Metrics.ResourceTypes
{
public class CosmosDbResourceDefinition : AzureResourceDefinition
{
public CosmosDbResourceDefinition(string resourceGroupName, string dbName)
: base(ResourceType.CosmosDb, resourceGroupName)
{
DbName = dbName;
}

public string DbName { get; }
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Promitor.Core.Scraping.Configuration.Model.Metrics.ResourceTypes
{
public class GenericAzureResourceDefinition : AzureResourceDefinition
{
public GenericAzureResourceDefinition(string resourceGroupName, string filter, string resourceUri)
: base(ResourceType.Generic, resourceGroupName)
{
Filter = filter;
ResourceUri = resourceUri;
}

public string Filter { get; }
public string ResourceUri { get; }
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Promitor.Core.Scraping.Configuration.Model.Metrics.ResourceTypes
{
public class NetworkInterfaceResourceDefinition : AzureResourceDefinition
{
public NetworkInterfaceResourceDefinition(string resourceGroupName, string networkInterfaceName)
: base(ResourceType.NetworkInterface, resourceGroupName)
{
NetworkInterfaceName = networkInterfaceName;
}

public string NetworkInterfaceName { get; }
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Promitor.Core.Scraping.Configuration.Model.Metrics.ResourceTypes
{
public class PostgreSqlResourceDefinition : AzureResourceDefinition
{
public PostgreSqlResourceDefinition(string resourceGroupName, string serverName)
: base(ResourceType.PostgreSql, resourceGroupName)
{
ServerName = serverName;
}

public string ServerName { get; }
}
}
Loading

0 comments on commit 6d67d5a

Please sign in to comment.