Skip to content

Commit 9b5e114

Browse files
committed
Add onFlush callback to Metrics interface
1 parent 3e3141f commit 9b5e114

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

core/src/main/java/dev/faststats/core/Metrics.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,18 @@ interface Factory<T, F extends Factory<T, F>> {
8383
@Contract(mutates = "this")
8484
F addChart(Chart<?> chart) throws IllegalArgumentException;
8585

86+
/**
87+
* Sets the flush callback for this metrics instance.
88+
* <p>
89+
* This callback will be invoked when the metrics have been submitted to, and accepted by, the metrics server.
90+
*
91+
* @param flush the flush callback
92+
* @return the metrics factory
93+
* @since 0.15.0
94+
*/
95+
@Contract(mutates = "this")
96+
F onFlush(Runnable flush);
97+
8698
/**
8799
* Sets the error tracker for this metrics instance.
88100
* <p>

core/src/main/java/dev/faststats/core/SimpleMetrics.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public abstract class SimpleMetrics implements Metrics {
4545
private final Config config;
4646
private final @Token String token;
4747
private final @Nullable ErrorTracker tracker;
48+
private final @Nullable Runnable flush;
4849
private final URI url;
4950
private final boolean debug;
5051

@@ -58,6 +59,7 @@ protected SimpleMetrics(final Factory<?, ?> factory, final Config config) throws
5859
this.debug = factory.debug || Boolean.getBoolean("faststats.debug") || config.debug();
5960
this.token = factory.token;
6061
this.tracker = config.errorTracking ? factory.tracker : null;
62+
this.flush = factory.flush;
6163
this.url = factory.url;
6264
}
6365

@@ -72,6 +74,7 @@ protected SimpleMetrics(
7274
final Set<Chart<?>> charts,
7375
@Token final String token,
7476
@Nullable final ErrorTracker tracker,
77+
@Nullable final Runnable flush,
7578
final URI url,
7679
final boolean debug
7780
) {
@@ -84,6 +87,7 @@ protected SimpleMetrics(
8487
this.debug = debug;
8588
this.token = token;
8689
this.tracker = tracker;
90+
this.flush = flush;
8791
this.url = url;
8892
}
8993

@@ -201,6 +205,7 @@ private boolean submitNow() throws IOException {
201205
if (statusCode >= 200 && statusCode < 300) {
202206
info("Metrics submitted with status code: " + statusCode + " (" + body + ")");
203207
getErrorTracker().map(SimpleErrorTracker.class::cast).ifPresent(SimpleErrorTracker::clear);
208+
if (flush != null) flush.run();
204209
return true;
205210
} else if (statusCode >= 300 && statusCode < 400) {
206211
warn("Received redirect response from metrics server: " + statusCode + " (" + body + ")");
@@ -320,6 +325,7 @@ public abstract static class Factory<T, F extends Metrics.Factory<T, F>> impleme
320325
private final Set<Chart<?>> charts = new HashSet<>(0);
321326
private URI url = URI.create("https://metrics.faststats.dev/v1/collect");
322327
private @Nullable ErrorTracker tracker;
328+
private @Nullable Runnable flush;
323329
private @Nullable String token;
324330
private boolean debug = false;
325331

@@ -330,6 +336,13 @@ public F addChart(final Chart<?> chart) throws IllegalArgumentException {
330336
return (F) this;
331337
}
332338

339+
@Override
340+
@SuppressWarnings("unchecked")
341+
public F onFlush(final Runnable flush) {
342+
this.flush = flush;
343+
return (F) this;
344+
}
345+
333346
@Override
334347
@SuppressWarnings("unchecked")
335348
public F errorTracker(final ErrorTracker tracker) {

core/src/test/java/dev/faststats/MockMetrics.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
@NullMarked
1515
public class MockMetrics extends SimpleMetrics {
1616
public MockMetrics(final UUID serverId, @Token final String token, @Nullable final ErrorTracker tracker, final boolean debug) {
17-
super(new Config(serverId, true, debug, true, true, false, false), Set.of(), token, tracker, URI.create("http://localhost:5000/v1/collect"), debug);
17+
super(new Config(serverId, true, debug, true, true, false, false), Set.of(), token, tracker, null, URI.create("http://localhost:5000/v1/collect"), debug);
1818
}
1919

2020
@Override

0 commit comments

Comments
 (0)