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 1f37142ad0ecb679c7f86beadc22e96353430258..c2664364804d7b05c8d19c9923ed7f2e6ed9ee41 100644 --- a/theodolite/src/main/kotlin/rocks/theodolite/kubernetes/slo/SloCheckerFactory.kt +++ b/theodolite/src/main/kotlin/rocks/theodolite/kubernetes/slo/SloCheckerFactory.kt @@ -60,9 +60,9 @@ class SloCheckerFactory { "repetitionAggregation" to (properties["repetitionAggregation"] ?: throw IllegalArgumentException("repetitionAggregation expected")), "operator" to (properties["operator"] ?: throw IllegalArgumentException("operator expected")), - "threshold" to (properties["threshold"]?.toDouble() - ?: properties["thresholdRelToLoad"]?.toDouble()?.times(load) - ?: properties["thresholdRelToResources"]?.toDouble()?.times(resources) + "threshold" to (properties["threshold"]?.toDoubleOrNull() + ?: properties["thresholdRelToLoad"]?.toDoubleOrNull()?.times(load) + ?: properties["thresholdRelToResources"]?.toDoubleOrNull()?.times(resources) ?: throw IllegalArgumentException("'threshold', 'thresholdRelToLoad' or 'thresholdRelToResources' expected")) ) ) @@ -71,10 +71,10 @@ class SloCheckerFactory { ?: throw IllegalArgumentException("externalSloUrl expected"), metadata = mapOf( "warmup" to (properties["warmup"]?.toInt() ?: throw IllegalArgumentException("warmup expected")), - "threshold" to (properties["threshold"]?.toDouble() - ?: properties["thresholdRelToLoad"]?.toDouble()?.times(load) - ?: properties["thresholdRelToResources"]?.toDouble()?.times(resources) - ?: throw IllegalArgumentException("'threshold', 'thresholdRelToLoad' or 'thresholdRelToResources' expected")) + "threshold" to (properties["threshold"]?.toDoubleOrNull() + ?: properties["thresholdRelToLoad"]?.toDoubleOrNull()?.times(load) + ?: properties["thresholdRelToResources"]?.toDoubleOrNull()?.times(resources) + ?: throw IllegalArgumentException("Valid 'threshold', 'thresholdRelToLoad' or 'thresholdRelToResources' expected")) ) ) SloTypes.LAG_TREND_RATIO, SloTypes.DROPPED_RECORDS_RATIO -> { diff --git a/theodolite/src/test/kotlin/rocks/theodolite/kubernetes/slo/SloCheckerFactoryTest.kt b/theodolite/src/test/kotlin/rocks/theodolite/kubernetes/slo/SloCheckerFactoryTest.kt index ef018f1ecfaebeb5c9441211e7f8dc81f947ac6a..5f1a850050d139de7cb1c8fa59a30432c277e107 100644 --- a/theodolite/src/test/kotlin/rocks/theodolite/kubernetes/slo/SloCheckerFactoryTest.kt +++ b/theodolite/src/test/kotlin/rocks/theodolite/kubernetes/slo/SloCheckerFactoryTest.kt @@ -152,6 +152,30 @@ internal class SloCheckerFactoryTest { assertEquals(10.0, computedThreshold as Double, 0.001) } + @Test + fun testCreateGenericSloWithThresholdRelToLoadAndInvalidThreshold() { + val factory = SloCheckerFactory() + val sloChecker = factory.create( + SloTypes.GENERIC.value, + mapOf( + "externalSloUrl" to "http://localhost:1234", + "warmup" to "60", + "queryAggregation" to "median", + "repetitionAggregation" to "median", + "operator" to "lte", + "threshold" to "", + "thresholdRelToLoad" to "0.1" + ), + 100, + 5, + Metric.DEMAND + ) + assertTrue(sloChecker is ExternalSloChecker) + val computedThreshold = (sloChecker as ExternalSloChecker).metadata["threshold"] + assertTrue(computedThreshold is Double) + assertEquals(10.0, computedThreshold as Double, 0.001) + } + @Test fun testCreateGenericSloWithThresholdRelToResources() { val factory = SloCheckerFactory() @@ -290,6 +314,27 @@ internal class SloCheckerFactoryTest { assertEquals(10.0, computedThreshold as Double, 0.001) } + @Test + fun testCreateLagTrendSloWithThresholdRelToLoadAndInvalidThreshold() { + val factory = SloCheckerFactory() + val sloChecker = factory.create( + SloTypes.LAG_TREND.value, + mapOf( + "externalSloUrl" to "http://localhost:1234", + "warmup" to "60", + "threshold" to "", + "thresholdRelToLoad" to "0.1" + ), + 100, + 5, + Metric.DEMAND + ) + assertTrue(sloChecker is ExternalSloChecker) + val computedThreshold = (sloChecker as ExternalSloChecker).metadata["threshold"] + assertTrue(computedThreshold is Double) + assertEquals(10.0, computedThreshold as Double, 0.001) + } + @Test fun testCreateLagTrendSloWithThresholdRelToResources() { val factory = SloCheckerFactory()