diff --git a/theodolite/src/main/kotlin/rocks/theodolite/kubernetes/slo/ExternalSloChecker.kt b/theodolite/src/main/kotlin/rocks/theodolite/kubernetes/slo/ExternalSloChecker.kt index f9a8edc2e570c849611ec2e3ab56455a69644e68..fdf61ae3d1845a0ed42dd8a851e3372fe0923c07 100644 --- a/theodolite/src/main/kotlin/rocks/theodolite/kubernetes/slo/ExternalSloChecker.kt +++ b/theodolite/src/main/kotlin/rocks/theodolite/kubernetes/slo/ExternalSloChecker.kt @@ -42,7 +42,7 @@ class ExternalSloChecker( while (counter < RETRIES) { val request = HttpRequest.newBuilder() - .uri(URI(externalSlopeURL)) + .uri(URI.create(externalSlopeURL)) .POST(HttpRequest.BodyPublishers.ofString(data)) .timeout(TIMEOUT) .build() diff --git a/theodolite/src/main/kotlin/rocks/theodolite/kubernetes/slo/MetricFetcher.kt b/theodolite/src/main/kotlin/rocks/theodolite/kubernetes/slo/MetricFetcher.kt index cda2c3bac2b1599d8e995c504b24924ca294aef2..c7b971fdcbc795a97f17478b6d46e8de23eb768a 100644 --- a/theodolite/src/main/kotlin/rocks/theodolite/kubernetes/slo/MetricFetcher.kt +++ b/theodolite/src/main/kotlin/rocks/theodolite/kubernetes/slo/MetricFetcher.kt @@ -1,10 +1,12 @@ package rocks.theodolite.kubernetes.slo import com.fasterxml.jackson.databind.ObjectMapper -import khttp.get -import khttp.responses.Response import mu.KotlinLogging 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.Instant @@ -18,7 +20,7 @@ private val logger = KotlinLogging.logger {} */ class MetricFetcher(private val prometheusURL: String, private val offset: Duration) { 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. @@ -36,26 +38,27 @@ class MetricFetcher(private val prometheusURL: String, private val offset: Durat val offsetEnd = end.minus(offset) var counter = 0 - val parameter = mapOf( - "query" to query, - "start" to offsetStart.toString(), - "end" to offsetEnd.toString(), - "step" to "5s" - ) while (counter < RETRIES) { logger.info { "Request collected metrics from Prometheus for interval [$offsetStart,$offsetEnd]." } - val response = get("$prometheusURL/api/v1/query_range", params = parameter, timeout = TIMEOUT) - if (response.statusCode != 200) { - val message = response.jsonObject.toString() + val request = HttpRequest.newBuilder() + .uri(URI.create("$prometheusURL/api/v1/query_range?query=$query&start=$offsetStart&end=$offsetEnd&step=5s")) + .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." } counter++ } else { - val values = parseValues(response) + val values = parseValues(response.body()) if (values.data?.result.isNullOrEmpty()) { 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.") @@ -66,9 +69,9 @@ class MetricFetcher(private val prometheusURL: String, private val offset: Durat * @param values Response from Prometheus. * @return a [PrometheusResponse] */ - private fun parseValues(values: Response): PrometheusResponse { + private fun parseValues(values: String): PrometheusResponse { return ObjectMapper().readValue<PrometheusResponse>( - values.jsonObject.toString(), + values, PrometheusResponse::class.java ) }