Skip to content

Commit

Permalink
feat: add bearerToken builder to pushgateway exporter (#968)
Browse files Browse the repository at this point in the history
* feat: add bearerToken builder to pushgateway exporter

Signed-off-by: Martin Chodur <[email protected]>

* feat: add tests and docs

Signed-off-by: Martin Chodur <[email protected]>

* fix: drop integration tests for bearer token

Signed-off-by: Martin Chodur <[email protected]>

---------

Signed-off-by: Martin Chodur <[email protected]>
  • Loading branch information
FUSAKLA authored Jul 18, 2024
1 parent 5867b79 commit 594a9de
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 2 deletions.
17 changes: 16 additions & 1 deletion docs/content/exporters/pushgateway.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
---

Expand All @@ -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).
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).
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,4 @@ private static void makeMetrics() {
.register();
duration.set(0.5);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}

0 comments on commit 594a9de

Please sign in to comment.