|
5 | 5 | import java.io.IOException; |
6 | 6 | import java.io.InputStream; |
7 | 7 | import java.io.OutputStreamWriter; |
| 8 | +import java.io.UnsupportedEncodingException; |
8 | 9 | import java.net.HttpURLConnection; |
9 | 10 | import java.net.InetAddress; |
10 | 11 | import java.net.MalformedURLException; |
|
15 | 16 | import java.util.Collections; |
16 | 17 | import java.util.HashMap; |
17 | 18 | import java.util.Map; |
| 19 | +import javax.xml.bind.DatatypeConverter; |
18 | 20 |
|
19 | 21 | import io.prometheus.client.Collector; |
20 | 22 | import io.prometheus.client.CollectorRegistry; |
@@ -79,7 +81,7 @@ public PushGateway(String address) { |
79 | 81 | * @param serverBaseURL the base URL and optional context path of the Pushgateway server. |
80 | 82 | */ |
81 | 83 | public PushGateway(URL serverBaseURL) { |
82 | | - this.gatewayBaseURL = URI.create(serverBaseURL.toString() + "/metrics/job/") |
| 84 | + this.gatewayBaseURL = URI.create(serverBaseURL.toString() + "/metrics/") |
83 | 85 | .normalize() |
84 | 86 | .toString(); |
85 | 87 | } |
@@ -275,11 +277,20 @@ public void delete(String job, String instance) throws IOException { |
275 | 277 | } |
276 | 278 |
|
277 | 279 | void doRequest(CollectorRegistry registry, String job, Map<String, String> groupingKey, String method) throws IOException { |
278 | | - String url = gatewayBaseURL + URLEncoder.encode(job, "UTF-8"); |
| 280 | + String url = gatewayBaseURL; |
| 281 | + if (job.contains("/")) { |
| 282 | + url += "job@base64/" + base64url(job); |
| 283 | + } else { |
| 284 | + url += "job/" + URLEncoder.encode(job, "UTF-8"); |
| 285 | + } |
279 | 286 |
|
280 | 287 | if (groupingKey != null) { |
281 | 288 | for (Map.Entry<String, String> entry: groupingKey.entrySet()) { |
282 | | - url += "/" + entry.getKey() + "/" + URLEncoder.encode(entry.getValue(), "UTF-8"); |
| 289 | + if (entry.getValue().contains("/")) { |
| 290 | + url += "/" + entry.getKey() + "@base64/" + base64url(entry.getValue()); |
| 291 | + } else { |
| 292 | + url += "/" + entry.getKey() + "/" + URLEncoder.encode(entry.getValue(), "UTF-8"); |
| 293 | + } |
283 | 294 | } |
284 | 295 | } |
285 | 296 | HttpURLConnection connection = connectionFactory.create(url); |
@@ -318,6 +329,15 @@ void doRequest(CollectorRegistry registry, String job, Map<String, String> group |
318 | 329 | } |
319 | 330 | } |
320 | 331 |
|
| 332 | + private static String base64url(String v) { |
| 333 | + // Per RFC4648 table 2. We support Java 6, and java.util.Base64 was only added in Java 8, |
| 334 | + try { |
| 335 | + return DatatypeConverter.printBase64Binary(v.getBytes("UTF-8")).replace("+", "-").replace("/", "_"); |
| 336 | + } catch (UnsupportedEncodingException e) { |
| 337 | + throw new RuntimeException(e); // Unreachable. |
| 338 | + } |
| 339 | + } |
| 340 | + |
321 | 341 | /** |
322 | 342 | * Returns a grouping key with the instance label set to the machine's IP address. |
323 | 343 | * <p> |
|
0 commit comments