Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: toggleable CC metrics #4

Merged
merged 5 commits into from
Nov 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ minecraft {

dependencies {
compileOnlyApi(libs.jsr305)

// Core libraries
implementation(libs.bundles.prometheus)
compileOnly(libs.bundles.common)
compileOnly(libs.bundles.forgeConfig)
// Extra mods
compileOnly(libs.cct.forge) // We don't ship a common mod jar.
}
2 changes: 2 additions & 0 deletions common/src/main/java/cc/tweaked/prometheus/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ public final class Config {

vanilla = configBuilder
.comment("Whether to expose some metrics about the state of the vanilla server.")
.worldRestart()
.define("vanilla", false);

jvm = configBuilder
.comment("Whether to expose some metrics about the state of the Java runtime.")
.worldRestart()
.define("jvm", false);

spec = configBuilder.build();
Expand Down
22 changes: 19 additions & 3 deletions common/src/main/java/cc/tweaked/prometheus/ServerMetrics.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,19 @@ public static void onServerStart(MinecraftServer server) {
var ticking = ServerMetrics.toTick = new ArrayList<>();
var registry = new MetricContext(server, collectorRegistry, ticking::add);

ComputerCollector.register(registry);
ComputerFieldCollector.register(registry);
ThreadGroupCollector.register(registry);
if (classExists("dan200.computercraft.api.ComputerCraftAPI")) {
ComputerCollector.register(registry);
ComputerFieldCollector.register(registry);
ThreadGroupCollector.register(registry);
}

if (Config.vanilla.get()) VanillaCollector.export(registry);
if (Config.jvm.get()) DefaultExports.register(collectorRegistry);

if (!collectorRegistry.metricFamilySamples().hasMoreElements()) {
LOG.warn("Warning: no collectors are enabled! Check the configuration.");
}

try {
ServerMetrics.server = new HTTPServer.Builder()
.withHostname(Config.host.get())
Expand All @@ -61,4 +68,13 @@ public static void onServerStop() {
public static void onServerTick() {
for (var action : toTick) action.run();
}

private static boolean classExists(String name) {
try {
Class.forName(name, false, ServerMetrics.class.getClassLoader());
return true;
} catch (ClassNotFoundException ignored) {
return false;
}
}
}
20 changes: 8 additions & 12 deletions fabric/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,18 @@ dependencies {
},
)

modImplementation(libs.bundles.fabric)
modImplementation(libs.fabric.loader)
modImplementation(libs.fabric.api)
// Core libraries
implementation(libs.bundles.prometheus)
modImplementation(libs.bundles.forgeConfig)
// Extra mods
modImplementation(libs.cct.fabric)

include(libs.bundles.prometheus)
include(libs.bundles.forgeConfig)

implementation(project(":common"))

// implementation group: 'com.google.code.findbugs', name: 'jsr305', version: '3.0.1'
//
// implementation project(":common")
//
// modImplementation ("cc.tweaked:cc-tweaked-${minecraft_version}-fabric:${cct_version}")
//
// // IDK how Fabric config works (so much seems to be client-only??), so just copy CC:R.
// implementation 'com.electronwill.night-config:toml:3.6.5'
// include 'com.electronwill.night-config:core:3.6.5'
// include 'com.electronwill.night-config:toml:3.6.5'
}

loom {
Expand Down
4 changes: 3 additions & 1 deletion fabric/src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
"fabricloader": ">=0.14",
"fabric": "*",
"minecraft": "1.19.x",
"java": ">=17",
"java": ">=17"
},
"recommends": {
"computercraft": "*"
}
}
2 changes: 2 additions & 0 deletions forge/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ dependencies {

compileOnly(project(":common"))

// Core libraries
minecraftEmbed(libs.bundles.prometheus)
// Extra mods
implementation(fg.deobf(libs.cct.forge.get()))
}

Expand Down
2 changes: 1 addition & 1 deletion forge/src/main/resources/META-INF/mods.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Hosts a Prometheus server with various ComputerCraft related statistics

[[dependencies.ccprometheus]]
modId="computercraft"
mandatory=true
mandatory=false
versionRange="[1.102.0,)"
ordering="NONE"
side="BOTH"
8 changes: 3 additions & 5 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ nightConfig-core = { module = "com.electronwill.night-config:core", version.ref
nightConfig-toml = { module = "com.electronwill.night-config:toml", version.ref = "nightConfig" }

prometheus-core = { module = "io.prometheus:simpleclient", version.ref = "prometheus" }
prometheus-common = { module = "io.prometheus:simpleclient_common", version.ref = "prometheus" }
prometheus-server = { module = "io.prometheus:simpleclient_httpserver", version.ref = "prometheus" }
prometheus-hotspot = { module = "io.prometheus:simpleclient_hotspot", version.ref = "prometheus" }
# All the transitive deps of prometheus.
Expand All @@ -54,10 +55,7 @@ shadow = { id = "com.github.johnrengelman.shadow", version.ref = "shadow" }

[bundles]
prometheus = [
"prometheus-core", "prometheus-server", "prometheus-hotspot",
"prometheus-core", "prometheus-common", "prometheus-server", "prometheus-hotspot",
"prometheus-tracerCommon", "prometheus-tracerOtel", "prometheus-tracerOtelAgent"
]

common = ["cct-forge", "forgeConfig", "nightConfig-core", "nightConfig-toml"]
fabric = ["fabric-loader", "fabric-api", "cct-fabric", "forgeConfig", "nightConfig-core", "nightConfig-toml"]
forge = ["cct-forge"]
forgeConfig = ["forgeConfig", "nightConfig-core", "nightConfig-toml"]