|
1 | 1 | package io.prometheus.client.exporter; |
2 | 2 |
|
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; |
7 | 6 | import java.net.HttpURLConnection; |
8 | 7 | import java.net.InetAddress; |
9 | | -import java.net.UnknownHostException; |
| 8 | +import java.net.MalformedURLException; |
| 9 | +import java.net.URI; |
10 | 10 | import java.net.URL; |
11 | 11 | import java.net.URLEncoder; |
| 12 | +import java.net.UnknownHostException; |
12 | 13 | import java.util.Collections; |
13 | 14 | import java.util.HashMap; |
14 | 15 | import java.util.Map; |
15 | 16 |
|
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; |
19 | 20 |
|
20 | 21 | /** |
21 | 22 | * Export metrics via the Prometheus Pushgateway. |
|
54 | 55 | */ |
55 | 56 | public class PushGateway { |
56 | 57 |
|
57 | | - private final String address; |
| 58 | + // Visible for testing. |
| 59 | + protected final String gatewayBaseURL; |
| 60 | + |
58 | 61 | private static final int MILLISECONDS_PER_SECOND = 1000; |
59 | 62 | /** |
60 | 63 | * Construct a Pushgateway, with the given address. |
61 | 64 | * <p> |
62 | 65 | * @param address host:port or ip:port of the Pushgateway. |
63 | 66 | */ |
64 | 67 | 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 | + } |
66 | 97 | } |
67 | 98 |
|
68 | 99 | /** |
@@ -235,7 +266,8 @@ public void delete(String job, String instance) throws IOException { |
235 | 266 | } |
236 | 267 |
|
237 | 268 | 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 | + |
239 | 271 | if (groupingKey != null) { |
240 | 272 | for (Map.Entry<String, String> entry: groupingKey.entrySet()) { |
241 | 273 | url += "/" + entry.getKey() + "/" + URLEncoder.encode(entry.getValue(), "UTF-8"); |
|
0 commit comments