From a97edd34596bbd58f7723aab04436c6d728caf16 Mon Sep 17 00:00:00 2001
From: lorenz <stu203404@mail.uni-kiel.de>
Date: Fri, 2 Sep 2022 18:21:57 +0200
Subject: [PATCH] Add operator integration of the tupp slo-checker

---
 .../kubernetes/slo/SloCheckerFactory.kt        | 18 ++++++++++++++++++
 .../kubernetes/slo/SloConfigHandler.kt         |  5 +++--
 .../theodolite/kubernetes/slo/SloTypes.kt      |  4 +++-
 3 files changed, 24 insertions(+), 3 deletions(-)

diff --git a/theodolite/src/main/kotlin/rocks/theodolite/kubernetes/slo/SloCheckerFactory.kt b/theodolite/src/main/kotlin/rocks/theodolite/kubernetes/slo/SloCheckerFactory.kt
index a789050a1..3f7c1001f 100644
--- a/theodolite/src/main/kotlin/rocks/theodolite/kubernetes/slo/SloCheckerFactory.kt
+++ b/theodolite/src/main/kotlin/rocks/theodolite/kubernetes/slo/SloCheckerFactory.kt
@@ -71,6 +71,24 @@ class SloCheckerFactory {
                         ?: throw IllegalArgumentException("threshold expected"))
                 )
             )
+            SloTypes.AVERAGE_LAG -> ExternalSloChecker(
+                externalSlopeURL = properties["externalSloUrl"]
+                    ?: throw IllegalArgumentException("externalSloUrl expected"),
+                metadata = mapOf(
+                    "warmup" to (properties["warmup"]?.toInt()
+                        ?: throw IllegalArgumentException("warmup expected")),
+                    "beforeWindowLength" to (properties["beforeWindowLength"]?.toInt()
+                        ?: throw IllegalArgumentException("beforeWindowLength expected")),
+                    "ignoreDuring" to (properties["ignoreDuring"]?.toInt()
+                        ?: throw IllegalArgumentException("beforeWindowLength expected")),
+                    "afterWindowLength" to (properties["afterWindowLength"]?.toInt()
+                        ?: throw IllegalArgumentException("afterWindowLength expected")),
+                    "error" to (properties["error"]?.toDouble()
+                        ?: throw IllegalArgumentException("error expected")),
+                    "ratio" to (properties["ratio"]?.toDouble()?.times(load)
+                        ?: throw IllegalArgumentException("Threshold ratio is incorrect"))
+                )
+            )
             SloTypes.LAG_TREND_RATIO, SloTypes.DROPPED_RECORDS_RATIO -> {
                 val thresholdRatio =
                     properties["ratio"]?.toDouble()
diff --git a/theodolite/src/main/kotlin/rocks/theodolite/kubernetes/slo/SloConfigHandler.kt b/theodolite/src/main/kotlin/rocks/theodolite/kubernetes/slo/SloConfigHandler.kt
index ed18e4a0b..8dfcec3ae 100644
--- a/theodolite/src/main/kotlin/rocks/theodolite/kubernetes/slo/SloConfigHandler.kt
+++ b/theodolite/src/main/kotlin/rocks/theodolite/kubernetes/slo/SloConfigHandler.kt
@@ -14,12 +14,13 @@ class SloConfigHandler {
         fun getQueryString(slo: Slo): String {
             return when (slo.sloType.lowercase()) {
                 SloTypes.GENERIC.value -> slo.properties["promQLQuery"] ?: throw IllegalArgumentException("promQLQuery expected")
-                SloTypes.LAG_TREND.value, SloTypes.LAG_TREND_RATIO.value -> slo.properties["promQLQuery"] ?:
+                SloTypes.LAG_TREND.value, SloTypes.LAG_TREND_RATIO.value, SloTypes.AVERAGE_LAG.value ->
+                    slo.properties["promQLQuery"] ?:
                     (slo.properties["consumerGroup"]?.let { "{consumergroup='$it'}" } ?: "").let {
                         "sum by(consumergroup) ($DEFAULT_CONSUMER_LAG_METRIC_BASE$it >= 0)"
                     }
                 SloTypes.DROPPED_RECORDS.value, SloTypes.DROPPED_RECORDS_RATIO.value -> slo.properties["promQLQuery"] ?: DEFAULT_DROPPED_RECORDS_QUERY
-                SloTypes.LAG_TREND.value, SloTypes.LAG_TREND_RATIO.value -> slo.properties["promQLQuery"] ?: DEFAULT_CONSUMER_LAG_QUERY
+                SloTypes.LAG_TREND.value, SloTypes.LAG_TREND_RATIO.value, SloTypes.AVERAGE_LAG.value -> slo.properties["promQLQuery"] ?: DEFAULT_CONSUMER_LAG_QUERY
                 SloTypes.DROPPED_RECORDS.value, SloTypes.DROPPED_RECORDS_RATIO.value -> slo.properties["promQLQuery"] ?: DEFAULT_DROPPED_RECORDS_QUERY
                 else -> throw  InvalidPatcherConfigurationException("Could not find Prometheus query string for slo type ${slo.sloType}")
             }
diff --git a/theodolite/src/main/kotlin/rocks/theodolite/kubernetes/slo/SloTypes.kt b/theodolite/src/main/kotlin/rocks/theodolite/kubernetes/slo/SloTypes.kt
index 07cbcd634..7dab8b128 100644
--- a/theodolite/src/main/kotlin/rocks/theodolite/kubernetes/slo/SloTypes.kt
+++ b/theodolite/src/main/kotlin/rocks/theodolite/kubernetes/slo/SloTypes.kt
@@ -5,7 +5,9 @@ enum class SloTypes(val value: String) {
     LAG_TREND("lag trend"),
     LAG_TREND_RATIO("lag trend ratio"),
     DROPPED_RECORDS("dropped records"),
-    DROPPED_RECORDS_RATIO("dropped records ratio");
+    DROPPED_RECORDS_RATIO("dropped records ratio"),
+    AVERAGE_LAG("average lag");
+
 
     companion object {
         fun from(type: String): SloTypes =
-- 
GitLab