@@ -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 ) {
0 commit comments