private (string[] Names, string[] Values) DetermineLabels(PrometheusMetricDefinition metricDefinition, ScrapeResult scrapeResult, MeasuredMetric measuredMetric) { var labels = new Dictionary <string, string>(scrapeResult.Labels); if (measuredMetric.IsDimensional) { labels.Add(measuredMetric.DimensionName.ToLower(), measuredMetric.DimensionValue); } if (metricDefinition?.Labels?.Any() == true) { foreach (var customLabel in metricDefinition.Labels) { if (labels.ContainsKey(customLabel.Key)) { _logger.LogWarning("Custom label {CustomLabelName} was already specified with value 'LabelValue' instead of 'CustomLabelValue'. Ignoring...", customLabel.Key, labels[customLabel.Key], customLabel.Value); continue; } labels.Add(customLabel.Key, customLabel.Value); } } return(labels.Keys.ToArray(), labels.Values.ToArray()); }
private Dictionary <string, string> DetermineLabels(PrometheusMetricDefinition metricDefinition, ScrapeResult scrapeResult, MeasuredMetric measuredMetric) { var labels = new Dictionary <string, string>(scrapeResult.Labels.Select(label => new KeyValuePair <string, string>(label.Key.SanitizeForPrometheusLabelKey(), label.Value))); if (measuredMetric.IsDimensional) { labels.Add(measuredMetric.DimensionName.SanitizeForPrometheusLabelKey(), measuredMetric.DimensionValue); } if (metricDefinition?.Labels?.Any() == true) { foreach (var customLabel in metricDefinition.Labels) { var customLabelKey = customLabel.Key.SanitizeForPrometheusLabelKey(); if (labels.ContainsKey(customLabelKey)) { _logger.LogWarning("Custom label {CustomLabelName} was already specified with value 'LabelValue' instead of 'CustomLabelValue'. Ignoring...", customLabel.Key, labels[customLabelKey], customLabel.Value); continue; } labels.Add(customLabelKey, customLabel.Value); } } return(labels); }
public void ReportMetric(PrometheusMetricDefinition metricDefinition, ScrapeResult scrapedMetricResult) { var enableMetricTimestamps = _prometheusConfiguration.CurrentValue.EnableMetricTimestamps; var labels = DetermineLabels(metricDefinition, scrapedMetricResult); var gauge = Metrics.CreateGauge(metricDefinition.Name, metricDefinition.Description, includeTimestamp: enableMetricTimestamps, labelNames: labels.Names); var metricValue = DetermineMetricMeasurement(scrapedMetricResult); gauge.WithLabels(labels.Values).Set(metricValue); }
private Dictionary <string, string> DetermineLabels(PrometheusMetricDefinition metricDefinition, ScrapeResult scrapeResult, MeasuredMetric measuredMetric, Dictionary <string, string> defaultLabels) { var labels = new Dictionary <string, string>(scrapeResult.Labels.Select(label => new KeyValuePair <string, string>(label.Key.SanitizeForPrometheusLabelKey(), label.Value))); if (measuredMetric.IsDimensional) { labels.Add(measuredMetric.DimensionName.SanitizeForPrometheusLabelKey(), measuredMetric.DimensionValue); } if (metricDefinition?.Labels?.Any() == true) { foreach (var customLabel in metricDefinition.Labels) { var customLabelKey = customLabel.Key.SanitizeForPrometheusLabelKey(); if (labels.ContainsKey(customLabelKey)) { _logger.LogWarning("Custom label {CustomLabelName} was already specified with value '{LabelValue}' instead of '{CustomLabelValue}'. Ignoring...", customLabel.Key, labels[customLabelKey], customLabel.Value); continue; } labels.Add(customLabelKey, customLabel.Value); } } foreach (var defaultLabel in defaultLabels) { var defaultLabelKey = defaultLabel.Key.SanitizeForPrometheusLabelKey(); if (labels.ContainsKey(defaultLabelKey) == false) { labels.Add(defaultLabelKey, defaultLabel.Value); } } // Add the tenant id var metricsDeclaration = _metricsDeclarationProvider.Get(applyDefaults: true); if (labels.ContainsKey("tenant_id") == false) { labels.Add("tenant_id", metricsDeclaration.AzureMetadata.TenantId); } // Transform labels, if need be if (_prometheusConfiguration.CurrentValue.Labels != null) { labels = LabelTransformer.TransformLabels(_prometheusConfiguration.CurrentValue.Labels.Transformation, labels); } var orderedLabels = labels.OrderBy(kvp => kvp.Key).ToDictionary(kvp => kvp.Key, kvp => kvp.Value); return(orderedLabels); }