Skip to content

Commit 667b725

Browse files
authored
Merge pull request #292 from BhHaipls/feat-support-host-and-port-from-env-and-startup-properties
feat: implementation of support for host;port configuration from env and jar startup properties
2 parents 7ad2336 + 5648e69 commit 667b725

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

README.md

+28-1
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,41 @@ See [Health Checks](#health-checks) for more information on how to build your ow
4040

4141
## Installation & Configuration
4242

43+
#### Configuration Hierarchy
44+
45+
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:
46+
47+
1. **Environment Variables:**
48+
- `MINECRAFT_PROMETHEUS_EXPORTER_HOST`
49+
- `MINECRAFT_PROMETHEUS_EXPORTER_PORT`
50+
If these environment variables are set, their values will override all other settings.
51+
52+
2. **System Properties:**
53+
- `minecraft.prometheus.exporter.host`
54+
- `minecraft.prometheus.exporter.port`
55+
If the environment variables are not provided, the plugin checks these system properties. They can be set when launching the JVM.
56+
57+
3. **Configuration File:**
58+
If neither environment variables nor system properties are defined, the plugin falls back to the values in the configuration file.
59+
60+
4361
### Plugin config
4462

4563
The default configuration file will be created after the first use of the plugin.
4664

4765
```yml
4866
# Note that the HTTP server binds to localhost by default.
49-
# If your Prometheus runs on another host or inside a Kubernetes cluster
67+
# If your Prometheus runs on another host or inside a Kubernetes cluster,
5068
# set this to any reachable IP or 0.0.0.0 to listen on all interfaces.
69+
#
70+
# These settings can be overridden by environment variables or system properties.
71+
# Environment variables:
72+
# - MINECRAFT_PROMETHEUS_EXPORTER_HOST: Overrides the host setting (e.g., 0.0.0.0)
73+
# - MINECRAFT_PROMETHEUS_EXPORTER_PORT: Overrides the port setting (e.g., 9001)
74+
#
75+
# System properties (set during JVM startup):
76+
# -Dminecraft.prometheus.exporter.host=0.0.0.0
77+
# -Dminecraft.prometheus.exporter.port=9001
5178
host: localhost
5279
# The port can be changed in case it conflicts with any other application.
5380
port: 9940

src/main/kotlin/de/sldk/mc/PrometheusExporter.kt

+15-2
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,22 @@ class PrometheusExporter : JavaPlugin() {
2525
startMetricsServer(healthChecks)
2626
}
2727

28+
private fun getConfigValue(envKey: String, propertyKey: String, defaultValue: String): String {
29+
return System.getenv(envKey) ?: System.getProperty(propertyKey) ?: defaultValue
30+
}
31+
2832
private fun startMetricsServer(healthChecks: HealthChecks) {
29-
val host = config[PrometheusExporterConfig.HOST]
30-
val port = config[PrometheusExporterConfig.PORT]
33+
val host = getConfigValue(
34+
envKey = "MINECRAFT_PROMETHEUS_EXPORTER_HOST",
35+
propertyKey = "minecraft.prometheus.exporter.host",
36+
defaultValue = config[PrometheusExporterConfig.HOST].toString()
37+
)
38+
39+
val port = getConfigValue(
40+
envKey = "MINECRAFT_PROMETHEUS_EXPORTER_PORT",
41+
propertyKey = "minecraft.prometheus.exporter.port",
42+
defaultValue = config[PrometheusExporterConfig.PORT].toString()
43+
).toInt()
3144

3245
metricsServer = MetricsServer(host, port, this, healthChecks)
3346

0 commit comments

Comments
 (0)