solution: monitoring config

This commit is contained in:
Igor Artamonov
2021-02-03 23:08:44 -05:00
parent 93e08f58f6
commit 5ed22b368b
6 changed files with 124 additions and 2 deletions

View File

@@ -33,6 +33,7 @@ class MainConfigReader(
private val upstreamsConfigReader = UpstreamsConfigReader(fileResolver)
private val cacheConfigReader = CacheConfigReader()
private val tokensConfigReader = TokensConfigReader()
private val monitoringConfigReader = MonitoringConfigReader()
fun read(input: InputStream): MainConfig? {
val configNode = readNode(input)
@@ -63,6 +64,9 @@ class MainConfigReader(
tokensConfigReader.read(input)?.let {
config.tokens = it
}
monitoringConfigReader.read(input).let {
config.monitoring = it
}
return config
}

View File

@@ -24,17 +24,24 @@ class MonitoringConfig(
fun default(): MonitoringConfig {
return MonitoringConfig(true, PrometheusConfig.default())
}
fun disabled(): MonitoringConfig {
return MonitoringConfig(false, PrometheusConfig.disabled())
}
}
data class PrometheusConfig(
val enabled: Boolean,
val path: String,
val host: String,
val port: Int
) {
companion object {
fun default(): PrometheusConfig {
return PrometheusConfig(true, "/prometheus", 8081)
return PrometheusConfig(true, "/metrics", "127.0.0.1", 8081)
}
fun disabled(): PrometheusConfig {
return PrometheusConfig(false, "/", "127.0.0.1", 0)
}
}
}

View File

@@ -0,0 +1,64 @@
/**
* Copyright (c) 2021 EmeraldPay, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.emeraldpay.dshackle.config
import org.slf4j.LoggerFactory
import org.yaml.snakeyaml.nodes.MappingNode
import java.io.InputStream
class MonitoringConfigReader: YamlConfigReader(), ConfigReader<MonitoringConfig> {
companion object {
private val log = LoggerFactory.getLogger(MonitoringConfigReader::class.java)
}
fun read(input: InputStream): MonitoringConfig {
val configNode = readNode(input)
return read(configNode)
}
override fun read(input: MappingNode?): MonitoringConfig {
return readInternal(getMapping(input, "monitoring"))
}
fun readInternal(input: MappingNode?): MonitoringConfig {
if (input == null) {
return MonitoringConfig.default()
}
val enabled = getValueAsBool(input, "enabled") ?: false
if (!enabled) {
return MonitoringConfig.disabled()
}
val prometheus = readPrometheus(getMapping(input, "prometheus"))
return MonitoringConfig(enabled, prometheus)
}
private fun readPrometheus(input: MappingNode?): MonitoringConfig.PrometheusConfig {
if (input == null) {
return MonitoringConfig.PrometheusConfig.default()
}
val enabled = getValueAsBool(input, "enabled") ?: true
if (!enabled) {
return MonitoringConfig.PrometheusConfig.disabled()
}
val default = MonitoringConfig.PrometheusConfig.default()
val path = getValueAsString(input, "path") ?: default.path
val host = getValueAsString(input, "bind") ?: getValueAsString(input, "host") ?: default.host
val port = getValueAsInt(input, "port") ?: default.port
return MonitoringConfig.PrometheusConfig(enabled, path, host, port)
}
}

View File

@@ -57,7 +57,7 @@ class MonitoringSetup(
// use standard JVM server with a single thread blocking processing
// prometheus is a single thread periodic call, no reason to setup anything complex
try {
val server = HttpServer.create(InetSocketAddress(monitoringConfig.prometheus.port), 0);
val server = HttpServer.create(InetSocketAddress(monitoringConfig.prometheus.host, monitoringConfig.prometheus.port), 0);
server.createContext(monitoringConfig.prometheus.path) { httpExchange ->
val response = prometheusRegistry.scrape()
httpExchange.sendResponseHeaders(200, response.toByteArray().size.toLong());

View File

@@ -0,0 +1,40 @@
/**
* Copyright (c) 2021 EmeraldPay, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.emeraldpay.dshackle.config
import spock.lang.Specification
class MonitoringConfigReaderSpec extends Specification {
MonitoringConfigReader reader = new MonitoringConfigReader()
def "Read basic monitoring config"() {
setup:
def config = this.class.getClassLoader().getResourceAsStream("dshackle-monitoring-basic.yaml")
when:
def act = reader.read(config)
then:
act.enabled
act.prometheus != null
with(act.prometheus) {
enabled
port == 8000
host == "192.168.0.1"
path == "/status/prometheus"
}
}
}

View File

@@ -0,0 +1,7 @@
monitoring:
enabled: true
prometheus:
enabled: true
bind: 192.168.0.1
port: 8000
path: /status/prometheus