Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add bearerToken builder to pushgateway exporter #968

Merged
merged 3 commits into from
Jul 18, 2024
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
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();
}
}