Skip to content

Commit 204cbc0

Browse files
committed
[PROD-14034] Reduce loki query range to avoid rounding errors
We think that passing the exact max query range to loki can lead to some rounding error if the values do not align with the current moon phase. Reducing drastically (from the default 30d1h default value) the query range should limit this issue. Anyway, loki should not be used like that from the api, it's better suited for grafana and we should think going back to archiving logs in argo workflows directly.
1 parent 8eb3b7a commit 204cbc0

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

src/main/kotlin/com/cosmotech/api/loki/LokiService.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import com.fasterxml.jackson.databind.ObjectMapper
99
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory
1010
import com.fasterxml.jackson.module.kotlin.readValue
1111
import java.time.Instant
12+
import kotlin.math.min
1213
import kotlin.time.Duration
14+
import kotlin.time.Duration.Companion.days
1315
import org.json.JSONArray
1416
import org.json.JSONObject
1517
import org.springframework.http.HttpHeaders
@@ -60,9 +62,10 @@ class LokiService(private val csmPlatformProperties: CsmPlatformProperties) {
6062
val lokiConfig = getLokiConfig()
6163
val maxQueryLengthNano =
6264
Duration.parse(lokiConfig.limitsConfig.maxQueryLength).inWholeNanoseconds
65+
val queryLengthNano = min(maxQueryLengthNano, 1.days.inWholeNanoseconds)
6366

6467
val startTimeNano = startTime.toEpochMilli() * MILLI_TO_NANO
65-
val endTimeNano = startTimeNano + maxQueryLengthNano
68+
val endTimeNano = startTimeNano + queryLengthNano
6669

6770
val params = LinkedMultiValueMap<String, String>()
6871
params.add("query", "{namespace=\"$namespace\",pod=\"$podName\",container=\"main\"}")
@@ -98,7 +101,7 @@ class LokiService(private val csmPlatformProperties: CsmPlatformProperties) {
98101
logEntries.lastOrNull()?.let {
99102
val newStartTime = it.getLong(0) + 1
100103
params.set("start", newStartTime.toString())
101-
params.set("end", (newStartTime + maxQueryLengthNano).toString())
104+
params.set("end", (newStartTime + queryLengthNano).toString())
102105
}
103106
} while (logEntries.size >= lokiConfig.limitsConfig.maxEntriesLimitPerQuery)
104107

0 commit comments

Comments
 (0)