Skip to content

Commit

Permalink
Adding scraping interval to metricDefaults and metrics (#456)
Browse files Browse the repository at this point in the history
* Adding **configuration** support for metricDefaults.scraping.inteval

fixes #259

* Adding **configuration** support for <metric>.scraping.interval

Fixes #258

* unit tests for scraping interval config params

* adding .orig to ignored extensions due to merge conflict resolution for most merge tools

* Moving ScrapingInterval -> Scraping model + Interval property so serialize/deserialize matches the YAML definition
  • Loading branch information
brandonh-msft authored and brusMX committed Mar 27, 2019
1 parent 71244fc commit a903949
Show file tree
Hide file tree
Showing 12 changed files with 135 additions and 42 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -293,3 +293,4 @@ __pycache__/
# Local Jekyll output
_site/*
docs/_site/*
*.orig
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
public class MetricDefaults
{
public Aggregation Aggregation { get; set; } = new Aggregation();

public Scraping Scraping { get; set; } = new Scraping();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,10 @@ public abstract class MetricDefinition
/// Type of resource that is configured
/// </summary>
public abstract ResourceType ResourceType { get; }

/// <summary>
/// Gets or sets the scraping model.
/// </summary>
public Scraping Scraping { get; set; } = new Scraping();
}
}
9 changes: 9 additions & 0 deletions src/Promitor.Core.Scraping/Configuration/Model/Scraping.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace Promitor.Core.Scraping.Configuration.Model
{
public class Scraping
{
public TimeSpan? Interval { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using GuardNet;
using System;
using System.Collections.Generic;
using GuardNet;
using Microsoft.Extensions.Logging;
using Promitor.Core.Scraping.Configuration.Model;
using YamlDotNet.RepresentationModel;
Expand All @@ -15,18 +17,37 @@ internal override MetricDefaults Deserialize(YamlMappingNode node)
{
Guard.NotNull(node, nameof(node));

Aggregation aggregation = null;
var metricDefaults = new MetricDefaults();

if (node.Children.ContainsKey("aggregation"))
{
var metricDefaultsNode = (YamlMappingNode) node.Children[new YamlScalarNode("aggregation")];
var metricDefaultsNode = (YamlMappingNode)node.Children[new YamlScalarNode("aggregation")];
var metricDefaultsSerializer = new AggregationDeserializer(Logger);
aggregation = metricDefaultsSerializer.Deserialize(metricDefaultsNode);
var aggregation = metricDefaultsSerializer.Deserialize(metricDefaultsNode);
metricDefaults.Aggregation = aggregation;
}

return new MetricDefaults
if (node.Children.ContainsKey(@"scraping"))
{
Aggregation = aggregation
};
var scrapingNode = (YamlMappingNode)node.Children[new YamlScalarNode(@"scraping")];
try
{
var scrapingIntervalNode = scrapingNode.Children[new YamlScalarNode(@"interval")];

if (scrapingIntervalNode != null)
{
var scrapingIntervalTimeSpan = TimeSpan.Parse(scrapingIntervalNode.ToString());
metricDefaults.Scraping.Interval = scrapingIntervalTimeSpan;
}
}
catch (KeyNotFoundException)
{
// happens when the YAML doesn't have the properties in it which is fine because the object
// will get a default interval of 'null'
}
}

return metricDefaults;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using GuardNet;
using System;
using System.Collections.Generic;
using GuardNet;
using Microsoft.Extensions.Logging;
using Promitor.Core.Scraping.Configuration.Model.Metrics;
using YamlDotNet.RepresentationModel;
Expand Down Expand Up @@ -38,6 +40,25 @@ protected virtual TMetricDefinition DeserializeMetricDefinition<TMetricDefinitio
AzureMetricConfiguration = azureMetricConfiguration
};

if (metricNode.Children.ContainsKey(@"scraping"))
{
var scrapingNode = (YamlMappingNode)metricNode.Children[new YamlScalarNode(@"scraping")];
try
{
var scrapingIntervalNode = scrapingNode?.Children[new YamlScalarNode(@"interval")];

if (scrapingIntervalNode != null)
{
metricDefinition.Scraping.Interval = TimeSpan.Parse(scrapingIntervalNode.ToString());
}
}
catch (KeyNotFoundException)
{
// happens when the YAML doesn't have the properties in it which is fine because the object
// will get a default interval of 'null'
}
}

return metricDefinition;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ namespace Promitor.Scraper.Tests.Unit.Serialization.MetricsDeclaration
public class MetricsDeclarationWithAzureStorageQueueYamlSerializationTests : YamlSerializationTests<StorageQueueMetricDefinition>
{
[Theory]
[InlineData("promitor1")]
[InlineData(null)]
public void YamlSerialization_SerializeAndDeserializeValidConfigForAzureStorageQueue_SucceedsWithIdenticalOutput(string resourceGroupName)
[InlineData("promitor1", @"01:00", @"2:00")]
[InlineData(null, null, null)]
public void YamlSerialization_SerializeAndDeserializeValidConfigForAzureStorageQueue_SucceedsWithIdenticalOutput(string resourceGroupName, string defaultScrapingInterval, string metricScrapingInterval)
{
// Arrange
var azureMetadata = GenerateBogusAzureMetadata();
var azureStorageQueueMetricDefinition = GenerateBogusAzureStorageQueueMetricDefinition(resourceGroupName);
var metricDefaults = GenerateBogusMetricDefaults();
var azureStorageQueueMetricDefinition = GenerateBogusAzureStorageQueueMetricDefinition(resourceGroupName, metricScrapingInterval);
var metricDefaults = GenerateBogusMetricDefaults(defaultScrapingInterval);
var scrapingConfiguration = new Core.Scraping.Configuration.Model.MetricsDeclaration
{
AzureMetadata = azureMetadata,
Expand Down Expand Up @@ -64,9 +64,11 @@ private static void AssertAzureStorageQueueMetricDefinition(StorageQueueMetricDe
Assert.Equal(serviceBusMetricDefinition.AzureMetricConfiguration.Aggregation.Interval, deserializedMetricDefinition.AzureMetricConfiguration.Aggregation.Interval);
}

private StorageQueueMetricDefinition GenerateBogusAzureStorageQueueMetricDefinition(string resourceGroupName)
private StorageQueueMetricDefinition GenerateBogusAzureStorageQueueMetricDefinition(string resourceGroupName, string metricScrapingInterval)
{
var bogusScrapingInterval = GenerateBogusScrapingInterval(metricScrapingInterval);
var bogusAzureMetricConfiguration = GenerateBogusAzureMetricConfiguration();

var bogusGenerator = new Faker<StorageQueueMetricDefinition>()
.StrictMode(ensureRulesForAllProperties: true)
.RuleFor(metricDefinition => metricDefinition.Name, faker => faker.Name.FirstName())
Expand All @@ -77,6 +79,7 @@ private StorageQueueMetricDefinition GenerateBogusAzureStorageQueueMetricDefinit
.RuleFor(metricDefinition => metricDefinition.SasToken, faker => $"?sig={Base64UrlEncoder.Encode(faker.Lorem.Sentence(wordCount: 3))}")
.RuleFor(metricDefinition => metricDefinition.AzureMetricConfiguration, faker => bogusAzureMetricConfiguration)
.RuleFor(metricDefinition => metricDefinition.ResourceGroupName, faker => resourceGroupName)
.RuleFor(metricDefinition => metricDefinition.Scraping, faker => bogusScrapingInterval)
.Ignore(metricDefinition => metricDefinition.ResourceGroupName);

return bogusGenerator.Generate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ namespace Promitor.Scraper.Tests.Unit.Serialization.MetricsDeclaration
public class MetricsDeclarationWithContainerInstanceYamlSerializationTests : YamlSerializationTests<ContainerInstanceMetricDefinition>
{
[Theory]
[InlineData("promitor1")]
[InlineData(null)]
public void YamlSerialization_SerializeAndDeserializeValidConfigForContainerInstance_SucceedsWithIdenticalOutput(string resourceGroupName)
[InlineData("promitor1", @"01:00", @"2:00")]
[InlineData(null, null, null)]
public void YamlSerialization_SerializeAndDeserializeValidConfigForContainerInstance_SucceedsWithIdenticalOutput(string resourceGroupName, string defaultScrapingInterval, string metricScrapingInterval)
{
// Arrange
var azureMetadata = GenerateBogusAzureMetadata();
var containerInstanceMetricDefinition = GenerateBogusContainerInstanceMetricDefinition(resourceGroupName);
var metricDefaults = GenerateBogusMetricDefaults();
var containerInstanceMetricDefinition = GenerateBogusContainerInstanceMetricDefinition(resourceGroupName, metricScrapingInterval);
var metricDefaults = GenerateBogusMetricDefaults(defaultScrapingInterval);
var scrapingConfiguration = new Core.Scraping.Configuration.Model.MetricsDeclaration
{
AzureMetadata = azureMetadata,
Expand Down Expand Up @@ -60,9 +60,11 @@ private static void AssertContainerInstanceMetricDefinition(ContainerInstanceMet
Assert.Equal(containerInstanceMetricDefinition.AzureMetricConfiguration.Aggregation.Type, deserializedMetricDefinition.AzureMetricConfiguration.Aggregation.Type);
Assert.Equal(containerInstanceMetricDefinition.AzureMetricConfiguration.Aggregation.Interval, deserializedMetricDefinition.AzureMetricConfiguration.Aggregation.Interval);
}
private ContainerInstanceMetricDefinition GenerateBogusContainerInstanceMetricDefinition(string resourceGroupName)
private ContainerInstanceMetricDefinition GenerateBogusContainerInstanceMetricDefinition(string resourceGroupName, string metricScrapingInterval)
{
var bogusScrapingInterval = GenerateBogusScrapingInterval(metricScrapingInterval);
var bogusAzureMetricConfiguration = GenerateBogusAzureMetricConfiguration();

var bogusGenerator = new Faker<ContainerInstanceMetricDefinition>()
.StrictMode(ensureRulesForAllProperties: true)
.RuleFor(metricDefinition => metricDefinition.Name, faker => faker.Name.FirstName())
Expand All @@ -71,6 +73,7 @@ private ContainerInstanceMetricDefinition GenerateBogusContainerInstanceMetricDe
.RuleFor(metricDefinition => metricDefinition.ContainerGroup, faker => faker.Name.LastName())
.RuleFor(metricDefinition => metricDefinition.AzureMetricConfiguration, faker => bogusAzureMetricConfiguration)
.RuleFor(metricDefinition => metricDefinition.ResourceGroupName, faker => resourceGroupName)
.RuleFor(metricDefinition => metricDefinition.Scraping, faker => bogusScrapingInterval)
.Ignore(metricDefinition => metricDefinition.ResourceGroupName);

return bogusGenerator.Generate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ namespace Promitor.Scraper.Tests.Unit.Serialization.MetricsDeclaration
public class MetricsDeclarationWithContainerRegistryYamlSerializationTests : YamlSerializationTests<ContainerRegistryMetricDefinition>
{
[Theory]
[InlineData("promitor1")]
[InlineData(null)]
public void YamlSerialization_SerializeAndDeserializeValidConfigForContainerRegistry_SucceedsWithIdenticalOutput(string resourceGroupName)
[InlineData("promitor1", @"01:00", @"2:00")]
[InlineData(null, null, null)]
public void YamlSerialization_SerializeAndDeserializeValidConfigForContainerRegistry_SucceedsWithIdenticalOutput(string resourceGroupName, string defaultScrapingInterval, string metricScrapingInterval)
{
// Arrange
var azureMetadata = GenerateBogusAzureMetadata();
var containerRegistryMetricDefinition = GenerateBogusContainerRegistryMetricDefinition(resourceGroupName);
var metricDefaults = GenerateBogusMetricDefaults();
var containerRegistryMetricDefinition = GenerateBogusContainerRegistryMetricDefinition(resourceGroupName, metricScrapingInterval);
var metricDefaults = GenerateBogusMetricDefaults(defaultScrapingInterval);
var scrapingConfiguration = new Core.Scraping.Configuration.Model.MetricsDeclaration
{
AzureMetadata = azureMetadata,
Expand Down Expand Up @@ -56,9 +56,11 @@ private static void AssertContainerRegistryMetricDefinition(ContainerRegistryMet
Assert.Equal(serviceBusMetricDefinition.RegistryName, deserializedServiceBusMetricDefinition.RegistryName);
}

private ContainerRegistryMetricDefinition GenerateBogusContainerRegistryMetricDefinition(string resourceGroupName)
private ContainerRegistryMetricDefinition GenerateBogusContainerRegistryMetricDefinition(string resourceGroupName, string metricScrapingInterval)
{
var bogusScrapingInterval = GenerateBogusScrapingInterval(metricScrapingInterval);
var bogusAzureMetricConfiguration = GenerateBogusAzureMetricConfiguration();

var bogusGenerator = new Faker<ContainerRegistryMetricDefinition>()
.StrictMode(ensureRulesForAllProperties: true)
.RuleFor(metricDefinition => metricDefinition.Name, faker => faker.Name.FirstName())
Expand All @@ -67,6 +69,7 @@ private ContainerRegistryMetricDefinition GenerateBogusContainerRegistryMetricDe
.RuleFor(metricDefinition => metricDefinition.RegistryName, faker => faker.Name.LastName())
.RuleFor(metricDefinition => metricDefinition.AzureMetricConfiguration, faker => bogusAzureMetricConfiguration)
.RuleFor(metricDefinition => metricDefinition.ResourceGroupName, faker => resourceGroupName)
.RuleFor(metricDefinition => metricDefinition.Scraping, faker => bogusScrapingInterval)
.Ignore(metricDefinition => metricDefinition.ResourceGroupName);

return bogusGenerator.Generate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ namespace Promitor.Scraper.Tests.Unit.Serialization.MetricsDeclaration
public class ServiceBusQueueYamlSerializationTests : YamlSerializationTests<ServiceBusQueueMetricDefinition>
{
[Theory]
[InlineData("promitor1")]
[InlineData(null)]
public void YamlSerialization_SerializeAndDeserializeValidConfigForServiceBus_SucceedsWithIdenticalOutput(string resourceGroupName)
[InlineData("promitor1", @"01:00", @"2:00")]
[InlineData(null, null, null)]
public void YamlSerialization_SerializeAndDeserializeValidConfigForServiceBus_SucceedsWithIdenticalOutput(string resourceGroupName, string defaultScrapingInterval, string metricScrapingInterval)
{
// Arrange
var azureMetadata = GenerateBogusAzureMetadata();
var serviceBusMetricDefinition = GenerateBogusServiceBusMetricDefinition(resourceGroupName);
var metricDefaults = GenerateBogusMetricDefaults();
var serviceBusMetricDefinition = GenerateBogusServiceBusMetricDefinition(resourceGroupName, metricScrapingInterval);
var metricDefaults = GenerateBogusMetricDefaults(defaultScrapingInterval);
var scrapingConfiguration = new Core.Scraping.Configuration.Model.MetricsDeclaration
{
AzureMetadata = azureMetadata,
Expand Down Expand Up @@ -57,9 +57,11 @@ private static void AssertServiceBusQueueMetricDefinition(ServiceBusQueueMetricD
Assert.Equal(serviceBusMetricDefinition.QueueName, deserializedServiceBusMetricDefinition.QueueName);
}

private ServiceBusQueueMetricDefinition GenerateBogusServiceBusMetricDefinition(string resourceGroupName)
private ServiceBusQueueMetricDefinition GenerateBogusServiceBusMetricDefinition(string resourceGroupName, string metricScrapingInterval)
{
var bogusScrapingInterval = GenerateBogusScrapingInterval(metricScrapingInterval);
var bogusAzureMetricConfiguration = GenerateBogusAzureMetricConfiguration();

var bogusGenerator = new Faker<ServiceBusQueueMetricDefinition>()
.StrictMode(ensureRulesForAllProperties: true)
.RuleFor(metricDefinition => metricDefinition.Name, faker => faker.Name.FirstName())
Expand All @@ -69,7 +71,8 @@ private ServiceBusQueueMetricDefinition GenerateBogusServiceBusMetricDefinition(
.RuleFor(metricDefinition => metricDefinition.QueueName, faker => faker.Name.FirstName())
.RuleFor(metricDefinition => metricDefinition.AzureMetricConfiguration, faker => bogusAzureMetricConfiguration)
.RuleFor(metricDefinition => metricDefinition.ResourceGroupName, faker => resourceGroupName)
.Ignore(metricDefinition => metricDefinition.ResourceGroupName);
.RuleFor(metricDefinition => metricDefinition.Scraping, faker => bogusScrapingInterval)
.Ignore(metricDefinition => metricDefinition.ResourceGroupName);

return bogusGenerator.Generate();
}
Expand Down
Loading

0 comments on commit a903949

Please sign in to comment.