-
-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Metric deserialization refactor (#439)
Fixes #416 ## Proposed Changes The approach here is to move the deserializing of metric configuration from the Promitor YAML file in to a more decomposed approach. This allows for new metrics & scrapers to be added without risking merge conflicts as changes are isolated to specific files only pertaining to the metrics and deserialization thereof; not within a host of methods inside one specific file. Documentation for 'adding a scraper' has also been updated to be more descriptive & prescriptive on the metric deserialization procedure when new metrics are added.
- Loading branch information
1 parent
7a2ac8f
commit 2c72d10
Showing
26 changed files
with
208 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
.../ResourceTypes/GenericMetricDefinition.cs → ...urceTypes/GenericAzureMetricDefinition.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
namespace Promitor.Core.Scraping.Configuration.Model.Metrics.ResourceTypes | ||
{ | ||
public class GenericMetricDefinition : MetricDefinition | ||
public class GenericAzureMetricDefinition : MetricDefinition | ||
{ | ||
public string Filter { get; set; } | ||
public override ResourceType ResourceType { get; set; } = ResourceType.Generic; | ||
public override ResourceType ResourceType { get; } = ResourceType.Generic; | ||
public string ResourceUri { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...r.Core.Scraping/Configuration/Model/Metrics/ResourceTypes/StorageQueueMetricDefinition.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
namespace Promitor.Core.Scraping.Configuration.Model.Metrics.ResourceTypes | ||
{ | ||
public class StorageQueueMetricDefinition: MetricDefinition | ||
public class StorageQueueMetricDefinition : MetricDefinition | ||
{ | ||
public string AccountName { get; set; } | ||
public string QueueName { get; set; } | ||
public string SasToken { get; set; } | ||
public override ResourceType ResourceType { get; set; } = ResourceType.StorageQueue; | ||
public override ResourceType ResourceType { get; } = ResourceType.StorageQueue; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/Promitor.Core.Scraping/Configuration/Serialization/Core/MetricDeserializer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using GuardNet; | ||
using Microsoft.Extensions.Logging; | ||
using Promitor.Core.Scraping.Configuration.Model.Metrics; | ||
using YamlDotNet.RepresentationModel; | ||
|
||
namespace Promitor.Core.Scraping.Configuration.Serialization.Core | ||
{ | ||
internal abstract class MetricDeserializer | ||
{ | ||
protected ILogger Logger { get; private set; } | ||
|
||
internal MetricDeserializer WithLogger(ILogger logger) | ||
{ | ||
Guard.NotNull(logger, nameof(logger)); | ||
|
||
Logger = logger; | ||
return this; | ||
} | ||
|
||
internal abstract MetricDefinition Deserialize(YamlMappingNode metricNode); | ||
|
||
protected virtual TMetricDefinition DeserializeMetricDefinition<TMetricDefinition>(YamlMappingNode metricNode) | ||
where TMetricDefinition : MetricDefinition, new() | ||
{ | ||
Guard.NotNull(metricNode, nameof(metricNode)); | ||
|
||
var name = metricNode.Children[new YamlScalarNode("name")]; | ||
var description = metricNode.Children[new YamlScalarNode("description")]; | ||
var azureMetricConfigurationNode = (YamlMappingNode)metricNode.Children[new YamlScalarNode("azureMetricConfiguration")]; | ||
|
||
var azureMetricConfigurationDeserializer = new AzureMetricConfigurationDeserializer(Logger); | ||
var azureMetricConfiguration = azureMetricConfigurationDeserializer.Deserialize(azureMetricConfigurationNode); | ||
|
||
var metricDefinition = new TMetricDefinition | ||
{ | ||
Name = name?.ToString(), | ||
Description = description?.ToString(), | ||
AzureMetricConfiguration = azureMetricConfiguration | ||
}; | ||
|
||
return metricDefinition; | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/Promitor.Core.Scraping/Configuration/Serialization/Core/MetricsDeserializer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using System; | ||
using Microsoft.Extensions.Logging; | ||
using Promitor.Core.Scraping.Configuration.Model; | ||
using Promitor.Core.Scraping.Configuration.Model.Metrics; | ||
using Promitor.Core.Scraping.Factories; | ||
using YamlDotNet.RepresentationModel; | ||
|
||
namespace Promitor.Core.Scraping.Configuration.Serialization.Core | ||
{ | ||
internal class MetricsDeserializer : Deserializer<MetricDefinition> | ||
{ | ||
internal MetricsDeserializer(ILogger logger) : base(logger) | ||
{ | ||
} | ||
|
||
internal override MetricDefinition Deserialize(YamlMappingNode node) | ||
{ | ||
var rawResourceType = node.Children[new YamlScalarNode("resourceType")]; | ||
|
||
if (!Enum.TryParse<ResourceType>(rawResourceType.ToString(), out var resourceType)) | ||
{ | ||
throw new ArgumentException($@"Unknown 'resourceType' value in metric configuration: {rawResourceType}"); | ||
} | ||
|
||
return MetricDeserializerFactory | ||
.GetDeserializerFor(resourceType) | ||
.WithLogger(Logger) | ||
.Deserialize(node); | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...Core.Scraping/Configuration/Serialization/Deserializers/GenericAzureMetricDeserializer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using Promitor.Core.Scraping.Configuration.Model.Metrics; | ||
using Promitor.Core.Scraping.Configuration.Model.Metrics.ResourceTypes; | ||
using Promitor.Core.Scraping.Configuration.Serialization.Core; | ||
using YamlDotNet.RepresentationModel; | ||
|
||
namespace Promitor.Core.Scraping.Configuration.Serialization.Deserializers | ||
{ | ||
internal class GenericAzureMetricDeserializer : MetricDeserializer | ||
{ | ||
/// <summary>Deserializes the specified Generic Azure metric node from the YAML configuration file.</summary> | ||
/// <param name="metricNode">The metric node containing 'filter' and 'resourceUri' parameters pointing to an arbitrary Azure resource</param> | ||
/// <returns>A new <see cref="MetricDefinition"/> object (strongly typed as a <see cref="GenericAzureMetricDefinition"/>) </returns> | ||
internal override MetricDefinition Deserialize(YamlMappingNode metricNode) | ||
{ | ||
var metricDefinition = base.DeserializeMetricDefinition<GenericAzureMetricDefinition>(metricNode); | ||
|
||
if (metricNode.Children.TryGetValue(new YamlScalarNode("filter"), out var filterNode)) | ||
{ | ||
metricDefinition.Filter = filterNode?.ToString(); | ||
} | ||
|
||
var resourceUri = metricNode.Children[new YamlScalarNode("resourceUri")]; | ||
|
||
metricDefinition.ResourceUri = resourceUri?.ToString(); | ||
|
||
return metricDefinition; | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...e.Scraping/Configuration/Serialization/Deserializers/ServiceBusQueueMetricDeserializer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using Promitor.Core.Scraping.Configuration.Model.Metrics; | ||
using Promitor.Core.Scraping.Configuration.Model.Metrics.ResourceTypes; | ||
using YamlDotNet.RepresentationModel; | ||
|
||
namespace Promitor.Core.Scraping.Configuration.Serialization.Deserializers | ||
{ | ||
internal class ServiceBusQueueMetricDeserializer : GenericAzureMetricDeserializer | ||
{ | ||
/// <summary>Deserializes the specified Service Bus Queue metric node from the YAML configuration file.</summary> | ||
/// <param name="metricNode">The metric node containing 'queueName' and 'namespace' parameters pointing to an instance of a Service Bus queue</param> | ||
/// <returns>A new <see cref="MetricDefinition"/> object (strongly typed as a <see cref="ServiceBusQueueMetricDefinition"/>) </returns> | ||
internal override MetricDefinition Deserialize(YamlMappingNode metricNode) | ||
{ | ||
var metricDefinition = base.DeserializeMetricDefinition<ServiceBusQueueMetricDefinition>(metricNode); | ||
|
||
var queueName = metricNode.Children[new YamlScalarNode("queueName")]; | ||
var namespaceName = metricNode.Children[new YamlScalarNode("namespace")]; | ||
|
||
metricDefinition.QueueName = queueName?.ToString(); | ||
metricDefinition.Namespace = namespaceName?.ToString(); | ||
|
||
return metricDefinition; | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...Core.Scraping/Configuration/Serialization/Deserializers/StorageQueueMetricDeserializer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using Promitor.Core.Scraping.Configuration.Model.Metrics; | ||
using Promitor.Core.Scraping.Configuration.Model.Metrics.ResourceTypes; | ||
using YamlDotNet.RepresentationModel; | ||
|
||
namespace Promitor.Core.Scraping.Configuration.Serialization.Deserializers | ||
{ | ||
internal class StorageQueueMetricDeserializer : GenericAzureMetricDeserializer | ||
{ | ||
/// <summary>Deserializes the specified Storage Queue metric node from the YAML configuration file.</summary> | ||
/// <param name="metricNode">The metric node containing 'accountName', 'queueName', and 'sasToken' parameters pointing to an instance of a Storage queue</param> | ||
/// <returns>A new <see cref="MetricDefinition"/> object (strongly typed as a <see cref="StorageQueueMetricDefinition"/>) </returns> | ||
internal override MetricDefinition Deserialize(YamlMappingNode metricNode) | ||
{ | ||
var metricDefinition = base.DeserializeMetricDefinition<StorageQueueMetricDefinition>(metricNode); | ||
var accountName = metricNode.Children[new YamlScalarNode("accountName")]; | ||
var queueName = metricNode.Children[new YamlScalarNode("queueName")]; | ||
var sasToken = metricNode.Children[new YamlScalarNode("sasToken")]; | ||
|
||
metricDefinition.AccountName = accountName?.ToString(); | ||
metricDefinition.QueueName = queueName?.ToString(); | ||
metricDefinition.SasToken = sasToken?.ToString(); | ||
|
||
return metricDefinition; | ||
} | ||
} | ||
} |
Oops, something went wrong.