Polish "Add PUT shutdown operation for Prometheus Push Gateway"

See gh-31104
This commit is contained in:
Andy Wilkinson 2022-05-26 20:04:09 +01:00
parent 0f99c43198
commit bf56665718
2 changed files with 29 additions and 10 deletions

View File

@ -98,15 +98,15 @@ public class PrometheusPushGatewayManager {
this.groupingKey = groupingKey; this.groupingKey = groupingKey;
this.shutdownOperation = (shutdownOperation != null) ? shutdownOperation : ShutdownOperation.NONE; this.shutdownOperation = (shutdownOperation != null) ? shutdownOperation : ShutdownOperation.NONE;
this.scheduler = scheduler; 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 { try {
this.pushGateway.pushAdd(this.registry, this.job, this.groupingKey); this.pushGateway.pushAdd(this.registry, this.job, this.groupingKey);
} }
catch (Throwable ex) { 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);
} }
} }
@ -115,7 +115,7 @@ public class PrometheusPushGatewayManager {
this.pushGateway.push(this.registry, this.job, this.groupingKey); this.pushGateway.push(this.registry, this.job, this.groupingKey);
} }
catch (Throwable ex) { catch (Throwable ex) {
logger.warn("Unexpected exception thrown while pushing metrics to Prometheus Pushgateway", ex); logger.warn("Unexpected exception thrown by PUT of metrics to Prometheus Pushgateway", ex);
} }
} }
@ -124,7 +124,7 @@ public class PrometheusPushGatewayManager {
this.pushGateway.delete(this.job, this.groupingKey); this.pushGateway.delete(this.job, this.groupingKey);
} }
catch (Throwable ex) { 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);
} }
} }
@ -142,7 +142,8 @@ public class PrometheusPushGatewayManager {
this.scheduled.cancel(false); this.scheduled.cancel(false);
switch (shutdownOperation) { switch (shutdownOperation) {
case PUSH: case PUSH:
push(); case POST:
post();
break; break;
case PUT: case PUT:
put(); put();
@ -164,17 +165,24 @@ public class PrometheusPushGatewayManager {
NONE, NONE,
/** /**
* Perform a 'push' with POST method 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, PUSH,
/** /**
* Perform a 'push' with PUT method before shutdown. * Perform a PUT before shutdown.
*/ */
PUT, PUT,
/** /**
* Perform a 'delete' before shutdown. * Perform a DELETE before shutdown.
*/ */
DELETE DELETE

View File

@ -135,7 +135,8 @@ class PrometheusPushGatewayManagerTests {
} }
@Test @Test
void shutdownWhenShutdownOperationIsPushPerformsPushOnShutdown() throws Exception { @SuppressWarnings("deprecation")
void shutdownWhenShutdownOperationIsPushPerformsPushAddOnShutdown() throws Exception {
givenScheduleAtFixedRateWithReturnFuture(); givenScheduleAtFixedRateWithReturnFuture();
PrometheusPushGatewayManager manager = new PrometheusPushGatewayManager(this.pushGateway, this.registry, PrometheusPushGatewayManager manager = new PrometheusPushGatewayManager(this.pushGateway, this.registry,
this.scheduler, this.pushRate, "job", this.groupingKey, ShutdownOperation.PUSH); this.scheduler, this.pushRate, "job", this.groupingKey, ShutdownOperation.PUSH);
@ -144,6 +145,16 @@ class PrometheusPushGatewayManagerTests {
then(this.pushGateway).should().pushAdd(this.registry, "job", this.groupingKey); 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 @Test
void shutdownWhenShutdownOperationIsPutPerformsPushOnShutdown() throws Exception { void shutdownWhenShutdownOperationIsPutPerformsPushOnShutdown() throws Exception {
givenScheduleAtFixedRateWithReturnFuture(); givenScheduleAtFixedRateWithReturnFuture();