Skip to content

Commit 42849e8

Browse files
committed
Add config options for error tracking and additional metrics submission
1 parent 5c0d721 commit 42849e8

File tree

3 files changed

+63
-12
lines changed

3 files changed

+63
-12
lines changed

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

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ public interface Metrics {
6161
interface Factory<T> {
6262
/**
6363
* Adds a chart to the metrics submission.
64+
* <p>
65+
* If {@link Config#additionalMetrics()} is disabled, the chart will not be submitted.
6466
*
6567
* @param chart the chart to add
6668
* @return the metrics factory
@@ -72,6 +74,8 @@ interface Factory<T> {
7274

7375
/**
7476
* Sets the error tracker for this metrics instance.
77+
* <p>
78+
* If {@link Config#errorTracking()} is disabled, no errors will be submitted.
7579
*
7680
* @param tracker the error tracker
7781
* @return the metrics factory
@@ -158,16 +162,34 @@ interface Config {
158162
* <b>Bypassing this setting may get your project banned from FastStats.</b><br>
159163
* <b>Users have to be able to opt out from metrics submission.</b>
160164
*
161-
* @return true if metrics submission is enabled, false otherwise
165+
* @return {@code true} if metrics submission is enabled, {@code false} otherwise
162166
* @since 0.1.0
163167
*/
164168
@Contract(pure = true)
165169
boolean enabled();
166170

171+
/**
172+
* Whether error tracking is enabled across all metrics instances.
173+
*
174+
* @return {@code true} if error tracking is enabled, {@code false} otherwise
175+
* @since 0.11.0
176+
*/
177+
@Contract(pure = true)
178+
boolean errorTracking();
179+
180+
/**
181+
* Whether additional metrics are enabled across all metrics instances.
182+
*
183+
* @return {@code true} if additional metrics are enabled, {@code false} otherwise
184+
* @since 0.11.0
185+
*/
186+
@Contract(pure = true)
187+
boolean additionalMetrics();
188+
167189
/**
168190
* Whether debug logging is enabled across all metrics instances.
169191
*
170-
* @return true if debug logging is enabled, false otherwise
192+
* @return {@code true} if debug logging is enabled, {@code false} otherwise
171193
* @since 0.1.0
172194
*/
173195
@Contract(pure = true)

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

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.util.concurrent.ScheduledExecutorService;
3131
import java.util.concurrent.TimeUnit;
3232
import java.util.concurrent.atomic.AtomicBoolean;
33+
import java.util.function.BiPredicate;
3334
import java.util.zip.GZIPOutputStream;
3435

3536
import 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 = """

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

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

2222
@Override

0 commit comments

Comments
 (0)