-
Notifications
You must be signed in to change notification settings - Fork 567
[metrics][docs] Support Basic Auth for Prometheus PushGateway reporter #3552
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,14 +19,21 @@ | |
|
|
||
| import org.apache.fluss.metrics.Metric; | ||
| import org.apache.fluss.metrics.reporter.ScheduledMetricReporter; | ||
| import org.apache.fluss.utils.StringUtils; | ||
|
|
||
| import io.prometheus.client.exporter.HttpConnectionFactory; | ||
| import io.prometheus.client.exporter.PushGateway; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import javax.annotation.Nullable; | ||
|
|
||
| import java.io.IOException; | ||
| import java.net.HttpURLConnection; | ||
| import java.net.URL; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.time.Duration; | ||
| import java.util.Base64; | ||
| import java.util.Map; | ||
|
|
||
| /** {@link ScheduledMetricReporter} that pushes {@link Metric Metrics} to Prometheus PushGateway. */ | ||
|
|
@@ -46,12 +53,18 @@ public PrometheusPushGatewayReporter( | |
| String jobName, | ||
| Map<String, String> groupingKey, | ||
| final boolean deleteOnShutdown, | ||
| Duration pushInterval) { | ||
| Duration pushInterval, | ||
| @Nullable String username, | ||
| @Nullable String password) { | ||
| this.pushGateway = new PushGateway(hostUrl); | ||
| this.jobName = jobName; | ||
| this.groupingKey = groupingKey; | ||
| this.deleteOnShutdown = deleteOnShutdown; | ||
| this.pushInterval = pushInterval; | ||
| if (!StringUtils.isNullOrWhitespaceOnly(username)) { | ||
| this.pushGateway.setConnectionFactory( | ||
| basicAuthConnectionFactory(username, password == null ? "" : password)); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -80,4 +93,17 @@ public void report() { | |
| LOG.warn("Could not push metrics to PushGateway.", e); | ||
| } | ||
| } | ||
|
|
||
| private static HttpConnectionFactory basicAuthConnectionFactory(String user, String password) { | ||
| final String header = | ||
| "Basic " | ||
| + Base64.getEncoder() | ||
| .encodeToString( | ||
| (user + ":" + password).getBytes(StandardCharsets.UTF_8)); | ||
|
Comment on lines
+97
to
+102
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. #3552 (comment) Reason see this. |
||
| return url -> { | ||
| HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); | ||
| connection.setRequestProperty("Authorization", header); | ||
| return connection; | ||
| }; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
190 changes: 190 additions & 0 deletions
190
.../src/test/java/org/apache/fluss/metrics/prometheus/PrometheusPushGatewayReporterTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,190 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.fluss.metrics.prometheus; | ||
|
|
||
| import org.apache.fluss.config.ConfigOptions; | ||
| import org.apache.fluss.config.Configuration; | ||
| import org.apache.fluss.config.Password; | ||
| import org.apache.fluss.metrics.reporter.MetricReporter; | ||
|
|
||
| import com.sun.net.httpserver.HttpExchange; | ||
| import com.sun.net.httpserver.HttpServer; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.net.InetSocketAddress; | ||
| import java.net.URL; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.time.Duration; | ||
| import java.util.Base64; | ||
| import java.util.Collections; | ||
| import java.util.concurrent.ArrayBlockingQueue; | ||
| import java.util.concurrent.BlockingQueue; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| class PrometheusPushGatewayReporterTest { | ||
|
|
||
| private HttpServer server; | ||
| private BlockingQueue<String> receivedAuthHeaders; | ||
|
|
||
| @BeforeEach | ||
| void startFakePushGateway() throws IOException { | ||
| receivedAuthHeaders = new ArrayBlockingQueue<>(8); | ||
| server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0); | ||
| server.createContext( | ||
| "/", | ||
| (HttpExchange exchange) -> { | ||
| // capture (possibly null) Authorization header, using empty string as absent | ||
| String auth = exchange.getRequestHeaders().getFirst("Authorization"); | ||
| receivedAuthHeaders.offer(auth == null ? "" : auth); | ||
| // drain request body so client does not block (JDK 8 compatible) | ||
| try (InputStream body = exchange.getRequestBody()) { | ||
| byte[] buf = new byte[1024]; | ||
| while (body.read(buf) != -1) { | ||
| // discard | ||
| } | ||
| } | ||
|
|
||
| exchange.sendResponseHeaders(202, -1); | ||
| exchange.close(); | ||
| }); | ||
| server.start(); | ||
| } | ||
|
|
||
| @AfterEach | ||
| void stopFakePushGateway() { | ||
| if (server != null) { | ||
| server.stop(0); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void reportSendsAuthorizationHeaderWhenBasicAuthConfigured() throws Exception { | ||
| PrometheusPushGatewayReporter reporter = | ||
| new PrometheusPushGatewayReporter( | ||
| pushGatewayUrl(), | ||
| "test-job", | ||
| Collections.emptyMap(), | ||
| false, | ||
| Duration.ofSeconds(10), | ||
| "myuser", | ||
| "mypassword"); | ||
| try { | ||
| reporter.report(); | ||
|
|
||
| String header = receivedAuthHeaders.poll(5, TimeUnit.SECONDS); | ||
| assertThat(header).isNotNull().startsWith("Basic "); | ||
|
|
||
| String decoded = | ||
| new String( | ||
| Base64.getDecoder().decode(header.substring("Basic ".length())), | ||
| StandardCharsets.UTF_8); | ||
| assertThat(decoded).isEqualTo("myuser:mypassword"); | ||
| } finally { | ||
| reporter.close(); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void reportSendsNoAuthorizationHeaderWhenBasicAuthNotConfigured() throws Exception { | ||
| PrometheusPushGatewayReporter reporter = | ||
| new PrometheusPushGatewayReporter( | ||
| pushGatewayUrl(), | ||
| "test-job", | ||
| Collections.emptyMap(), | ||
| false, | ||
| Duration.ofSeconds(10), | ||
| null, | ||
| null); | ||
| try { | ||
| reporter.report(); | ||
|
|
||
| String header = receivedAuthHeaders.poll(5, TimeUnit.SECONDS); | ||
| assertThat(header).isNotNull().isEmpty(); | ||
| } finally { | ||
| reporter.close(); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void reportSendsNoAuthorizationHeaderWhenUsernameIsBlank() throws Exception { | ||
| // password without username should NOT enable basic auth | ||
| PrometheusPushGatewayReporter reporter = | ||
| new PrometheusPushGatewayReporter( | ||
| pushGatewayUrl(), | ||
| "test-job", | ||
| Collections.emptyMap(), | ||
| false, | ||
| Duration.ofSeconds(10), | ||
| "", | ||
| "somePwd"); | ||
| try { | ||
| reporter.report(); | ||
|
|
||
| String header = receivedAuthHeaders.poll(5, TimeUnit.SECONDS); | ||
| assertThat(header).isNotNull().isEmpty(); | ||
| } finally { | ||
| reporter.close(); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void pluginCreatesReporterCarryingBasicAuth() throws Exception { | ||
| Configuration config = new Configuration(); | ||
| config.setString( | ||
| ConfigOptions.METRICS_REPORTER_PROMETHEUS_PUSHGATEWAY_HOST_URL, | ||
| pushGatewayUrl().toString()); | ||
| config.setString( | ||
| ConfigOptions.METRICS_REPORTER_PROMETHEUS_PUSHGATEWAY_JOB_NAME, "plugin-job"); | ||
| config.setString( | ||
| ConfigOptions.METRICS_REPORTER_PROMETHEUS_PUSHGATEWAY_USERNAME, "plugUser"); | ||
| config.set( | ||
| ConfigOptions.METRICS_REPORTER_PROMETHEUS_PUSHGATEWAY_PASSWORD, | ||
| new Password("plugPwd")); | ||
| config.setString( | ||
| ConfigOptions.METRICS_REPORTER_PROMETHEUS_PUSHGATEWAY_GROUPING_KEY, "k1=v1"); | ||
|
|
||
| PrometheusPushGatewayReporterPlugin plugin = new PrometheusPushGatewayReporterPlugin(); | ||
| assertThat(plugin.identifier()).isEqualTo("prometheus-push"); | ||
|
|
||
| MetricReporter reporter = plugin.createMetricReporter(config); | ||
| assertThat(reporter).isInstanceOf(PrometheusPushGatewayReporter.class); | ||
| try { | ||
| ((PrometheusPushGatewayReporter) reporter).report(); | ||
|
|
||
| String header = receivedAuthHeaders.poll(5, TimeUnit.SECONDS); | ||
| assertThat(header).isNotNull().startsWith("Basic "); | ||
| String decoded = | ||
| new String( | ||
| Base64.getDecoder().decode(header.substring("Basic ".length())), | ||
| StandardCharsets.UTF_8); | ||
| assertThat(decoded).isEqualTo("plugUser:plugPwd"); | ||
| } finally { | ||
| reporter.close(); | ||
| } | ||
| } | ||
|
|
||
| private URL pushGatewayUrl() throws IOException { | ||
| return new URL("http://127.0.0.1:" + server.getAddress().getPort()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We do not reuse Prometheus' built-in BasicAuthHttpConnectionFactory because it relies on javax.xml.bind.DatatypeConverter for Base64 encoding, which has been removed from the JDK since Java 9 (JEP 320). Using java.util.Base64 keeps this reporter compatible with JDK 8+.
This issue has been resolved at the underlying level in higher versions of the Prometheus dependency, but the current 0.8 version does not include the fix. Upgrading the version rashly may cause problems, so we resolve it here with our own implementation.