Skip to content

Commit

Permalink
fix: InfluxDB write interval accuracy
Browse files Browse the repository at this point in the history
  • Loading branch information
Cubxity committed Jul 24, 2021
1 parent 6857cf4 commit 0909561
Showing 1 changed file with 11 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import dev.cubxity.plugins.metrics.api.util.fastForEach
import dev.cubxity.plugins.metrics.api.util.toGoString
import dev.cubxity.plugins.metrics.influx.config.InfluxConfig
import kotlinx.coroutines.*
import kotlin.math.max
import kotlin.system.measureTimeMillis

class InfluxMetricsDriver(private val api: UnifiedMetrics, private val config: InfluxConfig) : MetricsDriver {
private val coroutineScope = CoroutineScope(Dispatchers.Default) + SupervisorJob()
Expand Down Expand Up @@ -68,17 +70,19 @@ class InfluxMetricsDriver(private val api: UnifiedMetrics, private val config: I
}

private fun scheduleTasks() {
val interval = config.output.interval
val interval = config.output.interval * 1000

coroutineScope.launch {
while (true) {
try {
val metrics = api.metricsManager.collect()
writeMetrics(metrics)
} catch (error: Throwable) {
api.logger.severe("An error occurred whilst writing samples to InfluxDB", error)
val time = measureTimeMillis {
try {
val metrics = api.metricsManager.collect()
writeMetrics(metrics)
} catch (error: Throwable) {
api.logger.severe("An error occurred whilst writing samples to InfluxDB", error)
}
}
delay(interval * 1000)
delay(max(0, interval - time))
}
}
}
Expand Down

0 comments on commit 0909561

Please sign in to comment.