Skip to content
Snippets Groups Projects
Commit 0fd4721b authored by Sören Henning's avatar Sören Henning
Browse files

Replace khttp in MetricFetcher

parent 1909b1c3
No related branches found
No related tags found
1 merge request!313Replace khttp
...@@ -42,7 +42,7 @@ class ExternalSloChecker( ...@@ -42,7 +42,7 @@ class ExternalSloChecker(
while (counter < RETRIES) { while (counter < RETRIES) {
val request = HttpRequest.newBuilder() val request = HttpRequest.newBuilder()
.uri(URI(externalSlopeURL)) .uri(URI.create(externalSlopeURL))
.POST(HttpRequest.BodyPublishers.ofString(data)) .POST(HttpRequest.BodyPublishers.ofString(data))
.timeout(TIMEOUT) .timeout(TIMEOUT)
.build() .build()
......
package rocks.theodolite.kubernetes.slo package rocks.theodolite.kubernetes.slo
import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.databind.ObjectMapper
import khttp.get
import khttp.responses.Response
import mu.KotlinLogging import mu.KotlinLogging
import java.net.ConnectException import java.net.ConnectException
import java.net.URI
import java.net.http.HttpClient
import java.net.http.HttpRequest
import java.net.http.HttpResponse
import java.time.Duration import java.time.Duration
import java.time.Instant import java.time.Instant
...@@ -18,7 +20,7 @@ private val logger = KotlinLogging.logger {} ...@@ -18,7 +20,7 @@ private val logger = KotlinLogging.logger {}
*/ */
class MetricFetcher(private val prometheusURL: String, private val offset: Duration) { class MetricFetcher(private val prometheusURL: String, private val offset: Duration) {
private val RETRIES = 2 private val RETRIES = 2
private val TIMEOUT = 60.0 private val TIMEOUT = Duration.ofSeconds(60)
/** /**
* Tries to fetch a metric by a query to a Prometheus server. * Tries to fetch a metric by a query to a Prometheus server.
...@@ -36,26 +38,27 @@ class MetricFetcher(private val prometheusURL: String, private val offset: Durat ...@@ -36,26 +38,27 @@ class MetricFetcher(private val prometheusURL: String, private val offset: Durat
val offsetEnd = end.minus(offset) val offsetEnd = end.minus(offset)
var counter = 0 var counter = 0
val parameter = mapOf(
"query" to query,
"start" to offsetStart.toString(),
"end" to offsetEnd.toString(),
"step" to "5s"
)
while (counter < RETRIES) { while (counter < RETRIES) {
logger.info { "Request collected metrics from Prometheus for interval [$offsetStart,$offsetEnd]." } logger.info { "Request collected metrics from Prometheus for interval [$offsetStart,$offsetEnd]." }
val response = get("$prometheusURL/api/v1/query_range", params = parameter, timeout = TIMEOUT) val request = HttpRequest.newBuilder()
if (response.statusCode != 200) { .uri(URI.create("$prometheusURL/api/v1/query_range?query=$query&start=$offsetStart&end=$offsetEnd&step=5s"))
val message = response.jsonObject.toString() .GET()
.timeout(TIMEOUT)
.build()
val response = HttpClient.newBuilder()
.build()
.send(request, HttpResponse.BodyHandlers.ofString())
if (response.statusCode() != 200) {
val message = response.body()
logger.warn { "Could not connect to Prometheus: $message. Retry $counter/$RETRIES." } logger.warn { "Could not connect to Prometheus: $message. Retry $counter/$RETRIES." }
counter++ counter++
} else { } else {
val values = parseValues(response) val values = parseValues(response.body())
if (values.data?.result.isNullOrEmpty()) { if (values.data?.result.isNullOrEmpty()) {
throw NoSuchFieldException("Empty query result: $values between for query '$query' in interval [$offsetStart,$offsetEnd] .") throw NoSuchFieldException("Empty query result: $values between for query '$query' in interval [$offsetStart,$offsetEnd] .")
} }
return parseValues(response) return parseValues(response.body())
} }
} }
throw ConnectException("No answer from Prometheus received.") throw ConnectException("No answer from Prometheus received.")
...@@ -66,9 +69,9 @@ class MetricFetcher(private val prometheusURL: String, private val offset: Durat ...@@ -66,9 +69,9 @@ class MetricFetcher(private val prometheusURL: String, private val offset: Durat
* @param values Response from Prometheus. * @param values Response from Prometheus.
* @return a [PrometheusResponse] * @return a [PrometheusResponse]
*/ */
private fun parseValues(values: Response): PrometheusResponse { private fun parseValues(values: String): PrometheusResponse {
return ObjectMapper().readValue<PrometheusResponse>( return ObjectMapper().readValue<PrometheusResponse>(
values.jsonObject.toString(), values,
PrometheusResponse::class.java PrometheusResponse::class.java
) )
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment