3030import java .util .concurrent .ScheduledExecutorService ;
3131import java .util .concurrent .TimeUnit ;
3232import java .util .concurrent .atomic .AtomicBoolean ;
33+ import java .util .function .BiPredicate ;
3334import java .util .zip .GZIPOutputStream ;
3435
3536import static java .nio .charset .StandardCharsets .UTF_8 ;
@@ -62,11 +63,11 @@ public abstract class SimpleMetrics implements Metrics {
6263 protected SimpleMetrics (Factory <?> factory , Path config ) throws IllegalStateException {
6364 if (factory .token == null ) throw new IllegalStateException ("Token must be specified" );
6465
65- this .charts = Set .copyOf (factory .charts );
6666 this .config = new Config (config );
67+ this .charts = this .config .additionalMetrics ? Set .copyOf (factory .charts ) : Set .of ();
6768 this .debug = factory .debug || Boolean .getBoolean ("faststats.debug" ) || this .config .debug ();
6869 this .token = factory .token ;
69- this .tracker = factory .tracker ;
70+ this .tracker = this . config . errorTracking ? factory .tracker : null ;
7071 this .url = factory .url ;
7172
7273 Runtime .getRuntime ().addShutdownHook (new Thread (() -> {
@@ -85,7 +86,7 @@ protected SimpleMetrics(Config config, Set<Chart<?>> charts, @Token String token
8586 throw new IllegalArgumentException ("Invalid token '" + token + "', must match '" + Token .PATTERN + "'" );
8687 }
8788
88- this .charts = Set .copyOf (charts );
89+ this .charts = config . additionalMetrics ? Set .copyOf (charts ) : Set . of ( );
8990 this .config = config ;
9091 this .debug = debug ;
9192 this .token = token ;
@@ -339,8 +340,10 @@ public Metrics.Factory<T> url(URI url) {
339340
340341 protected static final class Config implements Metrics .Config {
341342 private final UUID serverId ;
343+ private final boolean additionalMetrics ;
342344 private final boolean debug ;
343345 private final boolean enabled ;
346+ private final boolean errorTracking ;
344347 private final boolean firstRun ;
345348
346349 @ Contract (mutates = "io" )
@@ -359,23 +362,37 @@ protected Config(Path file) {
359362 saveConfig .set (true );
360363 return UUID .randomUUID ();
361364 }
362- }).orElseGet (UUID ::randomUUID );
365+ }).orElseGet (() -> {
366+ saveConfig .set (true );
367+ return UUID .randomUUID ();
368+ });
369+
370+ BiPredicate <String , Boolean > predicate = (key , defaultValue ) -> {
371+ return properties .map (object -> object .getProperty (key )).map (Boolean ::parseBoolean ).orElseGet (() -> {
372+ saveConfig .set (true );
373+ return defaultValue ;
374+ });
375+ };
363376
364- this .enabled = properties .map (object -> object .getProperty ("enabled" )).map (Boolean ::parseBoolean ).orElse (true );
365- this .debug = properties .map (object -> object .getProperty ("debug" )).map (Boolean ::parseBoolean ).orElse (false );
377+ this .enabled = predicate .test ("enabled" , true );
378+ this .errorTracking = predicate .test ("submitErrors" , true );
379+ this .additionalMetrics = predicate .test ("submitAdditionalMetrics" , true );
380+ this .debug = predicate .test ("debug" , false );
366381
367382 if (saveConfig .get ()) try {
368- save (file , serverId , enabled , debug );
383+ save (file , serverId , enabled , errorTracking , additionalMetrics , debug );
369384 } catch (IOException e ) {
370385 throw new RuntimeException ("Failed to save metrics config" , e );
371386 }
372387 }
373388
374389 @ VisibleForTesting
375- public Config (UUID serverId , boolean enabled , boolean debug ) {
390+ public Config (UUID serverId , boolean enabled , boolean errorTracking , boolean additionalMetrics , boolean debug ) {
376391 this .serverId = serverId ;
377392 this .enabled = enabled ;
378393 this .debug = debug ;
394+ this .errorTracking = errorTracking ;
395+ this .additionalMetrics = additionalMetrics ;
379396 this .firstRun = false ;
380397 }
381398
@@ -389,6 +406,16 @@ public boolean enabled() {
389406 return enabled ;
390407 }
391408
409+ @ Override
410+ public boolean errorTracking () {
411+ return errorTracking ;
412+ }
413+
414+ @ Override
415+ public boolean additionalMetrics () {
416+ return additionalMetrics ;
417+ }
418+
392419 @ Override
393420 public boolean debug () {
394421 return debug ;
@@ -405,14 +432,16 @@ private static Optional<Properties> readOrEmpty(Path file) {
405432 }
406433 }
407434
408- private static void save (Path file , UUID serverId , boolean enabled , boolean debug ) throws IOException {
435+ private static void save (Path file , UUID serverId , boolean enabled , boolean errorTracking , boolean additionalMetrics , boolean debug ) throws IOException {
409436 Files .createDirectories (file .getParent ());
410437 try (var out = Files .newOutputStream (file );
411438 var writer = new OutputStreamWriter (out , UTF_8 )) {
412439 var properties = new Properties ();
413440
414441 properties .setProperty ("serverId" , serverId .toString ());
415442 properties .setProperty ("enabled" , Boolean .toString (enabled ));
443+ properties .setProperty ("submitErrors" , Boolean .toString (errorTracking ));
444+ properties .setProperty ("submitAdditionalMetrics" , Boolean .toString (additionalMetrics ));
416445 properties .setProperty ("debug" , Boolean .toString (debug ));
417446
418447 var comment = """
0 commit comments