Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Fix pending_calls metric to not lie #225

Merged
merged 3 commits into from
Aug 18, 2015
Merged
Changes from 1 commit
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
21 changes: 18 additions & 3 deletions synapse/metrics/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,18 +158,33 @@ def runUntilCurrentTimer(func):

@functools.wraps(func)
def f(*args, **kwargs):
pending_calls = len(reactor.getDelayedCalls())
now = reactor.seconds()
num_pending = 0

# _newTimedCalls is one long list of *all* pending calls. Below loop
# is based off of impl of reactor.runUntilCurrent
for p in reactor._newTimedCalls:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might want to check that the _newTimedCalls attribute exists if we are rummaging around in twisted internals.
Oh we do further down the file. Nevermind

if p.time > now:
break

if p.delayed_time > 0:
continue
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be nice to have a more meaningful variable name than "p" here.


num_pending += 1

num_pending += len(reactor.threadCallQueue)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to check if the "threadCallQueue" attribute exists?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


start = time.time() * 1000
ret = func(*args, **kwargs)
end = time.time() * 1000
tick_time.inc_by(end - start)
pending_calls_metric.inc_by(pending_calls)
pending_calls_metric.inc_by(num_pending)
return ret

return f


if hasattr(reactor, "runUntilCurrent"):
if hasattr(reactor, "runUntilCurrent") and hasattr(reactor, "_newTimedCalls"):
# runUntilCurrent is called when we have pending calls. It is called once
# per iteratation after fd polling.
reactor.runUntilCurrent = runUntilCurrentTimer(reactor.runUntilCurrent)