Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.MalformedURLException;
Expand All @@ -15,6 +16,7 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import javax.xml.bind.DatatypeConverter;

import io.prometheus.client.Collector;
import io.prometheus.client.CollectorRegistry;
Expand Down Expand Up @@ -79,7 +81,7 @@ public PushGateway(String address) {
* @param serverBaseURL the base URL and optional context path of the Pushgateway server.
*/
public PushGateway(URL serverBaseURL) {
this.gatewayBaseURL = URI.create(serverBaseURL.toString() + "/metrics/job/")
this.gatewayBaseURL = URI.create(serverBaseURL.toString() + "/metrics/")
.normalize()
.toString();
}
Expand Down Expand Up @@ -275,11 +277,20 @@ public void delete(String job, String instance) throws IOException {
}

void doRequest(CollectorRegistry registry, String job, Map<String, String> groupingKey, String method) throws IOException {
String url = gatewayBaseURL + URLEncoder.encode(job, "UTF-8");
String url = gatewayBaseURL;
if (job.contains("/")) {
url += "job@base64/" + base64url(job);
} else {
url += "job/" + URLEncoder.encode(job, "UTF-8");
}

if (groupingKey != null) {
for (Map.Entry<String, String> entry: groupingKey.entrySet()) {
url += "/" + entry.getKey() + "/" + URLEncoder.encode(entry.getValue(), "UTF-8");
if (entry.getValue().contains("/")) {
url += "/" + entry.getKey() + "@base64/" + base64url(entry.getValue());
} else {
url += "/" + entry.getKey() + "/" + URLEncoder.encode(entry.getValue(), "UTF-8");
}
}
}
HttpURLConnection connection = connectionFactory.create(url);
Expand Down Expand Up @@ -318,6 +329,15 @@ void doRequest(CollectorRegistry registry, String job, Map<String, String> group
}
}

private static String base64url(String v) {
// Per RFC4648 table 2. We support Java 6, and java.util.Base64 was only added in Java 8,
try {
return DatatypeConverter.printBase64Binary(v.getBytes("UTF-8")).replace("+", "-").replace("/", "_");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e); // Unreachable.
}
}

/**
* Returns a grouping key with the instance label set to the machine's IP address.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void testInvalidURLThrowsRuntimeException() {
public void testMultipleSlashesAreStrippedFromURL() {
final PushGateway pushGateway = new PushGateway("example.com:1234/context///path//");
Assert.assertEquals(
"http://example.com:1234/context/path/metrics/job/",
"http://example.com:1234/context/path/metrics/",
pushGateway.gatewayBaseURL
);
}
Expand Down Expand Up @@ -106,9 +106,9 @@ public void testPushWithGroupingKeyWithSlashes() throws IOException {
mockServerClient.when(
request()
.withMethod("PUT")
.withPath("/metrics/job/a%2Fb/l/v/l2/v%2F2")
.withPath("/metrics/job@base64/YS9i/l/v/l2@base64/75-_Lw==")
).respond(response().withStatusCode(202));
groupingKey.put("l2", "v/2");
groupingKey.put("l2", "\uF7FF/");
pg.push(registry, "a/b", groupingKey);
}

Expand Down Expand Up @@ -219,7 +219,7 @@ public void testOldPushWithSlashes() throws IOException {
mockServerClient.when(
request()
.withMethod("PUT")
.withPath("/metrics/job/a%2Fb/instance/c%2Fd")
.withPath("/metrics/job@base64/YS9i/instance@base64/Yy9k")
).respond(response().withStatusCode(202));
pg.push(registry, "a/b", "c/d");
}
Expand Down