Skip to content

Commit

Permalink
Allow taking more metrics than just the first one
Browse files Browse the repository at this point in the history
  • Loading branch information
SoerenHenning committed Dec 13, 2023
1 parent 5297a67 commit 85b1e97
Show file tree
Hide file tree
Showing 7 changed files with 8,989 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class AnalysisExecutor(

/**
* Analyses an experiment via prometheus data.
* First fetches data from prometheus, then documents them and afterwards evaluate it via a [slo].
* First fetches data from prometheus, then documents them and afterward evaluate it via a [slo].
* @param load of the experiment.
* @param resource of the experiment.
* @param executionIntervals list of start and end points of experiments
Expand All @@ -41,6 +41,7 @@ class AnalysisExecutor(
val fileURL = "${resultsFolder}exp${executionId}_${load}_${resource}_${slo.sloType.toSlug()}"

val stepSize = slo.properties["promQLStepSeconds"]?.toLong()?.let { Duration.ofSeconds(it) } ?: DEFAULT_STEP_SIZE
val onlyFirstMetric = slo.properties["takeOnlyFirstMetric"]?.toBoolean() ?: true

val prometheusData = executionIntervals
.map { interval ->
Expand All @@ -55,7 +56,7 @@ class AnalysisExecutor(
prometheusData.forEach{ data ->
ioHandler.writeToCSVFile(
fileURL = "${fileURL}_${slo.name}_${repetitionCounter++}",
data = data.getResultAsList(),
data = data.getResultAsList(onlyFirst = onlyFirstMetric),
columns = listOf("labels", "timestamp", "value")
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,24 @@ data class PrometheusResponse(
* The format of the returned list is: `[[ group, timestamp, value ], [ group, timestamp, value ], ... ]`
*/
@JsonIgnore
fun getResultAsList(): List<List<String>> {
val group = data?.result?.get(0)?.metric?.toString()!!
val values = data?.result?.get(0)?.values
val result = mutableListOf<List<String>>()
fun getResultAsList(onlyFirst: Boolean = true): List<List<String>> {
val resultsList = mutableListOf<List<String>>()

if (values != null) {
for (value in values) {
val valueList = value as List<*>
val timestamp = (valueList[0] as Double).toLong().toString()
val resultValue = valueList[1].toString()
result.add(listOf(group, timestamp, resultValue))
val results = data?.result ?: throw IllegalStateException("No 'results' available in the Prometheus response.")
for (result in results.subList(0, if (onlyFirst && results.isNotEmpty()) 1 else results.size)) {
val group = result.metric.toString()
val values = result.values

if (values != null) {
for (value in values) {
val valueList = value as List<*>
val timestamp = (valueList[0] as Number).toLong().toString()
val resultValue = valueList[1].toString()
resultsList.add(listOf(group, timestamp, resultValue))
}
}
}
return Collections.unmodifiableList(result)
return resultsList.toList()
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package rocks.theodolite.kubernetes.slo

import com.fasterxml.jackson.databind.ObjectMapper
import org.junit.jupiter.api.Test

import org.junit.jupiter.api.Assertions.*

class PrometheusResponseTest {

private val objectMapper = ObjectMapper()

@Test
fun testSingleTimeseriesListConvert() {
val prometheusResponse = readSingleTimeseriesResponse()
val resultsList = prometheusResponse.getResultAsList()
assertEquals(126, resultsList.size)
}

@Test
fun testSingleTimeseriesWithNoMillisListConvert() {
val prometheusResponse = readSingleTimeseriesNoMillisResponse()
val resultsList = prometheusResponse.getResultAsList()
assertEquals(11, resultsList.size)
}

@Test
fun testNoTimeseriesListConvert() {
val prometheusResponse = readNoTimeseriesResponse()
val resultsList = prometheusResponse.getResultAsList()
assertEquals(0, resultsList.size)
}

@Test
fun testMultiTimeseriesListConvertWithOnlyFirst() {
val prometheusResponse = readMultiTimeseriesResponse()
val resultsList = prometheusResponse.getResultAsList()
assertEquals(17, resultsList.size)
}

@Test
fun testMultiTimeseriesListConvertWithoutOnlyFirst() {
val prometheusResponse = readMultiTimeseriesResponse()
val resultsList = prometheusResponse.getResultAsList(onlyFirst = false)
assertEquals(1700, resultsList.size)
}

private fun readMultiTimeseriesResponse(): PrometheusResponse {
return objectMapper.readValue(
javaClass.getResourceAsStream("/prometheus-response/multi-timeseries.json"),
PrometheusResponse::class.java
)
}

private fun readSingleTimeseriesResponse(): PrometheusResponse {
return objectMapper.readValue(
javaClass.getResourceAsStream("/prometheus-response/single-timeseries.json"),
PrometheusResponse::class.java
)
}

private fun readSingleTimeseriesNoMillisResponse(): PrometheusResponse {
return objectMapper.readValue(
javaClass.getResourceAsStream("/prometheus-response/single-timeseries-nomillis.json"),
PrometheusResponse::class.java
)
}

private fun readNoTimeseriesResponse(): PrometheusResponse {
return objectMapper.readValue(
javaClass.getResourceAsStream("/prometheus-response/empty-timeseries.json"),
PrometheusResponse::class.java
)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"status": "success",
"data": {
"resultType": "matrix",
"result": [
]
}
}
Loading

0 comments on commit 85b1e97

Please sign in to comment.