Sets up the callback duration metric

This commit is contained in:
benbierens 2023-08-08 09:20:34 +02:00
parent 466241aa95
commit e07f0dc049
No known key found for this signature in database
GPG Key ID: FE44815D96D0A1AA
2 changed files with 17 additions and 0 deletions

View File

@ -15,6 +15,12 @@ import std/[tables, strutils, heapqueue, deques]
import stew/results
import "."/[config, futures, osdefs, oserrno, osutils, timer]
when defined(chronosEnableCallbackDurationMetric):
import std/monotimes
import pkg/metrics
declareGauge(chronosCallbackDuration, "chronos callback duration")
export Port
export futures, timer, results
@ -260,8 +266,15 @@ template processCallbacks(loop: untyped) =
if isSentinel(callable):
break
if not(isNil(callable.function)):
when defined(chronosEnableCallbackDurationMetric):
let startTime = getMonoTime().ticks
callable.function(callable.udata)
when defined(chronosEnableCallbackDurationMetric):
let durationUs = (getMonoTime().ticks - startTime) div 1000
chronosCallbackDuration.set(durationNs)
proc raiseAsDefect*(exc: ref Exception, msg: string) {.noreturn, noinline.} =
# Reraise an exception as a Defect, where it's unexpected and can't be handled
# We include the stack trace in the message because otherwise, it's easily

View File

@ -70,6 +70,10 @@ when (NimMajor, NimMinor) >= (1, 4):
""
## OS polling engine type which is going to be used by chronos.
chronosEnableCallbackDurationMetric {.booldefine.} = false
## At the cost of some performance, produce a 'chronosCallbackDuration' metric.
## Useful for detecting application stalling/blocking.
else:
# 1.2 doesn't support `booldefine` in `when` properly
const