auto restart prometheus metrics server (#669)
This commit is contained in:
@@ -32,7 +32,9 @@ import org.slf4j.LoggerFactory
|
|||||||
import org.springframework.beans.factory.annotation.Autowired
|
import org.springframework.beans.factory.annotation.Autowired
|
||||||
import org.springframework.stereotype.Service
|
import org.springframework.stereotype.Service
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
|
import java.net.InetAddress
|
||||||
import java.net.InetSocketAddress
|
import java.net.InetSocketAddress
|
||||||
|
import java.net.ServerSocket
|
||||||
import java.util.zip.GZIPOutputStream
|
import java.util.zip.GZIPOutputStream
|
||||||
import javax.annotation.PostConstruct
|
import javax.annotation.PostConstruct
|
||||||
|
|
||||||
@@ -45,6 +47,20 @@ class MonitoringSetup(
|
|||||||
private val log = LoggerFactory.getLogger(MonitoringSetup::class.java)
|
private val log = LoggerFactory.getLogger(MonitoringSetup::class.java)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun isTcpPortAvailable(host: String, port: Int): Boolean {
|
||||||
|
try {
|
||||||
|
ServerSocket().use { serverSocket ->
|
||||||
|
// setReuseAddress(false) is required only on macOS,
|
||||||
|
// otherwise the code will not work correctly on that platform
|
||||||
|
serverSocket.reuseAddress = false
|
||||||
|
serverSocket.bind(InetSocketAddress(InetAddress.getByName(host), port), 1)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
} catch (ex: java.lang.Exception) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@PostConstruct
|
@PostConstruct
|
||||||
fun setup() {
|
fun setup() {
|
||||||
val prometheusRegistry = PrometheusMeterRegistry(PrometheusConfig.DEFAULT)
|
val prometheusRegistry = PrometheusMeterRegistry(PrometheusConfig.DEFAULT)
|
||||||
@@ -75,6 +91,10 @@ class MonitoringSetup(
|
|||||||
if (monitoringConfig.prometheus.enabled) {
|
if (monitoringConfig.prometheus.enabled) {
|
||||||
// use standard JVM server with a single thread blocking processing
|
// use standard JVM server with a single thread blocking processing
|
||||||
// prometheus is a single thread periodic call, no reason to setup anything complex
|
// prometheus is a single thread periodic call, no reason to setup anything complex
|
||||||
|
var started = false
|
||||||
|
while (true) {
|
||||||
|
if (isTcpPortAvailable(monitoringConfig.prometheus.host, monitoringConfig.prometheus.port)) {
|
||||||
|
started = true
|
||||||
try {
|
try {
|
||||||
log.info("Run Prometheus metrics on ${monitoringConfig.prometheus.host}:${monitoringConfig.prometheus.port}${monitoringConfig.prometheus.path}")
|
log.info("Run Prometheus metrics on ${monitoringConfig.prometheus.host}:${monitoringConfig.prometheus.port}${monitoringConfig.prometheus.path}")
|
||||||
val server = HttpServer.create(
|
val server = HttpServer.create(
|
||||||
@@ -99,6 +119,13 @@ class MonitoringSetup(
|
|||||||
} catch (e: IOException) {
|
} catch (e: IOException) {
|
||||||
log.error("Failed to start Prometheus Server", e)
|
log.error("Failed to start Prometheus Server", e)
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
if (!started) {
|
||||||
|
log.error("Can't start prometheus metrics on ${monitoringConfig.prometheus.host}:${monitoringConfig.prometheus.port}${monitoringConfig.prometheus.path}")
|
||||||
|
}
|
||||||
|
Thread.sleep(1000)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user