Merge pull request #31104 from cansi

* gh-31104:
  Polish "Add PUT shutdown operation for Prometheus Push Gateway"
  Add PUT shutdown operation for Prometheus Push Gateway

Closes gh-31104
This commit is contained in:
Andy Wilkinson
2022-05-27 09:12:36 +01:00
2 changed files with 54 additions and 8 deletions

View File

@@ -98,15 +98,24 @@ 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);
}
}
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); 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);
} }
} }
@@ -133,7 +142,11 @@ public class PrometheusPushGatewayManager {
this.scheduled.cancel(false); this.scheduled.cancel(false);
switch (shutdownOperation) { switch (shutdownOperation) {
case PUSH: case PUSH:
push(); case POST:
post();
break;
case PUT:
put();
break; break;
case DELETE: case DELETE:
delete(); delete();
@@ -152,12 +165,24 @@ public class PrometheusPushGatewayManager {
NONE, 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, PUSH,
/** /**
* Perform a 'delete' before shutdown. * Perform a PUT before shutdown.
*/
PUT,
/**
* 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,26 @@ 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
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 @Test
void shutdownWhenShutdownOperationIsDeletePerformsDeleteOnShutdown() throws Exception { void shutdownWhenShutdownOperationIsDeletePerformsDeleteOnShutdown() throws Exception {
givenScheduleAtFixedRateWithReturnFuture(); givenScheduleAtFixedRateWithReturnFuture();