Skip to content
Snippets Groups Projects
Commit 2adc3612 authored by Lorenz Boguhn's avatar Lorenz Boguhn
Browse files

Add parsing and improve connection error handling

parent 9fcbe073
No related branches found
No related tags found
4 merge requests!159Re-implementation of Theodolite with Kotlin/Quarkus,!157Update Graal Image in CI pipeline,!96Handle shutdown,!83WIP: Re-implementation of Theodolite with Kotlin/Quarkus
...@@ -20,6 +20,7 @@ dependencies { ...@@ -20,6 +20,7 @@ dependencies {
implementation 'io.quarkus:quarkus-resteasy' implementation 'io.quarkus:quarkus-resteasy'
testImplementation 'io.quarkus:quarkus-junit5' testImplementation 'io.quarkus:quarkus-junit5'
testImplementation 'io.rest-assured:rest-assured' testImplementation 'io.rest-assured:rest-assured'
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'org.slf4j:slf4j-simple:1.7.29' implementation 'org.slf4j:slf4j-simple:1.7.29'
implementation 'io.github.microutils:kotlin-logging:1.12.0' implementation 'io.github.microutils:kotlin-logging:1.12.0'
......
package theodolite.evaluation package theodolite.evaluation
import com.google.gson.Gson
import khttp.get import khttp.get
import khttp.responses.Response
import theodolite.util.PrometheusResponse
import java.net.ConnectException
import java.util.* import java.util.*
class MetricFetcher(private val prometheusURL: String) { class MetricFetcher(private val prometheusURL: String) {
fun fetchMetric(start: Long, end: Long, query: String): Any { fun fetchMetric(start: Long, end: Long, query: String): PrometheusResponse {
var trys = 0
val parameter = mapOf( val parameter = mapOf(
"query" to query, "query" to query,
"start" to toISODate(start), "start" to toISODate(start),
...@@ -15,19 +19,27 @@ class MetricFetcher(private val prometheusURL: String) { ...@@ -15,19 +19,27 @@ class MetricFetcher(private val prometheusURL: String) {
"step" to "5s" "step" to "5s"
) )
val response = get("$prometheusURL/api/v1/query_range", params = parameter) while (trys < 2) {
//TODO FIX: NOTHING RECEIVED val response = get("$prometheusURL/api/v1/query_range", params = parameter)
return response.jsonObject.getJSONObject("data").getJSONArray("result")//.getJSONObject(0)["values"] if (response.statusCode != 200) {
trys++
} else {
return parseValues(response)
}
}
throw ConnectException("No answer from Prometheus received")
} }
private fun toISODate(timestamp: Long): String { private fun toISODate(timestamp: Long): String {
val sdf = java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.sss'Z'") val sdf = java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.sss'Z'")
val date = Date(timestamp - (3600 * 1000)) val date = Date(timestamp - (3600 * 1000))//subtract 1h since cluster is in another timezone
return sdf.format(date) return sdf.format(date)
} }
private fun parseValues(values: String): Any { private fun parseValues(values: Response): PrometheusResponse {
// TODO("pars with gson") return Gson().fromJson<PrometheusResponse>(
return "" values.jsonObject.toString(),
PrometheusResponse::class.java
)
} }
} }
...@@ -2,18 +2,25 @@ package theodolite.evaluation ...@@ -2,18 +2,25 @@ package theodolite.evaluation
import khttp.post import khttp.post
import org.json.JSONObject import org.json.JSONObject
import java.net.ConnectException
class SLOCheckerImpl(private val prometheusURL: String) : SLOChecker { class SLOCheckerImpl(private val prometheusURL: String) : SLOChecker {
override fun evaluate(start: Long, end: Long): Boolean { override fun evaluate(start: Long, end: Long): Boolean {
var counter = 0
val metricFetcher = MetricFetcher(prometheusURL = prometheusURL) val metricFetcher = MetricFetcher(prometheusURL = prometheusURL)
val totalLag = metricFetcher.fetchMetric(start, end, "sum by(group)(kafka_consumergroup_group_lag > 0)") val fetchedData = metricFetcher.fetchMetric(start, end, "sum by(group)(kafka_consumergroup_group_lag > 0)")
val parameter = mapOf("total_lag" to totalLag) val data = JSONObject(mapOf("total_lag" to fetchedData.data?.result))
while (counter < 2) {
val result = post("http://127.0.0.1:8000/evaluate-slope", data = data)
if (result.statusCode != 200) {
counter++
} else {
return result.text.toBoolean()
}
}
//val response = get("http://127.0.0.1:8000/evaluate-slope", params = parameter) throw ConnectException("Could not reach slope evaluation")
val post = post("http://127.0.0.1:8000/evaluate-slope", data = JSONObject(parameter))
//println(JSONObject(parameter))
//println(post.text.toBoolean())
return post.text.toBoolean()//response.jsonObject["suitable"] == "true"
} }
} }
package theodolite.util
data class PrometheusResponse(
var status: String? = null,
var data: PromData? = null
)
data class PromData(
var resultType: String? = null,
var result: List<PromResult>? = null
)
data class PromResult(
var metric: PromMetric? = null,
var values: List<Object>? = null
)
data class PromMetric(
var group: String? = null
)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment