Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Commit

Permalink
bug: Restore periodicreporter after complex merge dropped it.
Browse files Browse the repository at this point in the history
Closes #1119
  • Loading branch information
jrconlin committed Feb 2, 2018
1 parent c48d043 commit d9233f5
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
4 changes: 4 additions & 0 deletions autopush/main.py
Expand Up @@ -35,6 +35,7 @@
from autopush.haproxy import HAProxyServerEndpoint
from autopush.logging import PushLogger
from autopush.main_argparse import parse_connection, parse_endpoint
from autopush.metrics import periodic_reporter
from autopush.router import routers_from_config
from autopush.ssl import (
monkey_patch_ssl_wrap_socket,
Expand Down Expand Up @@ -189,6 +190,8 @@ def setup(self, rotate_tables=True):
# Start the table rotation checker/updater
if rotate_tables:
self.add_timer(60, self.db.update_rotating_tables)
self.add_timer(15, periodic_reporter, self.db.metrics,
prefix='autoendpoint')

def add_endpoint(self):
"""Start the Endpoint HTTP router"""
Expand Down Expand Up @@ -264,6 +267,7 @@ def setup(self, rotate_tables=True):
# Start the table rotation checker/updater
if rotate_tables:
self.add_timer(60, self.db.update_rotating_tables)
self.add_timer(15, periodic_reporter, self.db.metrics)

def add_internal_router(self):
"""Start the internal HTTP notification router"""
Expand Down
18 changes: 18 additions & 0 deletions autopush/metrics.py
Expand Up @@ -129,3 +129,21 @@ def from_config(conf):
return TwistedMetrics(conf.statsd_host, conf.statsd_port)
else:
return SinkMetrics()


def periodic_reporter(metrics, prefix=''):
# type: (IMetrics, Optional[str]) -> None
"""Emit metrics on twisted's thread pool.
Only meant to be called via a LoopingCall (TimerService).
"""
# unfortunately stats only available via the private '_team'
stats = reactor.getThreadPool()._team.statistics()
for attr in ('idleWorkerCount', 'busyWorkerCount', 'backloggedWorkCount'):
name = '{}{}twisted.threadpool.{}'.format(
prefix,
'.' if prefix else '',
attr
)
metrics.gauge(name, getattr(stats, attr))
19 changes: 18 additions & 1 deletion autopush/tests/test_metrics.py
Expand Up @@ -3,13 +3,14 @@
import twisted.internet.base

import pytest
from mock import Mock, patch
from mock import Mock, patch, call

from autopush.metrics import (
IMetrics,
DatadogMetrics,
TwistedMetrics,
SinkMetrics,
periodic_reporter,
)


Expand Down Expand Up @@ -71,3 +72,19 @@ def test_basic(self, mock_dog):
m.timing("lifespan", 113)
m._client.timing.assert_called_with("testpush.lifespan", value=113,
host=hostname)


class PeriodicReporterTestCase(unittest.TestCase):

def test_periodic_reporter(self):
metrics = Mock(spec=SinkMetrics)
periodic_reporter(metrics)
periodic_reporter(metrics, prefix='foo')
metrics.gauge.assert_has_calls([
call('twisted.threadpool.idleWorkerCount', 0),
call('twisted.threadpool.busyWorkerCount', 0),
call('twisted.threadpool.backloggedWorkCount', 0),
call('foo.twisted.threadpool.idleWorkerCount', 0),
call('foo.twisted.threadpool.busyWorkerCount', 0),
call('foo.twisted.threadpool.backloggedWorkCount', 0),
])

0 comments on commit d9233f5

Please sign in to comment.