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: implementation of support for host;port configuration from env and jar startup properties #292

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
29 changes: 28 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 15 additions & 2 deletions src/main/kotlin/de/sldk/mc/PrometheusExporter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down