22
33import com .google .gson .JsonObject ;
44import dev .faststats .core .chart .Chart ;
5+ import dev .faststats .errors .ErrorTracker ;
56import org .jetbrains .annotations .Async ;
67import org .jetbrains .annotations .Contract ;
78import org .jetbrains .annotations .MustBeInvokedByOverriders ;
@@ -52,6 +53,7 @@ public abstract class SimpleMetrics implements Metrics {
5253 private final Set <Chart <?>> charts ;
5354 private final Config config ;
5455 private final @ Token String token ;
56+ private final @ Nullable ErrorTracker tracker ;
5557 private final URI url ;
5658 private final boolean debug ;
5759
@@ -64,11 +66,12 @@ protected SimpleMetrics(SimpleMetrics.Factory<?> factory, Path config) throws Il
6466 this .config = new Config (config );
6567 this .debug = factory .debug || Boolean .getBoolean ("faststats.debug" ) || this .config .debug ();
6668 this .token = factory .token ;
69+ this .tracker = factory .tracker ;
6770 this .url = factory .url ;
6871 }
6972
7073 @ VisibleForTesting
71- protected SimpleMetrics (Config config , Set <Chart <?>> charts , @ Token String token , URI url , boolean debug ) {
74+ protected SimpleMetrics (Config config , Set <Chart <?>> charts , @ Token String token , @ Nullable ErrorTracker tracker , URI url , boolean debug ) {
7275 if (!token .matches (Token .PATTERN )) {
7376 throw new IllegalArgumentException ("Invalid token '" + token + "', must match '" + Token .PATTERN + "'" );
7477 }
@@ -77,6 +80,7 @@ protected SimpleMetrics(Config config, Set<Chart<?>> charts, @Token String token
7780 this .config = config ;
7881 this .debug = debug ;
7982 this .token = token ;
83+ this .tracker = tracker ;
8084 this .url = url ;
8185 }
8286
@@ -174,6 +178,7 @@ protected CompletableFuture<Boolean> submitAsync() throws IOException {
174178
175179 if (statusCode >= 200 && statusCode < 300 ) {
176180 info ("Metrics submitted with status code: " + statusCode + " (" + body + ")" );
181+ getErrorTracker ().ifPresent (ErrorTracker ::clear );
177182 return true ;
178183 } else if (statusCode >= 300 && statusCode < 400 ) {
179184 warn ("Received redirect response from metrics server: " + statusCode + " (" + body + ")" );
@@ -219,8 +224,10 @@ protected JsonObject createData() {
219224
220225 appendDefaultData (charts );
221226
222- data .addProperty ("server_id " , config .serverId ().toString ());
227+ data .addProperty ("identifier " , config .serverId ().toString ());
223228 data .add ("data" , charts );
229+
230+ getErrorTracker ().flatMap (ErrorTracker ::getData ).ifPresent (errors -> data .add ("errors" , errors ));
224231 return data ;
225232 }
226233
@@ -229,6 +236,11 @@ protected JsonObject createData() {
229236 return token ;
230237 }
231238
239+ @ Override
240+ public Optional <ErrorTracker > getErrorTracker () {
241+ return Optional .ofNullable (tracker );
242+ }
243+
232244 @ Override
233245 public Metrics .Config getConfig () {
234246 return config ;
@@ -270,6 +282,7 @@ public void shutdown() {
270282 public abstract static class Factory <T > implements Metrics .Factory <T > {
271283 private final Set <Chart <?>> charts = new HashSet <>(0 );
272284 private URI url = URI .create ("https://metrics.faststats.dev/v1/collect" );
285+ private @ Nullable ErrorTracker tracker ;
273286 private @ Nullable String token ;
274287 private boolean debug = false ;
275288
@@ -279,6 +292,12 @@ public Metrics.Factory<T> addChart(Chart<?> chart) throws IllegalArgumentExcepti
279292 return this ;
280293 }
281294
295+ @ Override
296+ public Metrics .Factory <T > errorTracker (ErrorTracker tracker ) {
297+ this .tracker = tracker ;
298+ return this ;
299+ }
300+
282301 @ Override
283302 public Metrics .Factory <T > debug (boolean enabled ) {
284303 this .debug = enabled ;
0 commit comments