From 1a84b0610e84148db2b921ad80c1417e22cfdb72 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Wed, 31 Mar 2021 17:01:08 +0100 Subject: [PATCH] Align PushGatewayManager's exception handling with PushMeterRegistry's Previously, would log an error for any exception and also stop publishing for an UnknownHostException. By constrast, Micrometer's PushMeterRegistry treats all exceptions the same, logging a warning and continuing with subsequent push attempts. This commit updates the push gateway manager's behaviour to match PushMeterRegistry. UknownHostExceptions no longer receive special treatment and push (and delete) failures are now logged as warnings rather than errors. Fixes gh-25804 --- .../prometheus/PrometheusPushGatewayManager.java | 16 +++------------- .../PrometheusPushGatewayManagerTests.java | 14 +------------- 2 files changed, 4 insertions(+), 26 deletions(-) diff --git a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/export/prometheus/PrometheusPushGatewayManager.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/export/prometheus/PrometheusPushGatewayManager.java index 1960ed6e07..466612baf8 100644 --- a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/export/prometheus/PrometheusPushGatewayManager.java +++ b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/export/prometheus/PrometheusPushGatewayManager.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,7 +16,6 @@ package org.springframework.boot.actuate.metrics.export.prometheus; -import java.net.UnknownHostException; import java.time.Duration; import java.util.Map; import java.util.concurrent.Executors; @@ -31,7 +30,6 @@ import org.apache.commons.logging.LogFactory; import org.springframework.scheduling.TaskScheduler; import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; import org.springframework.util.Assert; -import org.springframework.util.StringUtils; /** * Class that can be used to manage the pushing of metrics to a {@link PushGateway @@ -107,16 +105,8 @@ public class PrometheusPushGatewayManager { try { this.pushGateway.pushAdd(this.registry, this.job, this.groupingKey); } - catch (UnknownHostException ex) { - String host = ex.getMessage(); - String message = "Unable to locate prometheus push gateway host" - + (StringUtils.hasLength(host) ? " '" + host + "'" : "") - + ". No longer attempting metrics publication to this host"; - logger.error(message, ex); - shutdown(ShutdownOperation.NONE); - } catch (Throwable ex) { - logger.error("Unable to push metrics to Prometheus Pushgateway", ex); + logger.warn("Unexpected exception thrown while pushing metrics to Prometheus Pushgateway", ex); } } @@ -125,7 +115,7 @@ public class PrometheusPushGatewayManager { this.pushGateway.delete(this.job, this.groupingKey); } catch (Throwable ex) { - logger.error("Unable to delete metrics from Prometheus Pushgateway", ex); + logger.warn("Unexpected exception thrown while deleting metrics from Promethues Pushgateway", ex); } } diff --git a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/export/prometheus/PrometheusPushGatewayManagerTests.java b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/export/prometheus/PrometheusPushGatewayManagerTests.java index 740168a1ca..559c68eacc 100644 --- a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/export/prometheus/PrometheusPushGatewayManagerTests.java +++ b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/export/prometheus/PrometheusPushGatewayManagerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,7 +16,6 @@ package org.springframework.boot.actuate.metrics.export.prometheus; -import java.net.UnknownHostException; import java.time.Duration; import java.util.Collections; import java.util.Map; @@ -165,17 +164,6 @@ class PrometheusPushGatewayManagerTests { verifyNoInteractions(this.pushGateway); } - @Test - void pushWhenUnknownHostExceptionIsThrownDoesShutdown() throws Exception { - new PrometheusPushGatewayManager(this.pushGateway, this.registry, this.scheduler, this.pushRate, "job", - this.groupingKey, null); - verify(this.scheduler).scheduleAtFixedRate(this.task.capture(), eq(this.pushRate)); - willThrow(new UnknownHostException("foo")).given(this.pushGateway).pushAdd(this.registry, "job", - this.groupingKey); - this.task.getValue().run(); - verify(this.future).cancel(false); - } - @Test void pushDoesNotThrowException() throws Exception { new PrometheusPushGatewayManager(this.pushGateway, this.registry, this.scheduler, this.pushRate, "job",