Add PUT shutdown operation for Prometheus Push Gateway

See gh-31104
This commit is contained in:
Mattia Cansirro Cortorillo
2022-05-18 20:50:15 +02:00
committed by Andy Wilkinson
parent 36a60d8e2e
commit 0f99c43198
2 changed files with 28 additions and 1 deletions

View File

@@ -110,6 +110,15 @@ public class PrometheusPushGatewayManager {
}
}
private void put() {
try {
this.pushGateway.push(this.registry, this.job, this.groupingKey);
}
catch (Throwable ex) {
logger.warn("Unexpected exception thrown while pushing metrics to Prometheus Pushgateway", ex);
}
}
private void delete() {
try {
this.pushGateway.delete(this.job, this.groupingKey);
@@ -135,6 +144,9 @@ public class PrometheusPushGatewayManager {
case PUSH:
push();
break;
case PUT:
put();
break;
case DELETE:
delete();
break;
@@ -152,10 +164,15 @@ public class PrometheusPushGatewayManager {
NONE,
/**
* Perform a 'push' before shutdown.
* Perform a 'push' with POST method before shutdown.
*/
PUSH,
/**
* Perform a 'push' with PUT method before shutdown.
*/
PUT,
/**
* Perform a 'delete' before shutdown.
*/

View File

@@ -144,6 +144,16 @@ class PrometheusPushGatewayManagerTests {
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();