From 5648e69005f2d9c97f3786d845f18591dbe3297f Mon Sep 17 00:00:00 2001 From: BhHaipls Date: Tue, 25 Feb 2025 13:27:34 +0200 Subject: [PATCH] feat: implementation of support for host;port configuration from env and jar startup properties --- README.md | 29 ++++++++++++++++++- .../kotlin/de/sldk/mc/PrometheusExporter.kt | 17 +++++++++-- 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index fd617e14..a7ab6d2d 100644 --- a/README.md +++ b/README.md @@ -40,14 +40,41 @@ See [Health Checks](#health-checks) for more information on how to build your ow ## Installation & Configuration +#### Configuration Hierarchy + +The Prometheus metrics exporter plugin supports multiple configuration sources for the HTTP endpoint (host/port). The configuration is applied in the following order of priority: + +1. **Environment Variables:** + - `MINECRAFT_PROMETHEUS_EXPORTER_HOST` + - `MINECRAFT_PROMETHEUS_EXPORTER_PORT` + If these environment variables are set, their values will override all other settings. + +2. **System Properties:** + - `minecraft.prometheus.exporter.host` + - `minecraft.prometheus.exporter.port` + If the environment variables are not provided, the plugin checks these system properties. They can be set when launching the JVM. + +3. **Configuration File:** + If neither environment variables nor system properties are defined, the plugin falls back to the values in the configuration file. + + ### Plugin config The default configuration file will be created after the first use of the plugin. ```yml # Note that the HTTP server binds to localhost by default. -# If your Prometheus runs on another host or inside a Kubernetes cluster +# If your Prometheus runs on another host or inside a Kubernetes cluster, # set this to any reachable IP or 0.0.0.0 to listen on all interfaces. +# +# These settings can be overridden by environment variables or system properties. +# Environment variables: +# - MINECRAFT_PROMETHEUS_EXPORTER_HOST: Overrides the host setting (e.g., 0.0.0.0) +# - MINECRAFT_PROMETHEUS_EXPORTER_PORT: Overrides the port setting (e.g., 9001) +# +# System properties (set during JVM startup): +# -Dminecraft.prometheus.exporter.host=0.0.0.0 +# -Dminecraft.prometheus.exporter.port=9001 host: localhost # The port can be changed in case it conflicts with any other application. port: 9940 diff --git a/src/main/kotlin/de/sldk/mc/PrometheusExporter.kt b/src/main/kotlin/de/sldk/mc/PrometheusExporter.kt index b1cd0d4c..960c898e 100644 --- a/src/main/kotlin/de/sldk/mc/PrometheusExporter.kt +++ b/src/main/kotlin/de/sldk/mc/PrometheusExporter.kt @@ -25,9 +25,22 @@ class PrometheusExporter : JavaPlugin() { startMetricsServer(healthChecks) } + private fun getConfigValue(envKey: String, propertyKey: String, defaultValue: String): String { + return System.getenv(envKey) ?: System.getProperty(propertyKey) ?: defaultValue + } + private fun startMetricsServer(healthChecks: HealthChecks) { - val host = config[PrometheusExporterConfig.HOST] - val port = config[PrometheusExporterConfig.PORT] + val host = getConfigValue( + envKey = "MINECRAFT_PROMETHEUS_EXPORTER_HOST", + propertyKey = "minecraft.prometheus.exporter.host", + defaultValue = config[PrometheusExporterConfig.HOST].toString() + ) + + val port = getConfigValue( + envKey = "MINECRAFT_PROMETHEUS_EXPORTER_PORT", + propertyKey = "minecraft.prometheus.exporter.port", + defaultValue = config[PrometheusExporterConfig.PORT].toString() + ).toInt() metricsServer = MetricsServer(host, port, this, healthChecks)