Skip to content

Commit 8d697b3

Browse files
authored
add sum and count for DistributionDataPoint (#1669)
Fixes #1610 Signed-off-by: Gregor Zeitlinger <[email protected]>
1 parent 748b190 commit 8d697b3

File tree

5 files changed

+57
-0
lines changed

5 files changed

+57
-0
lines changed

prometheus-metrics-core/src/main/java/io/prometheus/metrics/core/datapoints/DistributionDataPoint.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919
*/
2020
public interface DistributionDataPoint extends DataPoint, TimerApi {
2121

22+
/** Get the count of observations. */
23+
long getCount();
24+
25+
/** Get the sum of all observed values. */
26+
double getSum();
27+
2228
/** Observe {@code value}. */
2329
void observe(double value);
2430

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,16 @@ private Histogram(Histogram.Builder builder, PrometheusProperties prometheusProp
170170
}
171171
}
172172

173+
@Override
174+
public double getSum() {
175+
return getNoLabels().getSum();
176+
}
177+
178+
@Override
179+
public long getCount() {
180+
return getNoLabels().getCount();
181+
}
182+
173183
@Override
174184
public void observe(double amount) {
175185
getNoLabels().observe(amount);
@@ -210,6 +220,16 @@ private DataPoint() {
210220
maybeScheduleNextReset();
211221
}
212222

223+
@Override
224+
public double getSum() {
225+
return sum.sum();
226+
}
227+
228+
@Override
229+
public long getCount() {
230+
return count.sum();
231+
}
232+
213233
@Override
214234
public void observe(double value) {
215235
if (Double.isNaN(value)) {

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,16 @@ private List<CKMSQuantiles.Quantile> makeQuantiles(MetricsProperties[] propertie
8484
return result;
8585
}
8686

87+
@Override
88+
public double getSum() {
89+
return getNoLabels().getSum();
90+
}
91+
92+
@Override
93+
public long getCount() {
94+
return getNoLabels().getCount();
95+
}
96+
8797
@Override
8898
public void observe(double amount) {
8999
getNoLabels().observe(amount);
@@ -143,6 +153,16 @@ private DataPoint() {
143153
}
144154
}
145155

156+
@Override
157+
public double getSum() {
158+
return sum.sum();
159+
}
160+
161+
@Override
162+
public long getCount() {
163+
return count.sum();
164+
}
165+
146166
@Override
147167
public void observe(double value) {
148168
if (Double.isNaN(value)) {

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,6 +1366,8 @@ private ClassicHistogramBucket getBucket(Histogram histogram, double le, String.
13661366
public void testObserve() {
13671367
Histogram noLabels = Histogram.builder().name("test").build();
13681368
noLabels.observe(2);
1369+
assertThat(noLabels.getCount()).isOne();
1370+
assertThat(noLabels.getSum()).isCloseTo(2.0, offset(.0));
13691371
assertThat(getData(noLabels).getCount()).isOne();
13701372
assertThat(getData(noLabels).getSum()).isCloseTo(2.0, offset(.0));
13711373
assertThat(getBucket(noLabels, 1).getCount()).isZero();
@@ -1450,6 +1452,9 @@ public void testObserveWithLabels() {
14501452
.labelNames("path", "status")
14511453
.build();
14521454
histogram.labelValues("/hello", "200").observe(0.11);
1455+
DistributionDataPoint distributionDataPoint = histogram.labelValues("/hello", "200");
1456+
assertThat(distributionDataPoint.getCount()).isOne();
1457+
assertThat(distributionDataPoint.getSum()).isCloseTo(0.11, offset(.0));
14531458
histogram.labelValues("/hello", "200").observe(0.2);
14541459
histogram.labelValues("/hello", "500").observe(0.19);
14551460
HistogramSnapshot.HistogramDataPointSnapshot data200 =

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import static org.assertj.core.api.Assertions.fail;
66
import static org.assertj.core.data.Offset.offset;
77

8+
import io.prometheus.metrics.core.datapoints.DistributionDataPoint;
89
import io.prometheus.metrics.core.datapoints.Timer;
910
import io.prometheus.metrics.model.registry.PrometheusRegistry;
1011
import io.prometheus.metrics.model.snapshots.Label;
@@ -64,13 +65,18 @@ public void setUp() {
6465
@Test
6566
public void testObserve() {
6667
noLabels.observe(2);
68+
assertThat(noLabels.getCount()).isOne();
69+
assertThat(noLabels.getSum()).isCloseTo(2.0, offset(.0));
6770
assertThat(getCount(noLabels, Labels.EMPTY)).isOne();
6871
assertThat(getSum(noLabels, Labels.EMPTY)).isCloseTo(2.0, offset(.001));
6972
noLabels.observe(3);
7073
assertThat(getCount(noLabels, Labels.EMPTY)).isEqualTo(2);
7174
assertThat(getSum(noLabels, Labels.EMPTY)).isCloseTo(5.0, offset(.001));
7275

7376
withLabels.labelValues(label.getValue()).observe(4);
77+
DistributionDataPoint distributionDataPoint = withLabels.labelValues(label.getValue());
78+
assertThat(distributionDataPoint.getCount()).isOne();
79+
assertThat(distributionDataPoint.getSum()).isCloseTo(4.0, offset(.0));
7480
assertThat(getCount(withLabels, labels)).isOne();
7581
assertThat(getSum(withLabels, labels)).isCloseTo(4.0, offset(.001));
7682

0 commit comments

Comments
 (0)