Skip to content

Commit af8d033

Browse files
thenevabrian-brazil
authored andcommitted
Accept URL to Pushgateway constructor. Closes #258 (#257)
1 parent ee34e76 commit af8d033

File tree

2 files changed

+57
-11
lines changed

2 files changed

+57
-11
lines changed

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

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

3-
import io.prometheus.client.Collector;
4-
import io.prometheus.client.CollectorRegistry;
5-
import io.prometheus.client.exporter.common.TextFormat;
6-
3+
import java.io.BufferedWriter;
4+
import java.io.IOException;
5+
import java.io.OutputStreamWriter;
76
import java.net.HttpURLConnection;
87
import java.net.InetAddress;
9-
import java.net.UnknownHostException;
8+
import java.net.MalformedURLException;
9+
import java.net.URI;
1010
import java.net.URL;
1111
import java.net.URLEncoder;
12+
import java.net.UnknownHostException;
1213
import java.util.Collections;
1314
import java.util.HashMap;
1415
import java.util.Map;
1516

16-
import java.io.BufferedWriter;
17-
import java.io.IOException;
18-
import java.io.OutputStreamWriter;
17+
import io.prometheus.client.Collector;
18+
import io.prometheus.client.CollectorRegistry;
19+
import io.prometheus.client.exporter.common.TextFormat;
1920

2021
/**
2122
* Export metrics via the Prometheus Pushgateway.
@@ -54,15 +55,45 @@
5455
*/
5556
public class PushGateway {
5657

57-
private final String address;
58+
// Visible for testing.
59+
protected final String gatewayBaseURL;
60+
5861
private static final int MILLISECONDS_PER_SECOND = 1000;
5962
/**
6063
* Construct a Pushgateway, with the given address.
6164
* <p>
6265
* @param address host:port or ip:port of the Pushgateway.
6366
*/
6467
public PushGateway(String address) {
65-
this.address = address;
68+
this(createURLSneakily("http://" + address));
69+
}
70+
71+
/**
72+
* Construct a Pushgateway, with the given URL.
73+
* <p>
74+
* @param serverBaseURL the base URL and optional context path of the Pushgateway server.
75+
*/
76+
public PushGateway(URL serverBaseURL) {
77+
this.gatewayBaseURL = URI.create(serverBaseURL.toString() + "/metrics/job/")
78+
.normalize()
79+
.toString();
80+
}
81+
82+
/**
83+
* Creates a URL instance from a String representation of a URL without throwing a checked exception.
84+
* Required because you can't wrap a call to another constructor in a try statement.
85+
*
86+
* TODO: Remove this along with other deprecated methods before version 1.0 is released.
87+
*
88+
* @param urlString the String representation of the URL.
89+
* @return The URL instance.
90+
*/
91+
private static URL createURLSneakily(final String urlString) {
92+
try {
93+
return new URL(urlString);
94+
} catch (MalformedURLException e) {
95+
throw new RuntimeException(e);
96+
}
6697
}
6798

6899
/**
@@ -235,7 +266,8 @@ public void delete(String job, String instance) throws IOException {
235266
}
236267

237268
void doRequest(CollectorRegistry registry, String job, Map<String, String> groupingKey, String method) throws IOException {
238-
String url = "http://" + address + "/metrics/job/" + URLEncoder.encode(job, "UTF-8");
269+
String url = gatewayBaseURL + URLEncoder.encode(job, "UTF-8");
270+
239271
if (groupingKey != null) {
240272
for (Map.Entry<String, String> entry: groupingKey.entrySet()) {
241273
url += "/" + entry.getKey() + "/" + URLEncoder.encode(entry.getValue(), "UTF-8");

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,20 @@ public void setUp() {
3636
groupingKey.put("l", "v");
3737
}
3838

39+
@Test(expected = RuntimeException.class)
40+
public void testInvalidURLThrowsRuntimeException() {
41+
new PushGateway("::"); // ":" is interpreted as port number, so parsing fails
42+
}
43+
44+
@Test
45+
public void testMultipleSlashesAreStrippedFromURL() {
46+
final PushGateway pushGateway = new PushGateway("example.com:1234/context///path//");
47+
Assert.assertEquals(
48+
"http://example.com:1234/context/path/metrics/job/",
49+
pushGateway.gatewayBaseURL
50+
);
51+
}
52+
3953
@Test
4054
public void testPush() throws IOException {
4155
mockServerClient.when(

0 commit comments

Comments
 (0)