The context-aware error tracker automatically tracks uncaught errors from the
same class loader:
import dev.faststats.ErrorTracker;
import dev.faststats.bukkit.BukkitContext;
import org.bukkit.plugin.java.JavaPlugin;
public final class MyBukkitPlugin extends JavaPlugin {
public static final ErrorTracker ERROR_TRACKER = ErrorTracker.contextAware();
private final BukkitContext context = new BukkitContext.Factory(this, "YOUR_TOKEN")
.errorTrackerService(ERROR_TRACKER)
.create();
@Override
public void onEnable() {
context.ready();
}
@Override
public void onDisable() {
context.shutdown();
}
public void doSomethingWrong() {
try {
throw new RuntimeException("Something went wrong!");
} catch (Exception e) {
// This exception will be reported as handled.
ERROR_TRACKER.trackError(e);
}
}
}
The automatic tracking only covers errors that would otherwise be swallowed by
the JVM. Use manual tracking in addition to passive tracking.
Exclusion rules let you decide in code which automatically tracked errors should
not be reported. A rule receives the thread the error occurred on and the error
itself. If any rule returns true, the error is not tracked.
public static final ErrorTracker ERROR_TRACKER = ErrorTracker.contextAware()
// Exclude all errors without a stack frame from "your.package.name"
.addExclusionRule((thread, error) -> {
for (final var element : error.getStackTrace()) {
if (element.getClassName().startsWith("your.package.name")) {
return false; // track this error
}
}
return true; // do not track this error
});
Exclusion rules only apply to errors captured automatically through an
attached error context, after the error has been attributed to the tracker’s
class loader. Errors passed to trackError() are not affected.
Attributes stored on an error tracker are attached to reports submitted through
that tracker. Use them for stable context such as environment or component.
All enabled metrics are already added to the error context by default. This
includes the standard runtime and platform details FastStats collects, such as
Java version and plugin version. Add tracker attributes only for extra context
that FastStats cannot infer automatically.
public static final ErrorTracker ERROR_TRACKER = ErrorTracker.contextAware();
public static final ErrorTracker MANUAL_ERROR_TRACKER = ErrorTracker.contextUnaware();
{
ERROR_TRACKER.getAttributes()
.put("environment", "production")
.put("component", "global-error-handler");
MANUAL_ERROR_TRACKER.getAttributes()
.put("component", "manual-error-handler");
}
Attributes on the global/internal tracker are used for every error report,
including reports submitted through additional registered trackers. Additional
trackers can override global attributes with the same key.
trackError(...) returns a tracked error, so you can add context that only
applies to that specific report. Per-error attributes are useful for operation
names, severity, retry state, player or request context, and other values that
change per failure. They override both global and per-tracker attributes with
the same key.
Use .handled(false) when the error was not recovered from properly and should
be treated as unhandled. Errors tracked manually are handled by default unless
you override that option.