Skip to content

Commit 24e4e47

Browse files
chkalbrian-brazil
authored andcommitted
MetricFamily#addMetric() should return this to allow builder like usage (#185)
MetricFamily#addMetric() should return "this" to allow builder like usage
1 parent 944ecf9 commit 24e4e47

File tree

6 files changed

+71
-5
lines changed

6 files changed

+71
-5
lines changed

simpleclient/src/main/java/io/prometheus/client/CounterMetricFamily.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,11 @@ public CounterMetricFamily(String name, String help, List<String> labelNames) {
4848
this.labelNames = labelNames;
4949
}
5050

51-
public void addMetric(List<String> labelValues, double value) {
51+
public CounterMetricFamily addMetric(List<String> labelValues, double value) {
5252
if (labelValues.size() != labelNames.size()) {
5353
throw new IllegalArgumentException("Incorrect number of labels.");
5454
}
5555
samples.add(new Sample(name, labelNames, labelValues, value));
56+
return this;
5657
}
5758
}

simpleclient/src/main/java/io/prometheus/client/GaugeMetricFamily.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,11 @@ public GaugeMetricFamily(String name, String help, List<String> labelNames) {
4848
this.labelNames = labelNames;
4949
}
5050

51-
public void addMetric(List<String> labelValues, double value) {
51+
public GaugeMetricFamily addMetric(List<String> labelValues, double value) {
5252
if (labelValues.size() != labelNames.size()) {
5353
throw new IllegalArgumentException("Incorrect number of labels.");
5454
}
5555
samples.add(new Sample(name, labelNames, labelValues, value));
56+
return this;
5657
}
5758
}

simpleclient/src/main/java/io/prometheus/client/SummaryMetricFamily.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ public SummaryMetricFamily(String name, String help, List<String> labelNames, Li
4949
this.quantiles = quantiles;
5050
}
5151

52-
public void addMetric(List<String> labelValues, double count, double sum) {
53-
addMetric(labelValues, count, sum, Collections.<Double>emptyList());
52+
public SummaryMetricFamily addMetric(List<String> labelValues, double count, double sum) {
53+
return addMetric(labelValues, count, sum, Collections.<Double>emptyList());
5454
}
55-
public void addMetric(List<String> labelValues, double count, double sum, List<Double> quantiles) {
55+
public SummaryMetricFamily addMetric(List<String> labelValues, double count, double sum, List<Double> quantiles) {
5656
if (labelValues.size() != labelNames.size()) {
5757
throw new IllegalArgumentException("Incorrect number of labels.");
5858
}
@@ -68,5 +68,6 @@ public void addMetric(List<String> labelValues, double count, double sum, List<D
6868
labelValuesWithQuantile.add(Collector.doubleToGoString(this.quantiles.get(i)));
6969
samples.add(new Sample(name, labelNamesWithQuantile, labelValuesWithQuantile, quantiles.get(i)));
7070
}
71+
return this;
7172
}
7273
}

simpleclient/src/test/java/io/prometheus/client/CounterMetricFamilyTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,25 @@ public List<MetricFamilySamples> collect() {
4242
assertEquals(5.0, registry.getSampleValue("my_other_counter", new String[]{"labelname"}, new String[]{"bar"}).doubleValue(), .001);
4343
}
4444

45+
@Test
46+
public void testBuilderStyleUsage() {
47+
class YourCustomCollector extends Collector {
48+
public List<MetricFamilySamples> collect() {
49+
return Arrays.<MetricFamilySamples>asList(
50+
new CounterMetricFamily("my_metric", "help", Arrays.asList("name"))
51+
.addMetric(Arrays.asList("value1"), 1.0)
52+
.addMetric(Arrays.asList("value2"), 2.0)
53+
);
54+
}
55+
}
56+
new YourCustomCollector().register(registry);
57+
58+
assertEquals(1.0,
59+
registry.getSampleValue("my_metric", new String[]{"name"}, new String[]{"value1"})
60+
.doubleValue(), .001);
61+
assertEquals(2.0,
62+
registry.getSampleValue("my_metric", new String[]{"name"}, new String[]{"value2"})
63+
.doubleValue(), .001);
64+
}
65+
4566
}

simpleclient/src/test/java/io/prometheus/client/GaugeMetricFamilyTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,25 @@ public List<MetricFamilySamples> collect() {
4242
assertEquals(5.0, registry.getSampleValue("my_other_gauge", new String[]{"labelname"}, new String[]{"bar"}).doubleValue(), .001);
4343
}
4444

45+
@Test
46+
public void testBuilderStyleUsage() {
47+
class YourCustomCollector extends Collector {
48+
public List<MetricFamilySamples> collect() {
49+
return Arrays.<MetricFamilySamples>asList(
50+
new GaugeMetricFamily("my_metric", "help", Arrays.asList("name"))
51+
.addMetric(Arrays.asList("value1"), 1.0)
52+
.addMetric(Arrays.asList("value2"), 2.0)
53+
);
54+
}
55+
}
56+
new YourCustomCollector().register(registry);
57+
58+
assertEquals(1.0,
59+
registry.getSampleValue("my_metric", new String[]{"name"}, new String[]{"value1"})
60+
.doubleValue(), .001);
61+
assertEquals(2.0,
62+
registry.getSampleValue("my_metric", new String[]{"name"}, new String[]{"value2"})
63+
.doubleValue(), .001);
64+
}
65+
4566
}

simpleclient/src/test/java/io/prometheus/client/SummaryMetricFamilyTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,25 @@ public List<MetricFamilySamples> collect() {
4545
assertEquals(5.0, registry.getSampleValue("my_other_summary", new String[]{"labelname", "quantile"}, new String[]{"foo", "0.99"}).doubleValue(), .001);
4646
}
4747

48+
@Test
49+
public void testBuilderStyleUsage() {
50+
class YourCustomCollector extends Collector {
51+
public List<MetricFamilySamples> collect() {
52+
return Arrays.<MetricFamilySamples>asList(
53+
new SummaryMetricFamily("my_metric", "help", Arrays.asList("name"))
54+
.addMetric(Arrays.asList("value1"), 1, 1.0)
55+
.addMetric(Arrays.asList("value2"), 2, 2.0)
56+
);
57+
}
58+
}
59+
new YourCustomCollector().register(registry);
60+
61+
assertEquals(1.0,
62+
registry.getSampleValue("my_metric_count", new String[]{"name"}, new String[]{"value1"})
63+
.doubleValue(), .001);
64+
assertEquals(2.0,
65+
registry.getSampleValue("my_metric_count", new String[]{"name"}, new String[]{"value2"})
66+
.doubleValue(), .001);
67+
}
68+
4869
}

0 commit comments

Comments
 (0)