Skip to content

Commit 2a77a70

Browse files
authored
Merge pull request #71 from faststats-dev/example/velocity
Add ExamplePlugin with metrics and error tracking
2 parents 0bee3a1 + 7f851a9 commit 2a77a70

File tree

3 files changed

+87
-1
lines changed

3 files changed

+87
-1
lines changed

settings.gradle.kts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ include("minestom")
1313
include("nukkit")
1414
include("sponge")
1515
include("sponge:example-plugin")
16-
include("velocity")
16+
include("velocity")
17+
include("velocity:example-plugin")
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
repositories {
2+
maven("https://repo.papermc.io/repository/maven-public/")
3+
}
4+
5+
dependencies {
6+
compileOnly("com.velocitypowered:velocity-api:3.4.0-SNAPSHOT")
7+
annotationProcessor("com.velocitypowered:velocity-api:3.4.0-SNAPSHOT")
8+
implementation(project(":velocity"))
9+
}
10+
11+
tasks.shadowJar {
12+
// optionally relocate faststats
13+
relocate("dev.faststats", "com.example.utils.faststats")
14+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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

Comments
 (0)