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

bug: Fix metric calls #1462

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
30 changes: 15 additions & 15 deletions autopush/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ def __init__(self, *args, **kwargs):
def start(self):
"""Start any connection needed for metric transmission"""

def increment(self, name, count=1, **kwargs):
def increment(self, name, count=1, tags=None):
"""Increment a counter for a metric name"""
raise NotImplementedError("No increment implemented")

def gauge(self, name, count, **kwargs):
def gauge(self, name, count, tags=None):
"""Record a gauge for a metric name"""
raise NotImplementedError("No gauge implemented")

def timing(self, name, duration, **kwargs):
def timing(self, name, duration, tags=None):
"""Record a timing in ms for a metric name"""
raise NotImplementedError("No timing implemented")

Expand All @@ -69,14 +69,14 @@ def start(self):
protocol = StatsDClientProtocol(self.client)
reactor.listenUDP(0, protocol)

def increment(self, name, count=1, **kwargs):
self._metric.increment(name, count, **kwargs)
def increment(self, name, count=1, tags=None):
self._metric.increment(name, count, tags=tags)

def gauge(self, name, count, **kwargs):
self._metric.gauge(name, count, **kwargs)
def gauge(self, name, count, tags=None):
self._metric.gauge(name, count, tags=tags)

def timing(self, name, duration, **kwargs):
self._metric.timing(name, duration, **kwargs)
def timing(self, name, duration, tags=None):
self._metric.timing(name, duration, tags=tags)


def make_tags(base=None, **kwargs):
Expand Down Expand Up @@ -106,17 +106,17 @@ def start(self):
self._client.start(flush_interval=self._flush_interval,
roll_up_interval=self._flush_interval)

def increment(self, name, count=1, **kwargs):
def increment(self, name, count=1, tags=None):
self._client.increment(self._prefix_name(name), count, host=self._host,
**kwargs)
tags=tags)

def gauge(self, name, count, **kwargs):
def gauge(self, name, count, tags=None):
self._client.gauge(self._prefix_name(name), count, host=self._host,
**kwargs)
tags=tags)

def timing(self, name, duration, **kwargs):
def timing(self, name, duration, tags=None):
self._client.timing(self._prefix_name(name), value=duration,
host=self._host, **kwargs)
host=self._host, tags=tags)


def from_config(conf):
Expand Down
5 changes: 4 additions & 1 deletion autopush/router/apnsrouter.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,10 @@ def _route(self, notification, router_data):
elif isinstance(e, (HTTP20Error, socket.error)):
reason = "http2_error"
else:
reason = e.extra.get("reason", "unknown")
try:
reason = e.extra.get("reason", "unknown")
except AttributeError:
reason = "unknown"
if isinstance(e, RouterException) and e.status_code in [404, 410]:
raise RouterException(
str(e),
Expand Down
2 changes: 1 addition & 1 deletion autopush/tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -2193,7 +2193,7 @@ def test_successful(self):
}),
body=data
)
print ("Response: %s" % response.code)
print("Response: %s" % response.code)
assert response.code == 201

ca_data = self._mock_send.post.mock_calls[1][2]['json']['data']
Expand Down
15 changes: 9 additions & 6 deletions autopush/tests/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ def test_basic(self, mock_reactor):
assert len(mock_reactor.mock_calls) > 0
m._metric = Mock()
m.increment("test", 5)
m._metric.increment.assert_called_with("test", 5)
m._metric.increment.assert_called_with("test", 5, tags=None)
m.gauge("connection_count", 200)
m._metric.gauge.assert_called_with("connection_count", 200)
m._metric.gauge.assert_called_with("connection_count", 200, tags=None)
m.timing("lifespan", 113)
m._metric.timing.assert_called_with("lifespan", 113)
m._metric.timing.assert_called_with("lifespan", 113, tags=None)

@patch("autopush.metrics.reactor")
def test_tags(self, mock_reactor):
Expand Down Expand Up @@ -77,13 +77,16 @@ def test_basic(self, mock_dog):
roll_up_interval=10)
m.increment("test", 5)
m._client.increment.assert_called_with("testpush.test", 5,
host=hostname)
host=hostname,
tags=None)
m.gauge("connection_count", 200)
m._client.gauge.assert_called_with("testpush.connection_count", 200,
host=hostname)
host=hostname,
tags=None)
m.timing("lifespan", 113)
m._client.timing.assert_called_with("testpush.lifespan", value=113,
host=hostname)
host=hostname,
tags=None)


class PeriodicReporterTestCase(unittest.TestCase):
Expand Down
5 changes: 3 additions & 2 deletions autopush/web/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,12 +338,13 @@ def _router_fail_err(self, fail, router_type=None, vapid=False, uaid=None):
self._base_tags.update({
"platform": router_type,
"reason": "unregistered",
"error": exc.status_code,
"errno": 0
})
self.metrics.increment(
"notification.bridge.error",
tags=self._base_tags,
error=exc.status_code,
errno=0)
)

self.db.router.drop_user(uaid)
self._router_response(exc, router_type, vapid)
Expand Down