Skip to content

Commit

Permalink
Move featureFlags.disableMetricTimestamps to prometheus.enableMe… (#659)
Browse files Browse the repository at this point in the history
* Move featureFlags.disableMetricTimestamps to prometheus.enableMetricTimestamps

* Remove redundant initializer

Signed-off-by: Tom Kerkhove <[email protected]>

* Fix test

Signed-off-by: Tom Kerkhove <[email protected]>

* Redundant initializer

* Flag when whole config is passed.
  • Loading branch information
tomkerkhove authored Aug 12, 2019
1 parent aa3898c commit f4ce88a
Show file tree
Hide file tree
Showing 16 changed files with 69 additions and 244 deletions.
8 changes: 2 additions & 6 deletions src/Promitor.Core.Configuration/Defaults.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ public static class Server

public static class Prometheus
{
public static string ScrapeEndpointBaseUri { get; } = "/metrics";
public static bool EnableMetricTimestamps { get; set; } = false;
public static double MetricUnavailableValue { get; } = double.NaN;
public static string ScrapeEndpointBaseUri { get; } = "/metrics";
}

public static class MetricsConfiguration
Expand All @@ -36,10 +37,5 @@ public class ApplicationInsights
public static bool IsEnabled { get; set; } = false;
}
}

public static class FeatureFlags
{
public static bool DisableMetricTimestamps { get; set; } = false;
}
}
}

This file was deleted.

7 changes: 0 additions & 7 deletions src/Promitor.Core.Configuration/FeatureFlags/ToggleNames.cs

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ public class PrometheusConfiguration
{
public ScrapeEndpointConfiguration ScrapeEndpoint { get; set; } = new ScrapeEndpointConfiguration();
public double? MetricUnavailableValue { get; set; } = Defaults.Prometheus.MetricUnavailableValue;
public bool EnableMetricTimestamps { get; set; } = Defaults.Prometheus.EnableMetricTimestamps;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Promitor.Core.Configuration.Model.FeatureFlags;
using Promitor.Core.Configuration.Model.Metrics;
using Promitor.Core.Configuration.Model.Metrics;
using Promitor.Core.Configuration.Model.Prometheus;
using Promitor.Core.Configuration.Model.Server;
using Promitor.Core.Configuration.Model.Telemetry;
Expand All @@ -12,6 +11,5 @@ public class RuntimeConfiguration
public PrometheusConfiguration Prometheus { get; set; } = new PrometheusConfiguration();
public MetricsConfiguration MetricsConfiguration { get; set; } = new MetricsConfiguration();
public TelemetryConfiguration Telemetry { get; set; } = new TelemetryConfiguration();
public FeatureFlagsConfiguration FeatureFlags { get; set; } = new FeatureFlagsConfiguration();
}
}
8 changes: 2 additions & 6 deletions src/Promitor.Core.Scraping/Factories/MetricScraperFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using GuardNet;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Promitor.Core.Configuration.FeatureFlags;
using Promitor.Core.Scraping.Configuration.Model;
using Promitor.Core.Scraping.Configuration.Model.Metrics;
using Promitor.Core.Scraping.Interfaces;
Expand All @@ -18,18 +17,15 @@ public class MetricScraperFactory
{
private readonly IExceptionTracker _exceptionTracker;
private readonly IConfiguration _configuration;
private readonly FeatureToggleClient _featureToggleClient;
private readonly ILogger _logger;

public MetricScraperFactory(IConfiguration configuration, FeatureToggleClient featureToggleClient, ILogger logger, IExceptionTracker exceptionTracker)
public MetricScraperFactory(IConfiguration configuration, ILogger logger, IExceptionTracker exceptionTracker)
{
Guard.NotNull(configuration, nameof(configuration));
Guard.NotNull(featureToggleClient, nameof(featureToggleClient));
Guard.NotNull(logger, nameof(logger));
Guard.NotNull(exceptionTracker, nameof(exceptionTracker));

_logger = logger;
_featureToggleClient = featureToggleClient;
_configuration = configuration;
_exceptionTracker = exceptionTracker;
}
Expand All @@ -45,7 +41,7 @@ public IScraper<MetricDefinition> CreateScraper(ResourceType metricDefinitionRes
IPrometheusMetricWriter prometheusMetricWriter, IRuntimeMetricsCollector runtimeMetricsCollector)
{
var azureMonitorClient = CreateAzureMonitorClient(azureMetadata, runtimeMetricsCollector);
var scraperConfiguration = new ScraperConfiguration(azureMetadata, azureMonitorClient, prometheusMetricWriter, _featureToggleClient, _logger, _exceptionTracker);
var scraperConfiguration = new ScraperConfiguration(azureMetadata, azureMonitorClient, prometheusMetricWriter, _logger, _exceptionTracker);

switch (metricDefinitionResourceType)
{
Expand Down
11 changes: 3 additions & 8 deletions src/Promitor.Core.Scraping/Prometheus/PrometheusMetricWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using Microsoft.Extensions.Options;
using Prometheus.Client;
using Promitor.Core.Configuration;
using Promitor.Core.Configuration.FeatureFlags;
using Promitor.Core.Configuration.Model.Prometheus;
using Promitor.Core.Scraping.Configuration.Model.Metrics;
using Promitor.Core.Scraping.Prometheus.Interfaces;
Expand All @@ -14,28 +13,24 @@ namespace Promitor.Core.Scraping.Prometheus
{
public class PrometheusMetricWriter : IPrometheusMetricWriter
{
private readonly FeatureToggleClient _featureToggleClient;
private readonly IOptionsMonitor<PrometheusConfiguration> _prometheusConfiguration;
private readonly ILogger<PrometheusMetricWriter> _logger;

public PrometheusMetricWriter(FeatureToggleClient featureToggleClient, IOptionsMonitor<PrometheusConfiguration> prometheusConfiguration, ILogger<PrometheusMetricWriter> logger)
public PrometheusMetricWriter(IOptionsMonitor<PrometheusConfiguration> prometheusConfiguration, ILogger<PrometheusMetricWriter> logger)
{
Guard.NotNull(featureToggleClient, nameof(featureToggleClient));
Guard.NotNull(prometheusConfiguration, nameof(prometheusConfiguration));
Guard.NotNull(logger, nameof(logger));

_featureToggleClient = featureToggleClient;
_prometheusConfiguration = prometheusConfiguration;
_logger = logger;
}

public void ReportMetric(MetricDefinition metricDefinition, ScrapeResult scrapedMetricResult)
{
var metricsTimestampFeatureFlag = _featureToggleClient.IsActive(ToggleNames.DisableMetricTimestamps, defaultFlagState: true);

var enableMetricTimestamps = _prometheusConfiguration.CurrentValue.EnableMetricTimestamps;
var labels = DetermineLabels(metricDefinition, scrapedMetricResult);

var gauge = Metrics.CreateGauge(metricDefinition.Name, metricDefinition.Description, includeTimestamp: metricsTimestampFeatureFlag, labelNames: labels.Names);
var gauge = Metrics.CreateGauge(metricDefinition.Name, metricDefinition.Description, includeTimestamp: enableMetricTimestamps, labelNames: labels.Names);
var metricValue = DetermineMetricMeasurement(scrapedMetricResult);
gauge.WithLabels(labels.Values).Set(metricValue);
}
Expand Down
11 changes: 1 addition & 10 deletions src/Promitor.Core.Scraping/ScraperConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using GuardNet;
using Microsoft.Extensions.Logging;
using Promitor.Core.Configuration.FeatureFlags;
using Promitor.Core.Scraping.Configuration.Model;
using Promitor.Core.Scraping.Prometheus.Interfaces;
using Promitor.Core.Telemetry.Interfaces;
Expand All @@ -25,11 +24,6 @@ public class ScraperConfiguration
/// </summary>
public IPrometheusMetricWriter PrometheusMetricWriter { get; }

/// <summary>
/// Interaction with feature flags
/// </summary>
public FeatureToggleClient FeatureToggleClient { get; }

/// <summary>
/// Logger used for telemetry
/// </summary>
Expand All @@ -46,22 +40,19 @@ public class ScraperConfiguration
/// <param name="azureMetadata">Metadata concerning the Azure resources</param>
/// <param name="azureMonitorClient">Client to communicate with Azure Monitor</param>
/// <param name="prometheusMetricWriter">Metrics collector for our Prometheus scraping endpoint</param>
/// <param name="featureToggleClient">Interaction with feature flags</param>
/// <param name="logger">General logger</param>
/// <param name="exceptionTracker">Exception tracker</param>
public ScraperConfiguration(AzureMetadata azureMetadata, AzureMonitorClient azureMonitorClient, IPrometheusMetricWriter prometheusMetricWriter, FeatureToggleClient featureToggleClient, ILogger logger, IExceptionTracker exceptionTracker)
public ScraperConfiguration(AzureMetadata azureMetadata, AzureMonitorClient azureMonitorClient, IPrometheusMetricWriter prometheusMetricWriter, ILogger logger, IExceptionTracker exceptionTracker)
{
Guard.NotNull(azureMetadata, nameof(azureMetadata));
Guard.NotNull(azureMonitorClient, nameof(azureMonitorClient));
Guard.NotNull(prometheusMetricWriter, nameof(prometheusMetricWriter));
Guard.NotNull(featureToggleClient, nameof(featureToggleClient));
Guard.NotNull(logger, nameof(logger));
Guard.NotNull(exceptionTracker, nameof(exceptionTracker));

AzureMetadata = azureMetadata;
AzureMonitorClient = azureMonitorClient;
PrometheusMetricWriter = prometheusMetricWriter;
FeatureToggleClient = featureToggleClient;
Logger = logger;
ExceptionTracker = exceptionTracker;
}
Expand Down
13 changes: 7 additions & 6 deletions src/Promitor.Core.Telemetry.Metrics/RuntimeMetricsCollector.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
using System.Collections.Generic;
using System.Linq;
using Promitor.Core.Configuration.FeatureFlags;
using Microsoft.Extensions.Options;
using Promitor.Core.Configuration.Model.Prometheus;
using Promitor.Core.Telemetry.Metrics.Interfaces;

namespace Promitor.Core.Telemetry.Metrics
{
public class RuntimeMetricsCollector : IRuntimeMetricsCollector
{
private readonly FeatureToggleClient _featureToggleClient;
private readonly IOptionsMonitor<PrometheusConfiguration> _prometheusConfiguration;

public RuntimeMetricsCollector(FeatureToggleClient featureToggleClient)
public RuntimeMetricsCollector(IOptionsMonitor<PrometheusConfiguration> prometheusConfiguration)
{
_featureToggleClient = featureToggleClient;
_prometheusConfiguration = prometheusConfiguration;
}

/// <summary>
Expand All @@ -23,9 +24,9 @@ public RuntimeMetricsCollector(FeatureToggleClient featureToggleClient)
/// <param name="labels">Labels that are applicable for this measurement</param>
public void SetGaugeMeasurement(string name, string description, double value, Dictionary<string, string> labels)
{
var metricsTimestampFeatureFlag = _featureToggleClient.IsActive(ToggleNames.DisableMetricTimestamps);
var enableMetricTimestamps = _prometheusConfiguration.CurrentValue.EnableMetricTimestamps;

var gauge = Prometheus.Client.Metrics.CreateGauge($"promitor_{name}", help: description, includeTimestamp: metricsTimestampFeatureFlag, labelNames: labels.Keys.ToArray());
var gauge = Prometheus.Client.Metrics.CreateGauge($"promitor_{name}", help: description, includeTimestamp: enableMetricTimestamps, labelNames: labels.Keys.ToArray());
gauge.WithLabels(labels.Values.ToArray()).Set(value);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
using Microsoft.Extensions.Logging;
using Promitor.Core.Scraping.Configuration.Providers;
using Promitor.Core.Scraping.Configuration.Providers.Interfaces;
using Promitor.Core.Configuration.FeatureFlags;
using Promitor.Core.Configuration.Model.FeatureFlags;
using Promitor.Core.Configuration.Model.Metrics;
using Promitor.Core.Configuration.Model.Prometheus;
using Promitor.Core.Configuration.Model.Server;
Expand Down Expand Up @@ -69,7 +67,6 @@ public static IServiceCollection DefineDependencies(this IServiceCollection serv
services.AddTransient<ILogger, RuntimeLogger>();
services.AddTransient<IMetricsDeclarationProvider, MetricsDeclarationProvider>();
services.AddTransient<IRuntimeMetricsCollector, RuntimeMetricsCollector>();
services.AddTransient<FeatureToggleClient>();
services.AddTransient<MetricScraperFactory>();
services.AddTransient<RuntimeValidator>();
services.AddTransient<ValidationLogger>();
Expand Down Expand Up @@ -110,7 +107,6 @@ public static IServiceCollection UseWebApi(this IServiceCollection services)
/// </summary>
public static IServiceCollection ConfigureYamlConfiguration(this IServiceCollection services, IConfiguration configuration)
{
services.Configure<FeatureFlagsConfiguration>(configuration.GetSection("featureFlags"));
services.Configure<MetricsConfiguration>(configuration.GetSection("metricsConfiguration"));
services.Configure<TelemetryConfiguration>(configuration.GetSection("telemetry"));
services.Configure<ApplicationInsightsConfiguration>(configuration.GetSection("telemetry:applicationInsights"));
Expand Down
Loading

0 comments on commit f4ce88a

Please sign in to comment.