|
| 1 | +package com.example; |
| 2 | + |
| 3 | +import com.google.inject.Inject; |
| 4 | +import com.velocitypowered.api.event.Subscribe; |
| 5 | +import com.velocitypowered.api.event.proxy.ProxyInitializeEvent; |
| 6 | +import com.velocitypowered.api.event.proxy.ProxyShutdownEvent; |
| 7 | +import com.velocitypowered.api.plugin.Plugin; |
| 8 | +import dev.faststats.core.ErrorTracker; |
| 9 | +import dev.faststats.core.Metrics; |
| 10 | +import dev.faststats.core.chart.Chart; |
| 11 | +import dev.faststats.velocity.VelocityMetrics; |
| 12 | +import org.jspecify.annotations.Nullable; |
| 13 | + |
| 14 | +import java.net.URI; |
| 15 | + |
| 16 | + |
| 17 | +@Plugin(id = "example", name = "Example Plugin", version = "1.0.0", |
| 18 | + url = "https://example.com", authors = {"Your Name"}) |
| 19 | +public class ExamplePlugin { |
| 20 | + // context-aware error tracker, automatically tracks errors in the same class loader |
| 21 | + public static final ErrorTracker ERROR_TRACKER = ErrorTracker.contextAware(); |
| 22 | + |
| 23 | + // context-unaware error tracker, does not automatically track errors |
| 24 | + public static final ErrorTracker CONTEXT_UNAWARE_ERROR_TRACKER = ErrorTracker.contextUnaware(); |
| 25 | + |
| 26 | + private final VelocityMetrics.Factory metricsFactory; |
| 27 | + private @Nullable Metrics metrics = null; |
| 28 | + |
| 29 | + @Inject |
| 30 | + public ExamplePlugin(VelocityMetrics.Factory factory) { |
| 31 | + this.metricsFactory = factory; |
| 32 | + } |
| 33 | + |
| 34 | + @Subscribe |
| 35 | + public void onProxyInitialize(ProxyInitializeEvent event) { |
| 36 | + this.metrics = metricsFactory |
| 37 | + .url(URI.create("https://metrics.example.com/v1/collect")) // For self-hosted metrics servers only |
| 38 | + |
| 39 | + // Custom example charts |
| 40 | + // For this to work you have to create a corresponding data source in your project settings first |
| 41 | + .addChart(Chart.number("example_chart", () -> 42)) |
| 42 | + .addChart(Chart.string("example_string", () -> "Hello, World!")) |
| 43 | + .addChart(Chart.bool("example_boolean", () -> true)) |
| 44 | + .addChart(Chart.stringArray("example_string_array", () -> new String[]{"Option 1", "Option 2"})) |
| 45 | + .addChart(Chart.numberArray("example_number_array", () -> new Number[]{1, 2, 3})) |
| 46 | + .addChart(Chart.booleanArray("example_boolean_array", () -> new Boolean[]{true, false})) |
| 47 | + |
| 48 | + // Attach an error tracker |
| 49 | + // This must be enabled in the project settings |
| 50 | + .errorTracker(ERROR_TRACKER) |
| 51 | + |
| 52 | + .debug(true) // Enable debug mode for development and testing |
| 53 | + |
| 54 | + .token("YOUR_TOKEN_HERE") // required -> token can be found in the settings of your project |
| 55 | + .create(this); |
| 56 | + } |
| 57 | + |
| 58 | + @Subscribe |
| 59 | + public void onProxyStop(ProxyShutdownEvent event) { |
| 60 | + if (metrics != null) metrics.shutdown(); |
| 61 | + } |
| 62 | + |
| 63 | + public void doSomethingWrong() { |
| 64 | + try { |
| 65 | + // Do something that might throw an error |
| 66 | + throw new RuntimeException("Something went wrong!"); |
| 67 | + } catch (Exception e) { |
| 68 | + CONTEXT_UNAWARE_ERROR_TRACKER.trackError(e); |
| 69 | + } |
| 70 | + } |
| 71 | +} |
0 commit comments