Skip to content

Commit 1defbbf

Browse files
committed
fix: include counter names in negative value errors
1 parent 014f248 commit 1defbbf

5 files changed

Lines changed: 29 additions & 45 deletions

File tree

prometheus-metrics-core/src/main/java/io/prometheus/metrics/core/metrics/Counter.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,13 @@ private CounterSnapshot.CounterDataPointSnapshot collect(Labels labels, String m
213213
}
214214
}
215215
}
216-
return new CounterSnapshot.CounterDataPointSnapshot(
217-
get(), labels, latestExemplar, createdTimeMillis, metricName);
216+
return CounterSnapshot.CounterDataPointSnapshot.builder()
217+
.value(get())
218+
.labels(labels)
219+
.exemplar(latestExemplar)
220+
.createdTimestampMillis(createdTimeMillis)
221+
.metricName(metricName)
222+
.build();
218223
}
219224
}
220225

prometheus-metrics-core/src/main/java/io/prometheus/metrics/core/metrics/CounterWithCallback.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,11 @@ public CounterSnapshot collect() {
4747
callback.accept(
4848
(value, labelValues) -> {
4949
dataPoints.add(
50-
new CounterSnapshot.CounterDataPointSnapshot(
51-
value, makeLabels(labelValues), null, 0L, metadata.getName()));
50+
CounterSnapshot.CounterDataPointSnapshot.builder()
51+
.value(value)
52+
.labels(makeLabels(labelValues))
53+
.metricName(metadata.getName())
54+
.build());
5255
});
5356
return new CounterSnapshot(metadata, dataPoints);
5457
}

prometheus-metrics-core/src/test/java/io/prometheus/metrics/core/metrics/CounterWithCallbackTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,17 @@ void testCounterNoCallback() {
4343
.isThrownBy(
4444
() -> CounterWithCallback.builder().name("counter").labelNames("l1", "l2").build());
4545
}
46+
47+
@Test
48+
void testNegativeValueIncludesMetricName() {
49+
CounterWithCallback counter =
50+
CounterWithCallback.builder()
51+
.name("negative_counter")
52+
.callback(callback -> callback.call(-1.0))
53+
.build();
54+
55+
assertThatExceptionOfType(IllegalArgumentException.class)
56+
.isThrownBy(counter::collect)
57+
.withMessage("negative_counter=-1.0: counters cannot have a negative value");
58+
}
4659
}

prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/CounterSnapshot.java

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public static class CounterDataPointSnapshot extends DataPointSnapshot {
4848

4949
private final double value;
5050
@Nullable private final Exemplar exemplar;
51+
5152
/** Optional metric name used only in validation error messages. */
5253
@Nullable private final String metricName;
5354

@@ -64,20 +65,7 @@ public static class CounterDataPointSnapshot extends DataPointSnapshot {
6465
*/
6566
public CounterDataPointSnapshot(
6667
double value, Labels labels, @Nullable Exemplar exemplar, long createdTimestampMillis) {
67-
this(value, labels, exemplar, createdTimestampMillis, 0, null);
68-
}
69-
70-
/**
71-
* Same as {@link #CounterDataPointSnapshot(double, Labels, Exemplar, long)} with an optional
72-
* metric name included in validation error messages when the value is negative.
73-
*/
74-
public CounterDataPointSnapshot(
75-
double value,
76-
Labels labels,
77-
@Nullable Exemplar exemplar,
78-
long createdTimestampMillis,
79-
@Nullable String metricName) {
80-
this(value, labels, exemplar, createdTimestampMillis, 0, metricName);
68+
this(value, labels, exemplar, createdTimestampMillis, 0, false, null);
8169
}
8270

8371
/**
@@ -95,29 +83,6 @@ public CounterDataPointSnapshot(
9583
this(value, labels, exemplar, createdTimestampMillis, scrapeTimestampMillis, false, null);
9684
}
9785

98-
/**
99-
* Constructor with scrape timestamp and optional metric name for validation messages.
100-
*
101-
* @see #CounterDataPointSnapshot(double, Labels, Exemplar, long, long)
102-
*/
103-
@SuppressWarnings("this-escape")
104-
public CounterDataPointSnapshot(
105-
double value,
106-
Labels labels,
107-
@Nullable Exemplar exemplar,
108-
long createdTimestampMillis,
109-
long scrapeTimestampMillis,
110-
@Nullable String metricName) {
111-
this(
112-
value,
113-
labels,
114-
exemplar,
115-
createdTimestampMillis,
116-
scrapeTimestampMillis,
117-
false,
118-
metricName);
119-
}
120-
12186
@SuppressWarnings("this-escape")
12287
public CounterDataPointSnapshot(
12388
double value,
@@ -231,6 +196,7 @@ public CounterDataPointSnapshot build() {
231196
exemplar,
232197
createdTimestampMillis,
233198
scrapeTimestampMillis,
199+
false,
234200
metricName);
235201
}
236202

prometheus-metrics-model/src/test/java/io/prometheus/metrics/model/snapshots/CounterSnapshotTest.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,7 @@ void testNegativeValueIncludesMetricNameInMessage() {
136136
assertThatExceptionOfType(IllegalArgumentException.class)
137137
.isThrownBy(
138138
() ->
139-
CounterDataPointSnapshot.builder()
140-
.metricName("http_requests")
141-
.value(-2.0)
142-
.build())
139+
CounterDataPointSnapshot.builder().metricName("http_requests").value(-2.0).build())
143140
.withMessageContaining("http_requests")
144141
.withMessageContaining("-2.0")
145142
.withMessageContaining("counters cannot have a negative value");

0 commit comments

Comments
 (0)