Skip to content

Commit b24ea69

Browse files
authored
[hotfix-1786][chunjun-metrics-prometheus] Fix uploading of startLocation and endLocation metrics to PushGateway (#1894)
1 parent 47e517f commit b24ea69

File tree

1 file changed

+19
-1
lines changed
  • chunjun-metrics/chunjun-metrics-prometheus/src/main/java/com/dtstack/chunjun/metrics/prometheus

1 file changed

+19
-1
lines changed

chunjun-metrics/chunjun-metrics-prometheus/src/main/java/com/dtstack/chunjun/metrics/prometheus/PrometheusReport.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848

4949
import java.io.IOException;
5050
import java.lang.reflect.Field;
51+
import java.net.MalformedURLException;
52+
import java.net.URL;
5153
import java.util.AbstractMap;
5254
import java.util.ArrayList;
5355
import java.util.Arrays;
@@ -70,6 +72,8 @@ public class PrometheusReport extends CustomReporter {
7072

7173
private Configuration configuration;
7274

75+
// 如果是 Flink1.16 或更高版本,PrometheusPushGateway 主机名和端口号的配置项变成了 metrics.reporter.promgateway.hostUrl
76+
private static final String KEY_HOST_URL = "metrics.reporter.promgateway.hostUrl";
7377
private static final String KEY_HOST = "metrics.reporter.promgateway.host";
7478
private static final String KEY_PORT = "metrics.reporter.promgateway.port";
7579
private static final String KEY_JOB_NAME = "metrics.reporter.promgateway.jobName";
@@ -79,7 +83,7 @@ public class PrometheusReport extends CustomReporter {
7983
"metrics.reporter.promgateway.deleteOnShutdown";
8084

8185
private static final char SCOPE_SEPARATOR = '_';
82-
private static final String SCOPE_PREFIX = "org/apache/flink" + SCOPE_SEPARATOR;
86+
private static final String SCOPE_PREFIX = "flink" + SCOPE_SEPARATOR;
8387

8488
private final Map<String, AbstractMap.SimpleImmutableEntry<Collector, Integer>>
8589
collectorsWithCountByMetricName = new HashMap<>();
@@ -107,8 +111,22 @@ private void initConfiguration() {
107111

108112
@Override
109113
public void open() {
114+
// 尝试从新的配置项读取,如果未配置则使用旧的配置项
115+
String hostUrl = configuration.getString(KEY_HOST_URL, "");
110116
String host = configuration.getString(KEY_HOST, null);
111117
int port = configuration.getInteger(KEY_PORT, 0);
118+
// 解析 hostUrl 如果有
119+
if (!StringUtils.isNullOrWhitespaceOnly(hostUrl)) {
120+
try {
121+
URL url = new URL(hostUrl);
122+
host = url.getHost();
123+
port = url.getPort();
124+
} catch (MalformedURLException e) {
125+
log.error("Error parsing Prometheus PushGateway URL: {}", hostUrl, e);
126+
return; // URL 格式错误则终止执行
127+
}
128+
}
129+
112130
String configuredJobName = configuration.getString(KEY_JOB_NAME, "jiangboJob");
113131
boolean randomSuffix = configuration.getBoolean(KEY_RANDOM_JOB_NAME_SUFFIX, false);
114132
deleteOnShutdown = configuration.getBoolean(KEY_DELETE_ON_SHUTDOWN, true);

0 commit comments

Comments
 (0)