Skip to content

Commit 594a9de

Browse files
authored
feat: add bearerToken builder to pushgateway exporter (#968)
* feat: add bearerToken builder to pushgateway exporter Signed-off-by: Martin Chodur <m.chodur@seznam.cz> * feat: add tests and docs Signed-off-by: Martin Chodur <m.chodur@seznam.cz> * fix: drop integration tests for bearer token Signed-off-by: Martin Chodur <m.chodur@seznam.cz> --------- Signed-off-by: Martin Chodur <m.chodur@seznam.cz>
1 parent 5867b79 commit 594a9de

File tree

4 files changed

+76
-2
lines changed

4 files changed

+76
-2
lines changed

docs/content/exporters/pushgateway.md

+16-1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,21 @@ PushGateway pushGateway = PushGateway.builder()
8080

8181
The `PushGatewayTestApp` in `integration-tests/it-pushgateway` has a complete example of this.
8282

83+
Bearer token
84+
----------
85+
86+
The [PushGateway](/client_java/api/io/prometheus/metrics/exporter/pushgateway/PushGateway.html) supports Bearer token authentication.
87+
88+
```java
89+
PushGateway pushGateway = PushGateway.builder()
90+
.job("example")
91+
.bearerToken("my_token")
92+
.build();
93+
```
94+
95+
The `PushGatewayTestApp` in `integration-tests/it-pushgateway` has a complete example of this.
96+
97+
8398
SSL
8499
---
85100

@@ -100,4 +115,4 @@ The `PushGatewayTestApp` in `integration-tests/it-pushgateway` has a complete ex
100115
Configuration Properties
101116
------------------------
102117

103-
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).
118+
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).

integration-tests/it-pushgateway/src/main/java/io/prometheus/metrics/it/pushgateway/PushGatewayTestApp.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,4 @@ private static void makeMetrics() {
128128
.register();
129129
duration.set(0.5);
130130
}
131-
}
131+
}

prometheus-metrics-exporter-pushgateway/src/main/java/io/prometheus/metrics/exporter/pushgateway/PushGateway.java

+11
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,17 @@ public Builder basicAuth(String user, String password) {
278278
return this;
279279
}
280280

281+
/**
282+
* Bearer token authorization when pushing to the Pushgateway.
283+
*/
284+
public Builder bearerToken(String token) {
285+
if (token == null) {
286+
throw new NullPointerException();
287+
}
288+
requestHeaders.put("Authorization", String.format("Bearer %s", token));
289+
return this;
290+
}
291+
281292
/**
282293
* Specify if metrics should be pushed using HTTP or HTTPS. Default is HTTP.
283294
* Can be overwritten at runtime with the {@code io.prometheus.exporter.pushgateway.scheme} property.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package io.prometheus.metrics.exporter.pushgateway;
2+
3+
import io.prometheus.metrics.core.metrics.Gauge;
4+
import io.prometheus.metrics.model.registry.PrometheusRegistry;
5+
import org.junit.Before;
6+
import org.junit.Rule;
7+
import org.junit.Test;
8+
import org.mockserver.client.MockServerClient;
9+
import org.mockserver.junit.MockServerRule;
10+
11+
import java.io.IOException;
12+
13+
import static org.mockserver.model.HttpRequest.request;
14+
import static org.mockserver.model.HttpResponse.response;
15+
16+
public class BearerTokenPushGatewayTest {
17+
18+
@Rule
19+
public MockServerRule mockServerRule = new MockServerRule(this);
20+
private MockServerClient mockServerClient;
21+
22+
PrometheusRegistry registry;
23+
Gauge gauge;
24+
PushGateway pushGateway;
25+
26+
@Before
27+
public void setUp() {
28+
registry = new PrometheusRegistry();
29+
gauge = Gauge.builder().name("g").help("help").build();
30+
pushGateway = PushGateway.builder()
31+
.address("localhost:" + mockServerRule.getPort())
32+
.bearerToken("xxx")
33+
.registry(registry)
34+
.job("j")
35+
.build();
36+
}
37+
38+
@Test
39+
public void testAuthorizedPush() throws IOException {
40+
mockServerClient.when(
41+
request()
42+
.withMethod("PUT")
43+
.withHeader("Authorization", "Bearer xxx")
44+
.withPath("/metrics/job/j")
45+
).respond(response().withStatusCode(202));
46+
pushGateway.push();
47+
}
48+
}

0 commit comments

Comments
 (0)