1717
1818package dev.cubxity.plugins.metrics.prometheus.exporter
1919
20+ import com.sun.net.httpserver.BasicAuthenticator
2021import dev.cubxity.plugins.metrics.api.UnifiedMetrics
2122import dev.cubxity.plugins.metrics.prometheus.PrometheusMetricsDriver
2223import dev.cubxity.plugins.metrics.prometheus.collector.UnifiedMetricsCollector
24+ import dev.cubxity.plugins.metrics.prometheus.config.AuthenticationScheme
2325import io.prometheus.client.CollectorRegistry
2426import io.prometheus.client.exporter.HTTPServer
25- import java.net.InetSocketAddress
2627
27- class PrometheusHTTPExporter (private val api : UnifiedMetrics , private val driver : PrometheusMetricsDriver ) : PrometheusExporter {
28+ class PrometheusHTTPExporter (
29+ private val api : UnifiedMetrics ,
30+ private val driver : PrometheusMetricsDriver
31+ ) : PrometheusExporter {
2832 private var server: HTTPServer ? = null
2933
3034 override fun initialize () {
3135 val registry = CollectorRegistry ()
3236 registry.register(UnifiedMetricsCollector (api))
3337
34- server = HTTPServer (InetSocketAddress (driver.config.http.host, driver.config.http.port), registry)
38+ server = HTTPServer .Builder ()
39+ .withHostname(driver.config.http.host)
40+ .withPort(driver.config.http.port)
41+ .withRegistry(registry)
42+ .apply {
43+ with (driver.config.http.authentication) {
44+ if (scheme == AuthenticationScheme .Basic ) {
45+ withAuthenticator(Authenticator (username, password))
46+ }
47+ }
48+ }
49+ .build()
3550 }
3651
3752 override fun close () {
3853 server?.close()
3954 server = null
4055 }
56+
57+ private class Authenticator (
58+ private val username : String ,
59+ private val password : String
60+ ) : BasicAuthenticator(" unifiedmetrics" ) {
61+ override fun checkCredentials (username : String? , password : String? ): Boolean =
62+ this .username == username && this .password == password
63+ }
4164}
0 commit comments