From 76555f16b7fd44c8236731881b4afaca5995edf1 Mon Sep 17 00:00:00 2001 From: "anton.g" Date: Mon, 7 Oct 2024 19:01:10 +0300 Subject: [PATCH] allow passing metric name to datapoint to show in case of validation error --- .../model/snapshots/CounterSnapshot.java | 22 ++++++++++++++++--- .../model/snapshots/DataPointSnapshot.java | 17 +++++++++++++- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/CounterSnapshot.java b/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/CounterSnapshot.java index fa807af19..85b2ef35d 100644 --- a/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/CounterSnapshot.java +++ b/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/CounterSnapshot.java @@ -56,7 +56,17 @@ public CounterDataPointSnapshot( Exemplar exemplar, long createdTimestampMillis, long scrapeTimestampMillis) { - super(labels, createdTimestampMillis, scrapeTimestampMillis); + this(null, value, labels, exemplar, createdTimestampMillis, scrapeTimestampMillis); + } + + public CounterDataPointSnapshot( + String metricName, + double value, + Labels labels, + Exemplar exemplar, + long createdTimestampMillis, + long scrapeTimestampMillis) { + super(metricName, labels, createdTimestampMillis, scrapeTimestampMillis); this.value = value; this.exemplar = exemplar; validate(); @@ -73,7 +83,7 @@ public Exemplar getExemplar() { protected void validate() { if (value < 0.0) { - throw new IllegalArgumentException(value + ": counters cannot have a negative value"); + throw new IllegalArgumentException((getMetricName() == null ? "" : getMetricName() + "=") + value + ": counters cannot have a negative value"); } } @@ -83,6 +93,7 @@ public static Builder builder() { public static class Builder extends DataPointSnapshot.Builder { + private String metricName = null; private Exemplar exemplar = null; private Double value = null; private long createdTimestampMillis = 0L; @@ -105,12 +116,17 @@ public Builder createdTimestampMillis(long createdTimestampMillis) { return this; } + public Builder metricName(String metricName) { + this.metricName = metricName; + return this; + } + public CounterDataPointSnapshot build() { if (value == null) { throw new IllegalArgumentException("Missing required field: value is null."); } return new CounterDataPointSnapshot( - value, labels, exemplar, createdTimestampMillis, scrapeTimestampMillis); + metricName, value, labels, exemplar, createdTimestampMillis, scrapeTimestampMillis); } @Override diff --git a/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/DataPointSnapshot.java b/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/DataPointSnapshot.java index 47ad4486b..b00fab4c6 100644 --- a/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/DataPointSnapshot.java +++ b/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/DataPointSnapshot.java @@ -1,12 +1,19 @@ package io.prometheus.metrics.model.snapshots; public abstract class DataPointSnapshot { + private final String metricName; private final Labels labels; private final long createdTimestampMillis; private final long scrapeTimestampMillis; protected DataPointSnapshot( - Labels labels, long createdTimestampMillis, long scrapeTimestampMillis) { + Labels labels, long createdTimestampMillis, long scrapeTimestampMillis) { + this(null, labels, createdTimestampMillis, scrapeTimestampMillis); + } + + protected DataPointSnapshot( + String metricName, Labels labels, long createdTimestampMillis, long scrapeTimestampMillis) { + this.metricName = metricName; this.labels = labels; this.createdTimestampMillis = createdTimestampMillis; this.scrapeTimestampMillis = scrapeTimestampMillis; @@ -14,6 +21,10 @@ protected DataPointSnapshot( } private void validate() { + if (metricName != null && metricName.trim().isEmpty()) { + throw new IllegalArgumentException("Metric name cannot be an empty string"); + } + if (labels == null) { throw new IllegalArgumentException( "Labels cannot be null. Use Labels.EMPTY if there are no labels."); @@ -60,6 +71,10 @@ public long getCreatedTimestampMillis() { return createdTimestampMillis; } + public String getMetricName() { + return metricName; + } + public abstract static class Builder> { protected Labels labels = Labels.EMPTY;