diff --git a/docs/content/exporters/pushgateway.md b/docs/content/exporters/pushgateway.md index 8f002fba9..1ca2946b1 100644 --- a/docs/content/exporters/pushgateway.md +++ b/docs/content/exporters/pushgateway.md @@ -80,6 +80,21 @@ PushGateway pushGateway = PushGateway.builder() The `PushGatewayTestApp` in `integration-tests/it-pushgateway` has a complete example of this. +Bearer token +---------- + +The [PushGateway](/client_java/api/io/prometheus/metrics/exporter/pushgateway/PushGateway.html) supports Bearer token authentication. + +```java +PushGateway pushGateway = PushGateway.builder() + .job("example") + .bearerToken("my_token") + .build(); +``` + +The `PushGatewayTestApp` in `integration-tests/it-pushgateway` has a complete example of this. + + SSL --- @@ -100,4 +115,4 @@ The `PushGatewayTestApp` in `integration-tests/it-pushgateway` has a complete ex Configuration Properties ------------------------ -The [PushGateway](/client_java/api/io/prometheus/metrics/exporter/pushgateway/PushGateway.html) supports a couple of properties that can be configured at runtime. See [config](../../config/config). \ No newline at end of file +The [PushGateway](/client_java/api/io/prometheus/metrics/exporter/pushgateway/PushGateway.html) supports a couple of properties that can be configured at runtime. See [config](../../config/config). diff --git a/integration-tests/it-pushgateway/src/main/java/io/prometheus/metrics/it/pushgateway/PushGatewayTestApp.java b/integration-tests/it-pushgateway/src/main/java/io/prometheus/metrics/it/pushgateway/PushGatewayTestApp.java index 279f05d90..69ef63081 100644 --- a/integration-tests/it-pushgateway/src/main/java/io/prometheus/metrics/it/pushgateway/PushGatewayTestApp.java +++ b/integration-tests/it-pushgateway/src/main/java/io/prometheus/metrics/it/pushgateway/PushGatewayTestApp.java @@ -128,4 +128,4 @@ private static void makeMetrics() { .register(); duration.set(0.5); } -} \ No newline at end of file +} diff --git a/prometheus-metrics-exporter-pushgateway/src/main/java/io/prometheus/metrics/exporter/pushgateway/PushGateway.java b/prometheus-metrics-exporter-pushgateway/src/main/java/io/prometheus/metrics/exporter/pushgateway/PushGateway.java index a2d587675..0e6ab8d54 100644 --- a/prometheus-metrics-exporter-pushgateway/src/main/java/io/prometheus/metrics/exporter/pushgateway/PushGateway.java +++ b/prometheus-metrics-exporter-pushgateway/src/main/java/io/prometheus/metrics/exporter/pushgateway/PushGateway.java @@ -278,6 +278,17 @@ public Builder basicAuth(String user, String password) { return this; } + /** + * Bearer token authorization when pushing to the Pushgateway. + */ + public Builder bearerToken(String token) { + if (token == null) { + throw new NullPointerException(); + } + requestHeaders.put("Authorization", String.format("Bearer %s", token)); + return this; + } + /** * Specify if metrics should be pushed using HTTP or HTTPS. Default is HTTP. * Can be overwritten at runtime with the {@code io.prometheus.exporter.pushgateway.scheme} property. diff --git a/prometheus-metrics-exporter-pushgateway/src/test/java/io/prometheus/metrics/exporter/pushgateway/BearerTokenPushGatewayTest.java b/prometheus-metrics-exporter-pushgateway/src/test/java/io/prometheus/metrics/exporter/pushgateway/BearerTokenPushGatewayTest.java new file mode 100644 index 000000000..8687b2d55 --- /dev/null +++ b/prometheus-metrics-exporter-pushgateway/src/test/java/io/prometheus/metrics/exporter/pushgateway/BearerTokenPushGatewayTest.java @@ -0,0 +1,48 @@ +package io.prometheus.metrics.exporter.pushgateway; + +import io.prometheus.metrics.core.metrics.Gauge; +import io.prometheus.metrics.model.registry.PrometheusRegistry; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.mockserver.client.MockServerClient; +import org.mockserver.junit.MockServerRule; + +import java.io.IOException; + +import static org.mockserver.model.HttpRequest.request; +import static org.mockserver.model.HttpResponse.response; + +public class BearerTokenPushGatewayTest { + + @Rule + public MockServerRule mockServerRule = new MockServerRule(this); + private MockServerClient mockServerClient; + + PrometheusRegistry registry; + Gauge gauge; + PushGateway pushGateway; + + @Before + public void setUp() { + registry = new PrometheusRegistry(); + gauge = Gauge.builder().name("g").help("help").build(); + pushGateway = PushGateway.builder() + .address("localhost:" + mockServerRule.getPort()) + .bearerToken("xxx") + .registry(registry) + .job("j") + .build(); + } + + @Test + public void testAuthorizedPush() throws IOException { + mockServerClient.when( + request() + .withMethod("PUT") + .withHeader("Authorization", "Bearer xxx") + .withPath("/metrics/job/j") + ).respond(response().withStatusCode(202)); + pushGateway.push(); + } +}