Skip to content

Commit fe88c75

Browse files
committed
Make it easier to push just one metric.
1 parent a407874 commit fe88c75

File tree

4 files changed

+56
-2
lines changed

4 files changed

+56
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ assets:
3535
* [PushGateway support](http://mvnrepository.com/artifact/io.prometheus.client/simpleclient_pushgateway)
3636
* groupId: _io.prometheus_
3737
* artifactId: _simpleclient_pushgateway_
38-
* version: _0.0.3_
38+
* version: _0.0.4_
3939

4040
### Getting Started
4141
There are canonical examples defined in the class definition Javadoc of the client packages.

simpleclient_pushgateway/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
<groupId>io.prometheus</groupId>
1212
<artifactId>simpleclient_pushgateway</artifactId>
13-
<version>0.0.3</version>
13+
<version>0.0.4</version>
1414

1515
<name>Prometheus Java Client Metrics</name>
1616
<description>

simpleclient_pushgateway/src/main/java/io/prometheus/client/exporter/PushGateway.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.prometheus.client.exporter;
22

3+
import io.prometheus.client.Collector;
34
import io.prometheus.client.CollectorRegistry;
45
import io.prometheus.client.exporter.common.TextFormat;
56

@@ -45,6 +46,21 @@ public void push(CollectorRegistry registry, String job, String instance) throws
4546
doRequest(registry, job, instance, "POST");
4647
}
4748

49+
/**
50+
* Pushes all metrics in a Collector, replacing all those with the same job and instance.
51+
* <p>
52+
* This is useful for pushing a single Gauge.
53+
* <p>
54+
* See the Pushgateway documentation for detailed implications of the job and
55+
* instance parameter. instance can be left empty. The Pushgateway will then
56+
* use the client's IP number instead. This uses the POST HTTP method.
57+
*/
58+
public void push(Collector collector, String job, String instance) throws IOException {
59+
CollectorRegistry registry = new CollectorRegistry();
60+
collector.register(registry);
61+
push(registry, job, instance);
62+
}
63+
4864
/**
4965
* Pushes all metrics in a registry, replacing only previously pushed metrics of the same name.
5066
* <p>
@@ -56,6 +72,21 @@ public void pushAdd(CollectorRegistry registry, String job, String instance) thr
5672
doRequest(registry, job, instance, "PUT");
5773
}
5874

75+
/**
76+
* Pushes all metrics in a Collector, replacing only previously pushed metrics of the same name.
77+
* <p>
78+
* This is useful for pushing a single Gauge.
79+
* <p>
80+
* See the Pushgateway documentation for detailed implications of the job and
81+
* instance parameter. instance can be left empty. The Pushgateway will then
82+
* use the client's IP number instead. This uses the POST HTTP method.
83+
*/
84+
public void pushAdd(Collector collector, String job, String instance) throws IOException {
85+
CollectorRegistry registry = new CollectorRegistry();
86+
collector.register(registry);
87+
pushAdd(registry, job, instance);
88+
}
89+
5990
/**
6091
* Deletes metrics from the Pushgateway.
6192
* <p>

simpleclient_pushgateway/src/test/java/io/prometheus/client/exporter/PushGatewayTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import static org.mockserver.model.HttpResponse.response;
66

77
import io.prometheus.client.CollectorRegistry;
8+
import io.prometheus.client.Gauge;
89
import java.io.IOException;;
910
import org.junit.Before;
1011
import org.junit.Rule;
@@ -19,11 +20,13 @@ public class PushGatewayTest {
1920
private MockServerClient mockServerClient;
2021

2122
CollectorRegistry registry;
23+
Gauge gauge;
2224
PushGateway pg;
2325

2426
@Before
2527
public void setUp() {
2628
registry = new CollectorRegistry();
29+
gauge = (Gauge) Gauge.build().name("g").help("help").create();
2730
pg = new PushGateway("localhost:" + mockServerRule.getHttpPort());
2831
}
2932

@@ -67,6 +70,16 @@ public void testPushWithSlashes() throws IOException {
6770
pg.push(registry, "a/b", "c/d");
6871
}
6972

73+
@Test
74+
public void testPushCollector() throws IOException {
75+
mockServerClient.when(
76+
request()
77+
.withMethod("POST")
78+
.withPath("/metrics/jobs/j/instances/i")
79+
).respond(response().withStatusCode(202));
80+
pg.push(gauge, "j", "i");
81+
}
82+
7083
@Test
7184
public void testPushAdd() throws IOException {
7285
mockServerClient.when(
@@ -77,6 +90,16 @@ public void testPushAdd() throws IOException {
7790
pg.pushAdd(registry, "j", "i");
7891
}
7992

93+
@Test
94+
public void testPushAddCollector() throws IOException {
95+
mockServerClient.when(
96+
request()
97+
.withMethod("PUT")
98+
.withPath("/metrics/jobs/j/instances/i")
99+
).respond(response().withStatusCode(202));
100+
pg.pushAdd(gauge, "j", "i");
101+
}
102+
80103
@Test
81104
public void testDelete() throws IOException {
82105
mockServerClient.when(

0 commit comments

Comments
 (0)