Skip to content

Commit

Permalink
Simplify how we use validation (#1148)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomkerkhove authored Jul 6, 2020
1 parent 98d29d2 commit f06ea1c
Show file tree
Hide file tree
Showing 45 changed files with 320 additions and 314 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Promitor.Agents.Scraper.Validation.Interfaces
namespace Promitor.Agents.Core.Validation.Interfaces
{
public interface IValidationStep
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,41 +1,21 @@
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Promitor.Agents.Scraper.Configuration;
using Promitor.Core.Scraping.Configuration.Providers.Interfaces;
using Promitor.Agents.Scraper.Validation.Interfaces;
using Promitor.Agents.Scraper.Validation.Steps;
using Promitor.Agents.Scraper.Validation.Steps.Sinks;
using Promitor.Core.Scraping.Configuration.Runtime;
using Promitor.Agents.Core.Validation.Interfaces;

#pragma warning disable 618
namespace Promitor.Agents.Scraper.Validation
namespace Promitor.Agents.Core.Validation
{
public class RuntimeValidator
{
private readonly ILogger _validationLogger;
private readonly List<IValidationStep> _validationSteps;

public RuntimeValidator(
IOptions<ScraperRuntimeConfiguration> runtimeConfiguration,
IOptions<MetricsConfiguration> metricsConfiguration,
ILogger<RuntimeValidator> validatorLogger,
IMetricsDeclarationProvider scrapeConfigurationProvider,
IConfiguration configuration)
public RuntimeValidator(IEnumerable<IValidationStep> validationSteps,
ILogger<RuntimeValidator> validatorLogger)
{
_validationLogger = validatorLogger;

_validationSteps = new List<IValidationStep>
{
new ConfigurationPathValidationStep(metricsConfiguration, _validationLogger),
new AzureAuthenticationValidationStep(configuration, _validationLogger),
new MetricsDeclarationValidationStep(scrapeConfigurationProvider, _validationLogger),
new ResourceDiscoveryValidationStep(runtimeConfiguration.Value.ResourceDiscovery, scrapeConfigurationProvider, _validationLogger),
new StatsDMetricSinkValidationStep(runtimeConfiguration, _validationLogger),
new PrometheusScrapingEndpointMetricSinkValidationStep(runtimeConfiguration, _validationLogger)
};
_validationSteps = validationSteps.ToList();
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Microsoft.Extensions.Logging;

namespace Promitor.Agents.Scraper.Validation.Steps
namespace Promitor.Agents.Core.Validation.Steps
{
public class ValidationStep
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Collections.Generic;
using System.Text;

namespace Promitor.Agents.Scraper.Validation
namespace Promitor.Agents.Core.Validation
{
public class ValidationResult
{
Expand Down
13 changes: 5 additions & 8 deletions src/Promitor.Agents.Scraper/Docs/Open-Api.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using Promitor.Agents.Core.Configuration.Server;
using Promitor.Agents.Core.Configuration.Telemetry;
using Promitor.Agents.Core.Configuration.Telemetry.Sinks;
using Promitor.Agents.Core.Validation;
using Promitor.Agents.Core.Validation.Interfaces;
using Promitor.Agents.Scraper;
using Promitor.Agents.Scraper.Configuration;
using Promitor.Agents.Scraper.Configuration.Sinks;
Expand All @@ -15,7 +17,8 @@
using Promitor.Core.Scraping.Configuration.Serialization.v1.Core;
using Promitor.Core.Scraping.Configuration.Serialization.v1.Model;
using Promitor.Core.Scraping.Factories;
using Promitor.Agents.Scraper.Validation;
using Promitor.Agents.Scraper.Validation.Steps;
using Promitor.Agents.Scraper.Validation.Steps.Sinks;
using Promitor.Core.Metrics;
using Promitor.Core.Metrics.Sinks;
using Promitor.Core.Scraping.Configuration.Runtime;
Expand All @@ -42,7 +45,6 @@ public static IServiceCollection DefineDependencies(this IServiceCollection serv
services.AddTransient<IMetricsDeclarationProvider, MetricsDeclarationProvider>();
services.AddTransient<IRuntimeMetricsCollector, RuntimeMetricsCollector>();
services.AddTransient<MetricScraperFactory>();
services.AddTransient<RuntimeValidator>();
services.AddTransient<ConfigurationSerializer>();
services.AddSingleton<AzureMonitorClientFactory>();

Expand All @@ -61,6 +63,22 @@ public static IServiceCollection DefineDependencies(this IServiceCollection serv

return services;
}
/// <summary>
/// Defines the validation for when Promitor starts up
/// </summary>
/// <param name="services">Collections of services in application</param>
public static IServiceCollection ConfigureValidation(this IServiceCollection services)
{
services.AddTransient<IValidationStep, ConfigurationPathValidationStep>();
services.AddTransient<IValidationStep, AzureAuthenticationValidationStep>();
services.AddTransient<IValidationStep, MetricsDeclarationValidationStep>();
services.AddTransient<IValidationStep, ResourceDiscoveryValidationStep>();
services.AddTransient<IValidationStep, StatsDMetricSinkValidationStep>();
services.AddTransient<IValidationStep, PrometheusScrapingEndpointMetricSinkValidationStep>();
services.AddTransient<RuntimeValidator>();

return services;
}

/// <summary>
/// Adds the required metric sinks
Expand Down
2 changes: 1 addition & 1 deletion src/Promitor.Agents.Scraper/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using Promitor.Agents.Core;
using Promitor.Agents.Core.Configuration.Server;
using Promitor.Agents.Core.Extensions;
using Promitor.Agents.Scraper.Validation;
using Promitor.Agents.Core.Validation;
using Promitor.Core;
using Serilog;

Expand Down
1 change: 1 addition & 0 deletions src/Promitor.Agents.Scraper/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public void ConfigureServices(IServiceCollection services)
.AddHttpCorrelation()
.AddAutoMapper(typeof(V1MappingProfile).Assembly)
.DefineDependencies()
.ConfigureValidation()
.ConfigureYamlConfiguration(Configuration)
.UseOpenApiSpecifications("Promitor - Scraper API v1", openApiDescription, 1);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Promitor.Agents.Core.Validation;

namespace Promitor.Agents.Scraper.Validation.Exceptions
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Promitor.Agents.Core.Validation;
using Promitor.Agents.Core.Validation.Interfaces;
using Promitor.Agents.Core.Validation.Steps;
using Promitor.Core;
using Promitor.Agents.Scraper.Validation.Interfaces;

namespace Promitor.Agents.Scraper.Validation.Steps
{
Expand All @@ -12,11 +13,7 @@ public class AzureAuthenticationValidationStep : ValidationStep, IValidationStep

public string ComponentName { get; } = "Azure Authentication";

public AzureAuthenticationValidationStep(IConfiguration configuration) : this(configuration, NullLogger.Instance)
{
}

public AzureAuthenticationValidationStep(IConfiguration configuration, ILogger logger) : base(logger)
public AzureAuthenticationValidationStep(IConfiguration configuration, ILogger<AzureAuthenticationValidationStep> logger) : base(logger)
{
_configuration = configuration;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using System.IO;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using Promitor.Agents.Scraper.Validation.Interfaces;
using Promitor.Agents.Core.Validation;
using Promitor.Agents.Core.Validation.Interfaces;
using Promitor.Agents.Core.Validation.Steps;
using Promitor.Core.Scraping.Configuration.Runtime;

namespace Promitor.Agents.Scraper.Validation.Steps
Expand All @@ -12,12 +13,7 @@ public class ConfigurationPathValidationStep : ValidationStep, IValidationStep
private readonly IOptions<MetricsConfiguration> _metricsConfiguration;
public string ComponentName { get; } = "Metrics Declaration Path";

public ConfigurationPathValidationStep(IOptions<MetricsConfiguration> metricsConfiguration) : this(metricsConfiguration, NullLogger.Instance)
{
_metricsConfiguration = metricsConfiguration;
}

public ConfigurationPathValidationStep(IOptions<MetricsConfiguration> metricsConfiguration, ILogger logger) : base(logger)
public ConfigurationPathValidationStep(IOptions<MetricsConfiguration> metricsConfiguration, ILogger<ConfigurationPathValidationStep> logger) : base(logger)
{
_metricsConfiguration = metricsConfiguration;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Promitor.Agents.Core.Validation;
using Promitor.Agents.Core.Validation.Interfaces;
using Promitor.Agents.Core.Validation.Steps;
using Promitor.Core.Scraping.Configuration.Model;
using Promitor.Core.Scraping.Configuration.Model.Metrics;
using Promitor.Core.Scraping.Configuration.Providers.Interfaces;
using Promitor.Core.Scraping.Configuration.Serialization;
using Promitor.Core.Serialization.Yaml;
using Promitor.Agents.Scraper.Validation.Interfaces;
using Promitor.Agents.Scraper.Validation.MetricDefinitions;
using Promitor.Core.Contracts.ResourceTypes;

Expand All @@ -17,12 +18,8 @@ namespace Promitor.Agents.Scraper.Validation.Steps
public class MetricsDeclarationValidationStep : ValidationStep, IValidationStep
{
private readonly IMetricsDeclarationProvider _metricsDeclarationProvider;

public MetricsDeclarationValidationStep(IMetricsDeclarationProvider metricsDeclarationProvider) : this(metricsDeclarationProvider, NullLogger.Instance)
{
}

public MetricsDeclarationValidationStep(IMetricsDeclarationProvider metricsDeclarationProvider, ILogger logger) : base( logger)

public MetricsDeclarationValidationStep(IMetricsDeclarationProvider metricsDeclarationProvider, ILogger<MetricsDeclarationValidationStep> logger) : base( logger)
{
_metricsDeclarationProvider = metricsDeclarationProvider;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,33 @@
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using Promitor.Agents.Core.Validation;
using Promitor.Agents.Core.Validation.Interfaces;
using Promitor.Agents.Core.Validation.Steps;
using Promitor.Agents.Scraper.Configuration;
using Promitor.Agents.Scraper.Validation.Interfaces;
using Promitor.Core.Scraping.Configuration.Providers.Interfaces;
using Promitor.Core.Scraping.Configuration.Serialization;

namespace Promitor.Agents.Scraper.Validation.Steps
{
public class ResourceDiscoveryValidationStep : ValidationStep, IValidationStep
{
private const string NoDiscoveryConfiguredError = "Resource discovery groups are defined in your metrics configuration, but resource discovery has not been configured in the runtime configuration. Please add a resource discovery configuration for Promitor Scraper runtime.";
private readonly IOptions<ResourceDiscoveryConfiguration> _resourceDiscoveryConfiguration;
private readonly IMetricsDeclarationProvider _metricsDeclarationProvider;
private readonly ResourceDiscoveryConfiguration _configuration;

public ResourceDiscoveryValidationStep(ResourceDiscoveryConfiguration configuration, IMetricsDeclarationProvider metricsDeclarationProvider) : this(configuration, metricsDeclarationProvider, NullLogger.Instance)
{
}

public ResourceDiscoveryValidationStep(ResourceDiscoveryConfiguration configuration, IMetricsDeclarationProvider metricsDeclarationProvider, ILogger logger) : base( logger)
public ResourceDiscoveryValidationStep(IOptions<ResourceDiscoveryConfiguration> resourceDiscoveryResourceDiscoveryConfiguration, IMetricsDeclarationProvider metricsDeclarationProvider, ILogger<ResourceDiscoveryValidationStep> logger) : base( logger)
{
_metricsDeclarationProvider = metricsDeclarationProvider;
_configuration = configuration;
_resourceDiscoveryConfiguration = resourceDiscoveryResourceDiscoveryConfiguration;
}

public string ComponentName { get; } = "Resource Discovery";

public ValidationResult Run()
{
var doesDeclareResourceDiscoveryGroups = DetermineIfDiscoveryGroupsAreDefined();
if (_configuration == null)
if (_resourceDiscoveryConfiguration == null)
{
if (doesDeclareResourceDiscoveryGroups)
{
Expand All @@ -41,23 +38,22 @@ public ValidationResult Run()
}

var errorMessages = new List<string>();
if (string.IsNullOrWhiteSpace(_configuration.Host))
if (string.IsNullOrWhiteSpace(_resourceDiscoveryConfiguration.Value.Host))
{
errorMessages.Add( "No host name for resource discovery was configured");
}

if (_configuration.Port <= 0)
if (_resourceDiscoveryConfiguration.Value.Port <= 0)
{
errorMessages.Add($"No valid port ({_configuration.Port}) for resource discovery was configured");
errorMessages.Add($"No valid port ({_resourceDiscoveryConfiguration.Value.Port}) for resource discovery was configured");
}

return errorMessages.Any() ? ValidationResult.Failure(ComponentName, errorMessages) : ValidationResult.Successful(ComponentName);
}

private bool DetermineIfDiscoveryGroupsAreDefined()
{
var errorReporter = new ErrorReporter();
var metricsDeclaration = _metricsDeclarationProvider.Get(applyDefaults: true, errorReporter: errorReporter);
var metricsDeclaration = _metricsDeclarationProvider.Get(applyDefaults: true);
return metricsDeclaration.Metrics.Any(metricDefinition => metricDefinition.ResourceDiscoveryGroups?.Count >= 1);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using Promitor.Agents.Core.Validation;
using Promitor.Agents.Core.Validation.Interfaces;
using Promitor.Agents.Core.Validation.Steps;
using Promitor.Agents.Scraper.Configuration;
using Promitor.Agents.Scraper.Validation.Interfaces;

namespace Promitor.Agents.Scraper.Validation.Steps.Sinks
{
public class PrometheusScrapingEndpointMetricSinkValidationStep : ValidationStep, IValidationStep
{
private readonly IOptions<ScraperRuntimeConfiguration> _runtimeConfiguration;

public PrometheusScrapingEndpointMetricSinkValidationStep(IOptions<ScraperRuntimeConfiguration> runtimeConfiguration)
: this(runtimeConfiguration, NullLogger.Instance)
{
}

public PrometheusScrapingEndpointMetricSinkValidationStep(IOptions<ScraperRuntimeConfiguration> runtimeConfiguration, ILogger validationLogger)
public PrometheusScrapingEndpointMetricSinkValidationStep(IOptions<ScraperRuntimeConfiguration> runtimeConfiguration, ILogger<PrometheusScrapingEndpointMetricSinkValidationStep> validationLogger)
: base(validationLogger)
{
_runtimeConfiguration = runtimeConfiguration;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using Promitor.Agents.Core.Validation;
using Promitor.Agents.Core.Validation.Interfaces;
using Promitor.Agents.Core.Validation.Steps;
using Promitor.Agents.Scraper.Configuration;
using Promitor.Agents.Scraper.Validation.Interfaces;

namespace Promitor.Agents.Scraper.Validation.Steps.Sinks
{
Expand All @@ -13,12 +14,7 @@ public class StatsDMetricSinkValidationStep : ValidationStep,
{
private readonly IOptions<ScraperRuntimeConfiguration> _runtimeConfiguration;

public StatsDMetricSinkValidationStep(IOptions<ScraperRuntimeConfiguration> runtimeConfiguration)
: this(runtimeConfiguration, NullLogger.Instance)
{
}

public StatsDMetricSinkValidationStep(IOptions<ScraperRuntimeConfiguration> runtimeConfiguration, ILogger validationLogger)
public StatsDMetricSinkValidationStep(IOptions<ScraperRuntimeConfiguration> runtimeConfiguration, ILogger<StatsDMetricSinkValidationStep> validationLogger)
: base(validationLogger)
{
_runtimeConfiguration = runtimeConfiguration;
Expand Down
Loading

0 comments on commit f06ea1c

Please sign in to comment.