Skip to content

Commit

Permalink
Update dependency Prometheus.Client to v4 (#1224)
Browse files Browse the repository at this point in the history
Co-authored-by: Renovate Bot <[email protected]>
Co-authored-by: Tom Kerkhove <[email protected]>
  • Loading branch information
3 people authored Aug 20, 2020
1 parent bca0f45 commit de5244c
Show file tree
Hide file tree
Showing 7 changed files with 426 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Logging;
using Prometheus.Client;
using Prometheus.Client.Abstractions;
using Promitor.Agents.Core.Configuration.Server;
using Promitor.Agents.Core.Configuration.Telemetry;
using Promitor.Agents.Core.Configuration.Telemetry.Sinks;
Expand Down Expand Up @@ -112,6 +114,7 @@ public static IServiceCollection UseMetricSinks(this IServiceCollection services

private static void AddPrometheusMetricSink(IServiceCollection services)
{
services.AddSingleton<IMetricFactory, MetricFactory>(serviceProvider => new MetricFactory(Metrics.DefaultCollectorRegistry));
services.AddTransient<IMetricSink, PrometheusScrapingEndpointMetricSink>();
}

Expand Down
2 changes: 1 addition & 1 deletion src/Promitor.Core.Scraping/Promitor.Core.Scraping.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<PackageReference Include="Guard.Net" Version="1.2.0" />
<PackageReference Include="Microsoft.ApplicationInsights" Version="2.14.0" />
<PackageReference Include="Microsoft.Azure.Management.Fluent" Version="1.34.0" />
<PackageReference Include="Prometheus.Client" Version="3.1.0" />
<PackageReference Include="Prometheus.Client" Version="4.1.0" />
<PackageReference Include="YamlDotNet" Version="8.1.2" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using GuardNet;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Prometheus.Client;
using Prometheus.Client.Abstractions;
using Promitor.Core;
using Promitor.Core.Metrics;
using Promitor.Core.Metrics.Sinks;
Expand All @@ -16,15 +16,18 @@ namespace Promitor.Integrations.Sinks.Prometheus
{
public class PrometheusScrapingEndpointMetricSink : IMetricSink
{
private readonly IMetricFactory _metricFactory;
private readonly ILogger<PrometheusScrapingEndpointMetricSink> _logger;
private readonly IMetricsDeclarationProvider _metricsDeclarationProvider;
private readonly IOptionsMonitor<PrometheusScrapingEndpointSinkConfiguration> _prometheusConfiguration;

public PrometheusScrapingEndpointMetricSink(IMetricsDeclarationProvider metricsDeclarationProvider, IOptionsMonitor<PrometheusScrapingEndpointSinkConfiguration> prometheusConfiguration, ILogger<PrometheusScrapingEndpointMetricSink> logger)
public PrometheusScrapingEndpointMetricSink(IMetricFactory metricFactory, IMetricsDeclarationProvider metricsDeclarationProvider, IOptionsMonitor<PrometheusScrapingEndpointSinkConfiguration> prometheusConfiguration, ILogger<PrometheusScrapingEndpointMetricSink> logger)
{
Guard.NotNull(metricFactory, nameof(metricFactory));
Guard.NotNull(prometheusConfiguration, nameof(prometheusConfiguration));
Guard.NotNull(logger, nameof(logger));

_metricFactory = metricFactory;
_metricsDeclarationProvider = metricsDeclarationProvider;
_prometheusConfiguration = prometheusConfiguration;
_logger = logger;
Expand Down Expand Up @@ -65,18 +68,24 @@ public Task ReportMetricAsync(string metricName, string metricDescription, doubl
Guard.NotNullOrEmpty(metricName, nameof(metricName));

var enableMetricTimestamps = _prometheusConfiguration.CurrentValue.EnableMetricTimestamps;

var gauge = Metrics.CreateGauge(metricName, metricDescription, includeTimestamp: enableMetricTimestamps, labelNames: labels.Keys.ToArray());
var gauge = CreateGauge(metricName, metricDescription, labels, enableMetricTimestamps);
gauge.WithLabels(labels.Values.ToArray()).Set(metricValue);

_logger.LogTrace("Metric {MetricName} with value {MetricValue} was written to StatsD server", metricName, metricValue);

return Task.CompletedTask;
}

private IMetricFamily<IGauge> CreateGauge(string metricName, string metricDescription, Dictionary<string, string> labels, bool enableMetricTimestamps)
{
var gauge = _metricFactory.CreateGauge(metricName, metricDescription, includeTimestamp: enableMetricTimestamps, labelNames: labels.Keys.ToArray());
return gauge;
}

private Dictionary<string, string> DetermineLabels(PrometheusMetricDefinition metricDefinition, ScrapeResult scrapeResult, MeasuredMetric measuredMetric)
{
var labels = new Dictionary<string, string>(scrapeResult.Labels);
var labels = new Dictionary<string, string>(scrapeResult.Labels.Select(label => new KeyValuePair<string, string>(label.Key.ToLower(), label.Value)));

if (measuredMetric.IsDimensional)
{
Expand All @@ -87,13 +96,14 @@ private Dictionary<string, string> DetermineLabels(PrometheusMetricDefinition me
{
foreach (var customLabel in metricDefinition.Labels)
{
if (labels.ContainsKey(customLabel.Key))
var customLabelKey = customLabel.Key.ToLower();
if (labels.ContainsKey(customLabelKey))
{
_logger.LogWarning("Custom label {CustomLabelName} was already specified with value 'LabelValue' instead of 'CustomLabelValue'. Ignoring...", customLabel.Key, labels[customLabel.Key], customLabel.Value);
_logger.LogWarning("Custom label {CustomLabelName} was already specified with value 'LabelValue' instead of 'CustomLabelValue'. Ignoring...", customLabel.Key, labels[customLabelKey], customLabel.Value);
continue;
}

labels.Add(customLabel.Key, customLabel.Value);
labels.Add(customLabelKey, customLabel.Value);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Prometheus.Client" Version="3.1.0" />
<PackageReference Include="Prometheus.Client" Version="4.1.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
using System.Collections.Generic;
using System.Linq;
using GuardNet;
using Microsoft.Extensions.Options;
using Prometheus.Client.Abstractions;
using Promitor.Core.Metrics;
using Promitor.Integrations.Sinks.Prometheus.Configuration;

namespace Promitor.Integrations.Sinks.Prometheus
{
public class RuntimeMetricsCollector : IRuntimeMetricsCollector
{
private readonly IMetricFactory _metricFactory;
private readonly IOptionsMonitor<PrometheusScrapingEndpointSinkConfiguration> _prometheusConfiguration;

public RuntimeMetricsCollector(IOptionsMonitor<PrometheusScrapingEndpointSinkConfiguration> prometheusConfiguration)
public RuntimeMetricsCollector(IMetricFactory metricFactory, IOptionsMonitor<PrometheusScrapingEndpointSinkConfiguration> prometheusConfiguration)
{
Guard.NotNull(metricFactory, nameof(metricFactory));

_metricFactory = metricFactory;
_prometheusConfiguration = prometheusConfiguration;
}

Expand All @@ -26,7 +32,7 @@ public void SetGaugeMeasurement(string name, string description, double value, D
{
var enableMetricTimestamps = _prometheusConfiguration.CurrentValue.EnableMetricTimestamps;

var gauge = global::Prometheus.Client.Metrics.CreateGauge(name, help: description, includeTimestamp: enableMetricTimestamps, labelNames: labels.Keys.ToArray());
var gauge = _metricFactory.CreateGauge(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 @@ -77,7 +77,8 @@ public MetricsDeclarationBuilder WithServiceBusMetric(string metricName = "promi
string azureMetricName = "Total",
string resourceDiscoveryGroupName = "",
bool omitResource = false,
List<string> queueNames = null)
List<string> queueNames = null,
Dictionary<string, string> labels = null)
{
var serviceBusQueueResources = new List<AzureResourceDefinitionV1>();

Expand All @@ -103,7 +104,7 @@ public MetricsDeclarationBuilder WithServiceBusMetric(string metricName = "promi
serviceBusQueueResources.Add(resource);
}

CreateAndAddMetricDefinition(ResourceType.ServiceBusQueue, metricName, metricDescription, resourceDiscoveryGroupName, omitResource, azureMetricName, serviceBusQueueResources, metricDimension);
CreateAndAddMetricDefinition(ResourceType.ServiceBusQueue, metricName, metricDescription, resourceDiscoveryGroupName, omitResource, azureMetricName, serviceBusQueueResources, metricDimension, labels);

return this;
}
Expand Down Expand Up @@ -561,15 +562,16 @@ private void CreateAndAddMetricDefinition(ResourceType resourceType, string metr
CreateAndAddMetricDefinition(resourceType, metricName, metricDescription, resourceDiscoveryGroupName, omitResource, azureMetricName, new List<AzureResourceDefinitionV1> {resource}, metricDimension);
}

private void CreateAndAddMetricDefinition(ResourceType resourceType, string metricName, string metricDescription, string resourceDiscoveryGroupName, bool omitResource, string azureMetricName, List<AzureResourceDefinitionV1> resources, string metricDimension = null)
private void CreateAndAddMetricDefinition(ResourceType resourceType, string metricName, string metricDescription, string resourceDiscoveryGroupName, bool omitResource, string azureMetricName, List<AzureResourceDefinitionV1> resources, string metricDimension = null, Dictionary<string, string> labels = null)
{
var azureMetricConfiguration = CreateAzureMetricConfiguration(azureMetricName, metricDimension);
var metric = new MetricDefinitionV1
{
Name = metricName,
Description = metricDescription,
AzureMetricConfiguration = azureMetricConfiguration,
ResourceType = resourceType
ResourceType = resourceType,
Labels = labels
};

if (omitResource == false)
Expand Down
Loading

0 comments on commit de5244c

Please sign in to comment.