From c5fcc03bdfd50f2624ba0a3d39f0d5e5e856deef Mon Sep 17 00:00:00 2001 From: DENNIS <599166320@qq.com> Date: Thu, 3 Nov 2022 18:50:27 +0800 Subject: [PATCH] PrometheusEmitter NullPointerException fix (#13286) * PrometheusEmitter NullPointerException fix * Improved null value judgment in pushMetric * Delete meaningless judgments about namespace * Delete unnecessary @Nullable above namespace attribute --- .../emitter/prometheus/PrometheusEmitter.java | 25 +++++++++---------- .../prometheus/PrometheusEmitterConfig.java | 1 - 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/extensions-contrib/prometheus-emitter/src/main/java/org/apache/druid/emitter/prometheus/PrometheusEmitter.java b/extensions-contrib/prometheus-emitter/src/main/java/org/apache/druid/emitter/prometheus/PrometheusEmitter.java index 8a9840191d6..c2909ec319f 100644 --- a/extensions-contrib/prometheus-emitter/src/main/java/org/apache/druid/emitter/prometheus/PrometheusEmitter.java +++ b/extensions-contrib/prometheus-emitter/src/main/java/org/apache/druid/emitter/prometheus/PrometheusEmitter.java @@ -57,7 +57,7 @@ public class PrometheusEmitter implements Emitter private HTTPServer server; private PushGateway pushGateway; - private String identifier; + private volatile String identifier; private ScheduledExecutorService exec; static PrometheusEmitter of(PrometheusEmitterConfig config) @@ -170,27 +170,26 @@ public class PrometheusEmitter implements Emitter private void pushMetric() { + if (pushGateway == null || identifier == null) { + return; + } Map map = metrics.getRegisteredMetrics(); CollectorRegistry metrics = new CollectorRegistry(); - if (config.getNamespace() != null) { - try { - for (DimensionsAndCollector collector : map.values()) { - metrics.register(collector.getCollector()); - } - pushGateway.push(metrics, config.getNamespace(), ImmutableMap.of(config.getNamespace(), identifier)); - } - catch (IOException e) { - log.error(e, "Unable to push prometheus metrics to pushGateway"); + try { + for (DimensionsAndCollector collector : map.values()) { + metrics.register(collector.getCollector()); } + pushGateway.push(metrics, config.getNamespace(), ImmutableMap.of(config.getNamespace(), identifier)); + } + catch (IOException e) { + log.error(e, "Unable to push prometheus metrics to pushGateway"); } } @Override public void flush() { - if (pushGateway != null) { - pushMetric(); - } + pushMetric(); } @Override diff --git a/extensions-contrib/prometheus-emitter/src/main/java/org/apache/druid/emitter/prometheus/PrometheusEmitterConfig.java b/extensions-contrib/prometheus-emitter/src/main/java/org/apache/druid/emitter/prometheus/PrometheusEmitterConfig.java index ac4e15355ab..2dbbe5662bf 100644 --- a/extensions-contrib/prometheus-emitter/src/main/java/org/apache/druid/emitter/prometheus/PrometheusEmitterConfig.java +++ b/extensions-contrib/prometheus-emitter/src/main/java/org/apache/druid/emitter/prometheus/PrometheusEmitterConfig.java @@ -39,7 +39,6 @@ public class PrometheusEmitterConfig private final Strategy strategy; @JsonProperty - @Nullable private final String namespace; @JsonProperty