From d7c090d974455446b0396b8006d93920be062955 Mon Sep 17 00:00:00 2001 From: Martin Chodur Date: Tue, 18 Jun 2024 15:10:26 +0200 Subject: [PATCH 1/3] feat: add bearerToken builder to pushgateway exporter Signed-off-by: Martin Chodur --- .../metrics/exporter/pushgateway/PushGateway.java | 11 +++++++++++ 1 file changed, 11 insertions(+) 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. From 19555fe39fcf931e927d2f262b5e4283972f9073 Mon Sep 17 00:00:00 2001 From: Martin Chodur Date: Tue, 18 Jun 2024 15:21:53 +0200 Subject: [PATCH 2/3] feat: add tests and docs Signed-off-by: Martin Chodur --- docs/content/exporters/pushgateway.md | 17 ++++++- .../it/pushgateway/PushGatewayTestApp.java | 12 ++++- .../metrics/it/pushgateway/PushGatewayIT.java | 21 ++++++++ .../resources/prometheus-bearertoken.yaml | 11 +++++ .../BearerTokenPushGatewayTest.java | 48 +++++++++++++++++++ 5 files changed, 107 insertions(+), 2 deletions(-) create mode 100644 integration-tests/it-pushgateway/src/test/resources/prometheus-bearertoken.yaml create mode 100644 prometheus-metrics-exporter-pushgateway/src/test/java/io/prometheus/metrics/exporter/pushgateway/BearerTokenPushGatewayTest.java 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..8afd4314c 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 @@ -73,6 +73,16 @@ private static void runBasicAuthTest() throws IOException { System.out.println("Push successful."); } + private static void runBearerTokenTest() throws IOException { + makeMetrics(); + PushGateway pg = PushGateway.builder() + .bearerToken("xxx") + .build(); + System.out.println("Pushing metrics..."); + pg.push(); + System.out.println("Push successful."); + } + private static void runSslTest() throws IOException { makeMetrics(); PushGateway pg = PushGateway.builder() @@ -128,4 +138,4 @@ private static void makeMetrics() { .register(); duration.set(0.5); } -} \ No newline at end of file +} diff --git a/integration-tests/it-pushgateway/src/test/java/io/prometheus/metrics/it/pushgateway/PushGatewayIT.java b/integration-tests/it-pushgateway/src/test/java/io/prometheus/metrics/it/pushgateway/PushGatewayIT.java index 6b89a8c58..a3de16a5d 100644 --- a/integration-tests/it-pushgateway/src/test/java/io/prometheus/metrics/it/pushgateway/PushGatewayIT.java +++ b/integration-tests/it-pushgateway/src/test/java/io/prometheus/metrics/it/pushgateway/PushGatewayIT.java @@ -118,6 +118,27 @@ public void testBasicAuth() throws IOException, InterruptedException { assertMetrics(); } + @Test + public void testBearerToken() throws IOException, InterruptedException { + pushGatewayContainer + .withCopyFileToContainer(MountableFile.forClasspathResource("/pushgateway-bearertoken.yaml"), "/pushgateway/pushgateway-bearertoken.yaml") + .withCommand("--web.config.file", "pushgateway-bearertoken.yaml") + .start(); + sampleAppContainer + .withCommand("java", + "-Dio.prometheus.exporter.pushgateway.address=pushgateway:9091", + "-jar", + "/app/pushgateway-test-app.jar", + "bearertoken" + ).start(); + prometheusContainer + .withCopyFileToContainer(MountableFile.forClasspathResource("/prometheus-bearertoken.yaml"), "/etc/prometheus/prometheus.yml") + .start(); + awaitTermination(sampleAppContainer, 10, TimeUnit.SECONDS); + assertMetrics(); + } + + @Test public void testSsl() throws InterruptedException, IOException { pushGatewayContainer diff --git a/integration-tests/it-pushgateway/src/test/resources/prometheus-bearertoken.yaml b/integration-tests/it-pushgateway/src/test/resources/prometheus-bearertoken.yaml new file mode 100644 index 000000000..8258b5836 --- /dev/null +++ b/integration-tests/it-pushgateway/src/test/resources/prometheus-bearertoken.yaml @@ -0,0 +1,11 @@ +global: + scrape_interval: 5s + +scrape_configs: + - job_name: "push" + honor_labels: true + static_configs: + - targets: ["pushgateway:9091"] + authorization: + type: "Bearer" + credentials: "xxx" 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(); + } +} From 78e1e12cd3598a58ceda7902315114cb06a18003 Mon Sep 17 00:00:00 2001 From: Martin Chodur Date: Tue, 18 Jun 2024 15:37:26 +0200 Subject: [PATCH 3/3] fix: drop integration tests for bearer token Signed-off-by: Martin Chodur --- .../it/pushgateway/PushGatewayTestApp.java | 10 --------- .../metrics/it/pushgateway/PushGatewayIT.java | 21 ------------------- .../resources/prometheus-bearertoken.yaml | 11 ---------- 3 files changed, 42 deletions(-) delete mode 100644 integration-tests/it-pushgateway/src/test/resources/prometheus-bearertoken.yaml 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 8afd4314c..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 @@ -73,16 +73,6 @@ private static void runBasicAuthTest() throws IOException { System.out.println("Push successful."); } - private static void runBearerTokenTest() throws IOException { - makeMetrics(); - PushGateway pg = PushGateway.builder() - .bearerToken("xxx") - .build(); - System.out.println("Pushing metrics..."); - pg.push(); - System.out.println("Push successful."); - } - private static void runSslTest() throws IOException { makeMetrics(); PushGateway pg = PushGateway.builder() diff --git a/integration-tests/it-pushgateway/src/test/java/io/prometheus/metrics/it/pushgateway/PushGatewayIT.java b/integration-tests/it-pushgateway/src/test/java/io/prometheus/metrics/it/pushgateway/PushGatewayIT.java index a3de16a5d..6b89a8c58 100644 --- a/integration-tests/it-pushgateway/src/test/java/io/prometheus/metrics/it/pushgateway/PushGatewayIT.java +++ b/integration-tests/it-pushgateway/src/test/java/io/prometheus/metrics/it/pushgateway/PushGatewayIT.java @@ -118,27 +118,6 @@ public void testBasicAuth() throws IOException, InterruptedException { assertMetrics(); } - @Test - public void testBearerToken() throws IOException, InterruptedException { - pushGatewayContainer - .withCopyFileToContainer(MountableFile.forClasspathResource("/pushgateway-bearertoken.yaml"), "/pushgateway/pushgateway-bearertoken.yaml") - .withCommand("--web.config.file", "pushgateway-bearertoken.yaml") - .start(); - sampleAppContainer - .withCommand("java", - "-Dio.prometheus.exporter.pushgateway.address=pushgateway:9091", - "-jar", - "/app/pushgateway-test-app.jar", - "bearertoken" - ).start(); - prometheusContainer - .withCopyFileToContainer(MountableFile.forClasspathResource("/prometheus-bearertoken.yaml"), "/etc/prometheus/prometheus.yml") - .start(); - awaitTermination(sampleAppContainer, 10, TimeUnit.SECONDS); - assertMetrics(); - } - - @Test public void testSsl() throws InterruptedException, IOException { pushGatewayContainer diff --git a/integration-tests/it-pushgateway/src/test/resources/prometheus-bearertoken.yaml b/integration-tests/it-pushgateway/src/test/resources/prometheus-bearertoken.yaml deleted file mode 100644 index 8258b5836..000000000 --- a/integration-tests/it-pushgateway/src/test/resources/prometheus-bearertoken.yaml +++ /dev/null @@ -1,11 +0,0 @@ -global: - scrape_interval: 5s - -scrape_configs: - - job_name: "push" - honor_labels: true - static_configs: - - targets: ["pushgateway:9091"] - authorization: - type: "Bearer" - credentials: "xxx"