Skip to content

Commit cad1f50

Browse files
committed
Followup: address PR feedback
- Keep `randomJobNameSuffix` config as deprecated - When `randomJobNameSuffix` is disabled (by default), group metrics using random reporter id
1 parent 7a55c41 commit cad1f50

4 files changed

Lines changed: 43 additions & 2 deletions

File tree

flink-metrics/flink-metrics-prometheus/src/main/java/org/apache/flink/metrics/prometheus/PrometheusPushGatewayReporter.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public class PrometheusPushGatewayReporter extends AbstractPrometheusReporter im
5858
PrometheusPushGatewayReporter(
5959
URL hostUrl,
6060
String jobName,
61+
boolean metricsGroupingByReporter,
6162
Map<String, String> groupingKey,
6263
final boolean deleteOnShutdown,
6364
@Nullable String username,
@@ -72,7 +73,10 @@ public class PrometheusPushGatewayReporter extends AbstractPrometheusReporter im
7273
this.basicAuthEnabled = false;
7374
}
7475
this.jobName = Preconditions.checkNotNull(jobName);
75-
this.groupingKey = new GroupingKeyMap(Preconditions.checkNotNull(groupingKey));
76+
this.groupingKey =
77+
metricsGroupingByReporter
78+
? new GroupingKeyMap(Preconditions.checkNotNull(groupingKey))
79+
: Preconditions.checkNotNull(groupingKey);
7680
this.deleteOnShutdown = deleteOnShutdown;
7781
}
7882

flink-metrics/flink-metrics-prometheus/src/main/java/org/apache/flink/metrics/prometheus/PrometheusPushGatewayReporterFactory.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.apache.flink.annotation.VisibleForTesting;
2121
import org.apache.flink.metrics.MetricConfig;
2222
import org.apache.flink.metrics.reporter.MetricReporterFactory;
23+
import org.apache.flink.util.AbstractID;
2324
import org.apache.flink.util.StringUtils;
2425

2526
import org.slf4j.Logger;
@@ -37,6 +38,7 @@
3738
import static org.apache.flink.metrics.prometheus.PrometheusPushGatewayReporterOptions.HOST_URL;
3839
import static org.apache.flink.metrics.prometheus.PrometheusPushGatewayReporterOptions.JOB_NAME;
3940
import static org.apache.flink.metrics.prometheus.PrometheusPushGatewayReporterOptions.PASSWORD;
41+
import static org.apache.flink.metrics.prometheus.PrometheusPushGatewayReporterOptions.RANDOM_JOB_NAME_SUFFIX;
4042
import static org.apache.flink.metrics.prometheus.PrometheusPushGatewayReporterOptions.USERNAME;
4143

4244
/** {@link MetricReporterFactory} for {@link PrometheusPushGatewayReporter}. */
@@ -49,6 +51,9 @@ public class PrometheusPushGatewayReporterFactory implements MetricReporterFacto
4951
public PrometheusPushGatewayReporter createMetricReporter(Properties properties) {
5052
MetricConfig metricConfig = (MetricConfig) properties;
5153
String configuredJobName = metricConfig.getString(JOB_NAME.key(), JOB_NAME.defaultValue());
54+
boolean randomSuffix =
55+
metricConfig.getBoolean(
56+
RANDOM_JOB_NAME_SUFFIX.key(), RANDOM_JOB_NAME_SUFFIX.defaultValue());
5257
boolean deleteOnShutdown =
5358
metricConfig.getBoolean(
5459
DELETE_ON_SHUTDOWN.key(), DELETE_ON_SHUTDOWN.defaultValue());
@@ -63,6 +68,10 @@ public PrometheusPushGatewayReporter createMetricReporter(Properties properties)
6368
}
6469

6570
String jobName = configuredJobName;
71+
if (randomSuffix) {
72+
jobName = configuredJobName + new AbstractID();
73+
}
74+
6675
String username = metricConfig.getString(USERNAME.key(), USERNAME.defaultValue());
6776
String password = metricConfig.getString(PASSWORD.key(), PASSWORD.defaultValue());
6877

@@ -78,15 +87,17 @@ public PrometheusPushGatewayReporter createMetricReporter(Properties properties)
7887
new PrometheusPushGatewayReporter(
7988
new URL(hostUrl),
8089
jobName,
90+
!randomSuffix,
8191
groupingKey,
8292
deleteOnShutdown,
8393
username,
8494
password);
8595

8696
LOG.info(
87-
"Configured PrometheusPushGatewayReporter with {hostUrl:{}, jobName:{}, deleteOnShutdown:{}, groupingKey:{}, basicAuth:{}}",
97+
"Configured PrometheusPushGatewayReporter with {hostUrl:{}, jobName:{}, randomJobNameSuffix:{}, deleteOnShutdown:{}, groupingKey:{}, basicAuth:{}}",
8898
hostUrl,
8999
jobName,
100+
randomSuffix,
90101
deleteOnShutdown,
91102
groupingKey,
92103
reporter.basicAuthEnabled);

flink-metrics/flink-metrics-prometheus/src/main/java/org/apache/flink/metrics/prometheus/PrometheusPushGatewayReporterOptions.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@ public class PrometheusPushGatewayReporterOptions {
4545
.defaultValue("")
4646
.withDescription("The job name under which metrics will be pushed");
4747

48+
@Deprecated
49+
public static final ConfigOption<Boolean> RANDOM_JOB_NAME_SUFFIX =
50+
ConfigOptions.key("randomJobNameSuffix")
51+
.booleanType()
52+
.defaultValue(false)
53+
.withDescription(
54+
"Specifies whether random suffixing `job` label (the old way) to avoid metric collision among reporters from taskamangers."
55+
+ " When disabled (now default) , metrics will be grouped under a random reporter id while job name is faithful to configuration."
56+
+ " This option is deprecated and it is recommended to rely on the reporter id grouping key.");
57+
4858
public static final ConfigOption<Boolean> DELETE_ON_SHUTDOWN =
4959
ConfigOptions.key("deleteOnShutdown")
5060
.booleanType()

flink-metrics/flink-metrics-prometheus/src/test/java/org/apache/flink/metrics/prometheus/PrometheusPushGatewayReporterTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.util.Map;
3131

3232
import static org.apache.flink.metrics.prometheus.PrometheusPushGatewayReporter.REPORTER_ID_GROUPPING_KEY;
33+
import static org.apache.flink.metrics.prometheus.PrometheusPushGatewayReporterOptions.GROUPING_KEY;
3334
import static org.apache.flink.metrics.prometheus.PrometheusPushGatewayReporterOptions.HOST_URL;
3435
import static org.apache.flink.metrics.prometheus.PrometheusPushGatewayReporterOptions.PASSWORD;
3536
import static org.apache.flink.metrics.prometheus.PrometheusPushGatewayReporterOptions.USERNAME;
@@ -80,6 +81,21 @@ void testGroupingKeysIteratorEndsWithReporterId() {
8081
REPORTER_ID_GROUPPING_KEY, groupingKey.reporterId()));
8182
}
8283

84+
@Test
85+
void testRejectReporterIdInUserGroupingKey() {
86+
MetricConfig metricConfig = new MetricConfig();
87+
metricConfig.setProperty(HOST_URL.key(), "http://localhost:8080");
88+
metricConfig.setProperty(GROUPING_KEY.key(), "k1=v1;" + REPORTER_ID_GROUPPING_KEY + "=123");
89+
assertThatThrownBy(
90+
() ->
91+
new PrometheusPushGatewayReporterFactory()
92+
.createMetricReporter(metricConfig))
93+
.isInstanceOf(IllegalArgumentException.class)
94+
.hasMessageContaining(
95+
"Grouping keys must not contain the reserved key: "
96+
+ REPORTER_ID_GROUPPING_KEY);
97+
}
98+
8399
@Test
84100
void testConnectToPushGatewayUsingHostUrl() {
85101
PrometheusPushGatewayReporterFactory factory = new PrometheusPushGatewayReporterFactory();

0 commit comments

Comments
 (0)