Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Callback duration metric #439

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions chronos/asyncloop.nim
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ import std/[tables, strutils, heapqueue, deques]
import stew/results
import "."/[config, futures, osdefs, oserrno, osutils, timer]

when chronosEnableCallbackDurationMetric:
import std/monotimes
import pkg/metrics

declareGauge(chronosCallbackDuration, "chronos callback duration")

export Port
export futures, timer, results

Expand Down Expand Up @@ -260,8 +266,15 @@ template processCallbacks(loop: untyped) =
if isSentinel(callable):
break
if not(isNil(callable.function)):
when chronosEnableCallbackDurationMetric:
let startTime = getMonoTime().ticks

callable.function(callable.udata)

when chronosEnableCallbackDurationMetric:
let durationUs = (getMonoTime().ticks - startTime) div 1000
chronosCallbackDuration.set(durationUs)

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
Expand Down
4 changes: 4 additions & 0 deletions chronos/config.nim
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ when (NimMajor, NimMinor) >= (1, 4):
""
## OS polling engine type which is going to be used by chronos.

chronosEnableCallbackDurationMetric* {.booldefine.} = defined(chronosEnableCallbackDurationMetric)
## 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
Expand Down
Loading