-
Notifications
You must be signed in to change notification settings - Fork 13.9k
[FLINK-27661][Runtime / Metrics] PrometheusPushGatewayReporter support pushgateway http authentication #27576
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,18 +19,27 @@ | |
| package org.apache.flink.metrics.prometheus; | ||
|
|
||
| import org.apache.flink.metrics.MetricConfig; | ||
| import org.apache.flink.testutils.logging.LoggerAuditingExtension; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.extension.RegisterExtension; | ||
| import org.slf4j.event.Level; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| import static org.apache.flink.metrics.prometheus.PrometheusPushGatewayReporterOptions.HOST_URL; | ||
| import static org.apache.flink.metrics.prometheus.PrometheusPushGatewayReporterOptions.PASSWORD; | ||
| import static org.apache.flink.metrics.prometheus.PrometheusPushGatewayReporterOptions.USERNAME; | ||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
|
||
| /** Test for {@link PrometheusPushGatewayReporter}. */ | ||
| class PrometheusPushGatewayReporterTest { | ||
|
|
||
| @RegisterExtension | ||
| private final LoggerAuditingExtension loggerExtension = | ||
| new LoggerAuditingExtension(PrometheusPushGatewayReporterFactory.class, Level.WARN); | ||
|
|
||
| @Test | ||
| void testParseGroupingKey() { | ||
| Map<String, String> groupingKey = | ||
|
|
@@ -68,4 +77,66 @@ void testConnectToPushGatewayThrowsExceptionWithoutHostInformation() { | |
| assertThatThrownBy(() -> factory.createMetricReporter(metricConfig)) | ||
| .isInstanceOf(IllegalArgumentException.class); | ||
| } | ||
|
|
||
| @Test | ||
| void testBasicAuthNotEnabledWithoutCredentials() { | ||
| PrometheusPushGatewayReporterFactory factory = new PrometheusPushGatewayReporterFactory(); | ||
| MetricConfig metricConfig = new MetricConfig(); | ||
| metricConfig.setProperty(HOST_URL.key(), "http://localhost:9091"); | ||
| // No authentication configured - should create reporter successfully without Basic Auth | ||
| PrometheusPushGatewayReporter reporter = factory.createMetricReporter(metricConfig); | ||
| assertThat(reporter).isNotNull(); | ||
| assertThat(reporter.hostUrl.toString()).isEqualTo("http://localhost:9091"); | ||
| assertThat(reporter.basicAuthEnabled).isFalse(); | ||
| } | ||
|
|
||
| @Test | ||
| void testBasicAuthNotEnabledWithOnlyUsername() { | ||
| PrometheusPushGatewayReporterFactory factory = new PrometheusPushGatewayReporterFactory(); | ||
| MetricConfig metricConfig = new MetricConfig(); | ||
| metricConfig.setProperty(HOST_URL.key(), "http://localhost:9091"); | ||
| metricConfig.setProperty(USERNAME.key(), "flink-user"); | ||
| // Only username configured - should create reporter successfully without Basic Auth | ||
| PrometheusPushGatewayReporter reporter = factory.createMetricReporter(metricConfig); | ||
| assertThat(reporter).isNotNull(); | ||
| assertThat(reporter.basicAuthEnabled).isFalse(); | ||
| // Verify warning log is emitted | ||
| assertThat(loggerExtension.getMessages()) | ||
| .anyMatch( | ||
| msg -> | ||
| msg.contains("Both username and password must be configured") | ||
| && msg.contains("Currently only username is configured")); | ||
| } | ||
|
|
||
| @Test | ||
| void testBasicAuthNotEnabledWithOnlyPassword() { | ||
| PrometheusPushGatewayReporterFactory factory = new PrometheusPushGatewayReporterFactory(); | ||
| MetricConfig metricConfig = new MetricConfig(); | ||
| metricConfig.setProperty(HOST_URL.key(), "http://localhost:9091"); | ||
| metricConfig.setProperty(PASSWORD.key(), "flink-password"); | ||
| // Only password configured - should create reporter successfully without Basic Auth | ||
| PrometheusPushGatewayReporter reporter = factory.createMetricReporter(metricConfig); | ||
| assertThat(reporter).isNotNull(); | ||
| assertThat(reporter.basicAuthEnabled).isFalse(); | ||
| // Verify warning log is emitted | ||
| assertThat(loggerExtension.getMessages()) | ||
| .anyMatch( | ||
| msg -> | ||
| msg.contains("Both username and password must be configured") | ||
| && msg.contains("Currently only password is configured")); | ||
| } | ||
|
|
||
| @Test | ||
| void testBasicAuthEnabledWithBothCredentials() { | ||
| PrometheusPushGatewayReporterFactory factory = new PrometheusPushGatewayReporterFactory(); | ||
| MetricConfig metricConfig = new MetricConfig(); | ||
| metricConfig.setProperty(HOST_URL.key(), "http://localhost:9091"); | ||
| metricConfig.setProperty(USERNAME.key(), "flink-user"); | ||
| metricConfig.setProperty(PASSWORD.key(), "flink-password"); | ||
| // Both username and password configured - Basic Auth should be enabled | ||
| PrometheusPushGatewayReporter reporter = factory.createMetricReporter(metricConfig); | ||
|
Contributor
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. I think we need to add another assertion to this test to verify that authentication was actually enabled. We may want to consider following a pattern similar to Either that or rely on a reflection based approach to inspect the underlying connection factory.
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. Thanks for the suggestion! I've added a @VisibleForTesting boolean basicAuthEnabled field following the same pattern as hostUrl. Now all four basic auth test cases verify this property: |
||
| assertThat(reporter).isNotNull(); | ||
| assertThat(reporter.hostUrl.toString()).isEqualTo("http://localhost:9091"); | ||
| assertThat(reporter.basicAuthEnabled).isTrue(); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.