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.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);
}
}
@ -115,7 +115,7 @@ public class PrometheusPushGatewayManager {
this.pushGateway.push(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 PUT of metrics to Prometheus Pushgateway", ex);
}
}
@ -124,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);
}
}
@ -142,7 +142,8 @@ public class PrometheusPushGatewayManager {
this.scheduled.cancel(false);
switch (shutdownOperation) {
case PUSH:
push();
case POST:
post();
break;
case PUT:
put();
@ -164,17 +165,24 @@ public class PrometheusPushGatewayManager {
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,
/**
* Perform a 'push' with PUT method before shutdown.
* Perform a PUT before shutdown.
*/
PUT,
/**
* Perform a 'delete' before shutdown.
* Perform a DELETE before shutdown.
*/
DELETE

View File

@ -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,16 @@ 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();