PrometheusEmitter NullPointerException fix (#13286)

* PrometheusEmitter NullPointerException fix

* Improved null value judgment in pushMetric

* Delete meaningless judgments about namespace

* Delete unnecessary @Nullable above namespace attribute
This commit is contained in:
DENNIS 2022-11-03 18:50:27 +08:00 committed by GitHub
parent ccc55ef899
commit c5fcc03bdf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 14 deletions

View File

@ -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<String, DimensionsAndCollector> 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

View File

@ -39,7 +39,6 @@ public class PrometheusEmitterConfig
private final Strategy strategy;
@JsonProperty
@Nullable
private final String namespace;
@JsonProperty