From f619ecb9bcad1a43fd670f8b6d5dbd88a89b349b Mon Sep 17 00:00:00 2001 From: Drew Edwards Date: Mon, 21 Nov 2022 05:13:40 +0000 Subject: [PATCH 1/5] feat: toggleable CC metrics --- .../src/main/java/cc/tweaked/prometheus/Config.java | 5 +++++ .../java/cc/tweaked/prometheus/ServerMetrics.java | 12 +++++++++--- fabric/src/main/resources/fabric.mod.json | 4 +++- forge/src/main/resources/META-INF/mods.toml | 2 +- gradle/libs.versions.toml | 3 ++- 5 files changed, 20 insertions(+), 6 deletions(-) diff --git a/common/src/main/java/cc/tweaked/prometheus/Config.java b/common/src/main/java/cc/tweaked/prometheus/Config.java index 8c4b02e..cadce88 100644 --- a/common/src/main/java/cc/tweaked/prometheus/Config.java +++ b/common/src/main/java/cc/tweaked/prometheus/Config.java @@ -5,6 +5,7 @@ public final class Config { public static final ForgeConfigSpec.ConfigValue host; public static final ForgeConfigSpec.ConfigValue port; + public static final ForgeConfigSpec.ConfigValue computercraft; public static final ForgeConfigSpec.ConfigValue vanilla; public static final ForgeConfigSpec.ConfigValue jvm; @@ -21,6 +22,10 @@ public final class Config { .comment("The port the Prometheus exporter should be hosted on.") .defineInRange("port", 9226, 1, 65535); + computercraft = configBuilder + .comment("Whether to expose ComputerCraft metrics.") + .define("computercraft", true); + vanilla = configBuilder .comment("Whether to expose some metrics about the state of the vanilla server.") .define("vanilla", false); diff --git a/common/src/main/java/cc/tweaked/prometheus/ServerMetrics.java b/common/src/main/java/cc/tweaked/prometheus/ServerMetrics.java index 6c30db5..6aa78d6 100644 --- a/common/src/main/java/cc/tweaked/prometheus/ServerMetrics.java +++ b/common/src/main/java/cc/tweaked/prometheus/ServerMetrics.java @@ -29,12 +29,18 @@ 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 (Config.computercraft.get()) { + 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()) diff --git a/fabric/src/main/resources/fabric.mod.json b/fabric/src/main/resources/fabric.mod.json index 09c2f70..2e5b03e 100644 --- a/fabric/src/main/resources/fabric.mod.json +++ b/fabric/src/main/resources/fabric.mod.json @@ -22,7 +22,9 @@ "fabricloader": ">=0.14", "fabric": "*", "minecraft": "1.19.x", - "java": ">=17", + "java": ">=17" + }, + "recommends": { "computercraft": "*" } } diff --git a/forge/src/main/resources/META-INF/mods.toml b/forge/src/main/resources/META-INF/mods.toml index b062253..43d3cd1 100644 --- a/forge/src/main/resources/META-INF/mods.toml +++ b/forge/src/main/resources/META-INF/mods.toml @@ -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" diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 2f96db7..287fa27 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -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. @@ -54,7 +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" ] From 799cb28e95fa5ec95dfa1bc9a856fdb3346dd9cf Mon Sep 17 00:00:00 2001 From: Jonathan Coates Date: Mon, 21 Nov 2022 09:18:37 +0000 Subject: [PATCH 2/5] Make sure we bundle forgeconfigapi-port As we no longer depend on CC:T being present, we need to include() it. --- common/build.gradle.kts | 6 +++++- .../java/cc/tweaked/prometheus/Config.java | 3 +++ fabric/build.gradle.kts | 20 ++++++++----------- forge/build.gradle.kts | 2 ++ gradle/libs.versions.toml | 5 +---- 5 files changed, 19 insertions(+), 17 deletions(-) diff --git a/common/build.gradle.kts b/common/build.gradle.kts index ebeeaa1..d76f21f 100644 --- a/common/build.gradle.kts +++ b/common/build.gradle.kts @@ -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. } diff --git a/common/src/main/java/cc/tweaked/prometheus/Config.java b/common/src/main/java/cc/tweaked/prometheus/Config.java index cadce88..b9b2a48 100644 --- a/common/src/main/java/cc/tweaked/prometheus/Config.java +++ b/common/src/main/java/cc/tweaked/prometheus/Config.java @@ -24,14 +24,17 @@ public final class Config { computercraft = configBuilder .comment("Whether to expose ComputerCraft metrics.") + .worldRestart() .define("computercraft", true); 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(); diff --git a/fabric/build.gradle.kts b/fabric/build.gradle.kts index 7f2fc7d..21a8f92 100644 --- a/fabric/build.gradle.kts +++ b/fabric/build.gradle.kts @@ -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 + modCompileOnly(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 { diff --git a/forge/build.gradle.kts b/forge/build.gradle.kts index 0db1ddf..48cb63d 100644 --- a/forge/build.gradle.kts +++ b/forge/build.gradle.kts @@ -44,7 +44,9 @@ dependencies { compileOnly(project(":common")) + // Core libraries minecraftEmbed(libs.bundles.prometheus) + // Extra mods implementation(fg.deobf(libs.cct.forge.get())) } diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 287fa27..598e3e9 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -58,7 +58,4 @@ prometheus = [ "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"] From 8cd97801fab1c16550025ea44f707d93353de12d Mon Sep 17 00:00:00 2001 From: Drew Edwards Date: Mon, 21 Nov 2022 16:52:51 +0000 Subject: [PATCH 3/5] fix: detect presence of CC instead of config --- common/src/main/java/cc/tweaked/prometheus/Config.java | 6 ------ .../main/java/cc/tweaked/prometheus/ServerMetrics.java | 10 +++++++++- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/common/src/main/java/cc/tweaked/prometheus/Config.java b/common/src/main/java/cc/tweaked/prometheus/Config.java index b9b2a48..5307886 100644 --- a/common/src/main/java/cc/tweaked/prometheus/Config.java +++ b/common/src/main/java/cc/tweaked/prometheus/Config.java @@ -5,7 +5,6 @@ public final class Config { public static final ForgeConfigSpec.ConfigValue host; public static final ForgeConfigSpec.ConfigValue port; - public static final ForgeConfigSpec.ConfigValue computercraft; public static final ForgeConfigSpec.ConfigValue vanilla; public static final ForgeConfigSpec.ConfigValue jvm; @@ -22,11 +21,6 @@ public final class Config { .comment("The port the Prometheus exporter should be hosted on.") .defineInRange("port", 9226, 1, 65535); - computercraft = configBuilder - .comment("Whether to expose ComputerCraft metrics.") - .worldRestart() - .define("computercraft", true); - vanilla = configBuilder .comment("Whether to expose some metrics about the state of the vanilla server.") .worldRestart() diff --git a/common/src/main/java/cc/tweaked/prometheus/ServerMetrics.java b/common/src/main/java/cc/tweaked/prometheus/ServerMetrics.java index 6aa78d6..1daf65e 100644 --- a/common/src/main/java/cc/tweaked/prometheus/ServerMetrics.java +++ b/common/src/main/java/cc/tweaked/prometheus/ServerMetrics.java @@ -29,7 +29,15 @@ public static void onServerStart(MinecraftServer server) { var ticking = ServerMetrics.toTick = new ArrayList<>(); var registry = new MetricContext(server, collectorRegistry, ticking::add); - if (Config.computercraft.get()) { + var computercraftLoaded = false; + try { + Class.forName("dan200.computercraft.impl.ComputerCraftAPIImpl"); + computercraftLoaded = true; + } catch (ClassNotFoundException ignored) { + LOG.warn("ComputerCraft not found, not registering ComputerCraft metrics"); + } + + if (computercraftLoaded) { ComputerCollector.register(registry); ComputerFieldCollector.register(registry); ThreadGroupCollector.register(registry); From 0e24d583c702dda712bfd02931edcc8f840158b8 Mon Sep 17 00:00:00 2001 From: Drew Edwards Date: Mon, 21 Nov 2022 17:27:41 +0000 Subject: [PATCH 4/5] Update common/src/main/java/cc/tweaked/prometheus/ServerMetrics.java Co-authored-by: Jonathan Coates --- common/src/main/java/cc/tweaked/prometheus/ServerMetrics.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/src/main/java/cc/tweaked/prometheus/ServerMetrics.java b/common/src/main/java/cc/tweaked/prometheus/ServerMetrics.java index 1daf65e..072b5cd 100644 --- a/common/src/main/java/cc/tweaked/prometheus/ServerMetrics.java +++ b/common/src/main/java/cc/tweaked/prometheus/ServerMetrics.java @@ -31,7 +31,7 @@ public static void onServerStart(MinecraftServer server) { var computercraftLoaded = false; try { - Class.forName("dan200.computercraft.impl.ComputerCraftAPIImpl"); + Class.forName("dan200.computercraft.api.ComputerCraftAPI"); computercraftLoaded = true; } catch (ClassNotFoundException ignored) { LOG.warn("ComputerCraft not found, not registering ComputerCraft metrics"); From 31afe9ee1391b38d4c441d6ad2915382bbd631cf Mon Sep 17 00:00:00 2001 From: Jonathan Coates Date: Mon, 21 Nov 2022 18:13:36 +0000 Subject: [PATCH 5/5] Move class exists check into its own method On the off-chance we'll ever support other mods I guess?? --- .../cc/tweaked/prometheus/ServerMetrics.java | 20 ++++++++++--------- fabric/build.gradle.kts | 2 +- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/common/src/main/java/cc/tweaked/prometheus/ServerMetrics.java b/common/src/main/java/cc/tweaked/prometheus/ServerMetrics.java index 072b5cd..8fa5592 100644 --- a/common/src/main/java/cc/tweaked/prometheus/ServerMetrics.java +++ b/common/src/main/java/cc/tweaked/prometheus/ServerMetrics.java @@ -29,19 +29,12 @@ public static void onServerStart(MinecraftServer server) { var ticking = ServerMetrics.toTick = new ArrayList<>(); var registry = new MetricContext(server, collectorRegistry, ticking::add); - var computercraftLoaded = false; - try { - Class.forName("dan200.computercraft.api.ComputerCraftAPI"); - computercraftLoaded = true; - } catch (ClassNotFoundException ignored) { - LOG.warn("ComputerCraft not found, not registering ComputerCraft metrics"); - } - - if (computercraftLoaded) { + 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); @@ -75,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; + } + } } diff --git a/fabric/build.gradle.kts b/fabric/build.gradle.kts index 21a8f92..39f9052 100644 --- a/fabric/build.gradle.kts +++ b/fabric/build.gradle.kts @@ -28,7 +28,7 @@ dependencies { implementation(libs.bundles.prometheus) modImplementation(libs.bundles.forgeConfig) // Extra mods - modCompileOnly(libs.cct.fabric) + modImplementation(libs.cct.fabric) include(libs.bundles.prometheus) include(libs.bundles.forgeConfig)