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 e02182783e..504f066f62 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 @@ -98,15 +98,24 @@ public class PrometheusPushGatewayManager { this.groupingKey = groupingKey; this.shutdownOperation = (shutdownOperation != null) ? shutdownOperation : ShutdownOperation.NONE; this.scheduler = scheduler; - this.scheduled = this.scheduler.scheduleAtFixedRate(this::push, pushRate); + this.scheduled = this.scheduler.scheduleAtFixedRate(this::post, pushRate); } - private void push() { + private void post() { try { this.pushGateway.pushAdd(this.registry, this.job, this.groupingKey); } catch (Throwable ex) { - logger.warn("Unexpected exception thrown while pushing metrics to Prometheus Pushgateway", ex); + logger.warn("Unexpected exception thrown by POST of metrics to Prometheus Pushgateway", ex); + } + } + + private void put() { + try { + this.pushGateway.push(this.registry, this.job, this.groupingKey); + } + catch (Throwable ex) { + logger.warn("Unexpected exception thrown by PUT of metrics to Prometheus Pushgateway", ex); } } @@ -115,7 +124,7 @@ public class PrometheusPushGatewayManager { this.pushGateway.delete(this.job, this.groupingKey); } catch (Throwable ex) { - logger.warn("Unexpected exception thrown while deleting metrics from Prometheus Pushgateway", ex); + logger.warn("Unexpected exception thrown by DELETE of metrics from Prometheus Pushgateway", ex); } } @@ -133,7 +142,11 @@ public class PrometheusPushGatewayManager { this.scheduled.cancel(false); switch (shutdownOperation) { case PUSH: - push(); + case POST: + post(); + break; + case PUT: + put(); break; case DELETE: delete(); @@ -152,12 +165,24 @@ public class PrometheusPushGatewayManager { NONE, /** - * Perform a 'push' before shutdown. + * Perform a POST before shutdown. */ + POST, + + /** + * Perform a POST before shutdown. + * @deprecated since 3.0.0 for removal in 3.2.0 in favor of {@link #POST}. + */ + @Deprecated PUSH, /** - * Perform a 'delete' before shutdown. + * Perform a PUT before shutdown. + */ + PUT, + + /** + * Perform a DELETE before shutdown. */ DELETE 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 7bb3ceef2f..b35cfecb2a 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 @@ -135,7 +135,8 @@ class PrometheusPushGatewayManagerTests { } @Test - void shutdownWhenShutdownOperationIsPushPerformsPushOnShutdown() throws Exception { + @SuppressWarnings("deprecation") + void shutdownWhenShutdownOperationIsPushPerformsPushAddOnShutdown() throws Exception { givenScheduleAtFixedRateWithReturnFuture(); PrometheusPushGatewayManager manager = new PrometheusPushGatewayManager(this.pushGateway, this.registry, this.scheduler, this.pushRate, "job", this.groupingKey, ShutdownOperation.PUSH); @@ -144,6 +145,26 @@ class PrometheusPushGatewayManagerTests { then(this.pushGateway).should().pushAdd(this.registry, "job", this.groupingKey); } + @Test + void shutdownWhenShutdownOperationIsPostPerformsPushAddOnShutdown() throws Exception { + givenScheduleAtFixedRateWithReturnFuture(); + PrometheusPushGatewayManager manager = new PrometheusPushGatewayManager(this.pushGateway, this.registry, + this.scheduler, this.pushRate, "job", this.groupingKey, ShutdownOperation.POST); + manager.shutdown(); + then(this.future).should().cancel(false); + then(this.pushGateway).should().pushAdd(this.registry, "job", this.groupingKey); + } + + @Test + void shutdownWhenShutdownOperationIsPutPerformsPushOnShutdown() throws Exception { + givenScheduleAtFixedRateWithReturnFuture(); + PrometheusPushGatewayManager manager = new PrometheusPushGatewayManager(this.pushGateway, this.registry, + this.scheduler, this.pushRate, "job", this.groupingKey, ShutdownOperation.PUT); + manager.shutdown(); + then(this.future).should().cancel(false); + then(this.pushGateway).should().push(this.registry, "job", this.groupingKey); + } + @Test void shutdownWhenShutdownOperationIsDeletePerformsDeleteOnShutdown() throws Exception { givenScheduleAtFixedRateWithReturnFuture();