August Lilleaas
08/04/2025, 4:48 PMsuspend fun <T> CoroutineScope.logSlowQueries(
timeoutSeconds: Long?,
block: suspend () -> T
): T {
val slowQueryDetectorJob = async {
delay(Duration.ofSeconds(timeoutSeconds ?: 10))
val otelCtx = Span.current().spanContext
if (otelCtx.isValid) {
log.error("Query not completed after $timeoutSeconds seconds - traceId=${otelCtx.traceId} spanId=${otelCtx.spanId}")
} else {
log.error("Query not completed after $timeoutSeconds seconds - NO OTEL CTX AVAILABLE")
}
}
return try {
block()
} finally {
slowQueryDetectorJob.cancel()
}
}