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

Commit

Permalink
feat: add metrics to bridge protocols
Browse files Browse the repository at this point in the history
This adds metric calls for attempted and successful bridge sends.
format:
        updates.client.bridge.[apns|gcm].[attempted|succeeded]
Errors are already logged.

closes #497
  • Loading branch information
jrconlin committed Jun 29, 2016
1 parent 654a01a commit 126203b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
8 changes: 8 additions & 0 deletions autopush/router/apnsrouter.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def _connect(self):
def __init__(self, ap_settings, router_conf):
"""Create a new APNS router and connect to APNS"""
self.ap_settings = ap_settings
self._base_tags = []
self.config = router_conf
self.default_title = router_conf.get("default_title", "SimplePush")
self.default_body = router_conf.get("default_body", "New Alert")
Expand Down Expand Up @@ -87,13 +88,20 @@ def _route(self, notification, router_data):
self.messages[now] = {"token": token, "payload": payload}
# TODO: Add listener for error handling.
self.apns.gateway_server.register_response_listener(self._error)
self.ap_settings.metrics.increment(
"updates.client.bridge.apns.attempted",
self._base_tags)

self.apns.gateway_server.send_notification(token, payload, now)

# cleanup sent messages
if self.messages:
for time_sent in self.messages.keys():
if time_sent < now - self.config.get("expry", 10):
del self.messages[time_sent]
self.ap_settings.metrics.increment(
"updates.client.bridge.apns.succeed",
self._base_tags)
return RouterResponse(status_code=200, response_body="Message sent")

def _error(self, err):
Expand Down
6 changes: 6 additions & 0 deletions autopush/router/gcm.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ def __init__(self, ap_settings, router_conf):
self.dryRun = router_conf.get("dryrun", False)
self.collapseKey = router_conf.get("collapseKey", "simplepush")
self.senderIDs = router_conf.get("senderIDs")
self.metrics = ap_settings.metrics
self._base_tags = []
if not self.senderIDs:
self.senderIDs = SenderIDs(router_conf)
try:
Expand Down Expand Up @@ -103,6 +105,8 @@ def _route(self, notification, router_data):
except Exception, e:
raise self._error("Unhandled exception in GCM Routing: %s" % e,
500)
self.metrics.increment("updates.client.bridge.gcm.attempted",
self._base_tags)
return self._process_reply(result)

def _error(self, err, status, **kwargs):
Expand Down Expand Up @@ -144,4 +148,6 @@ def _process_reply(self, reply):
raise RouterException("GCM failure to deliver", status_code=503,
response_body="Please try request later.")

self.metrics.increment("updates.client.bridge.gcm.succeeded",
self._base_tags)
return RouterResponse(status_code=200, response_body="Message Sent")
7 changes: 6 additions & 1 deletion autopush/tests/test_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,12 @@ def throw_ex():
raise AttributeError
fsenderids = Mock()
fsenderids.choose_ID.side_effect = throw_ex
self.assertRaises(IOError, GCMRouter, {}, {"senderIDs": fsenderids})
settings = AutopushSettings(
hostname="localhost",
statsd_host=None,
)
self.assertRaises(IOError, GCMRouter, settings,
{"senderIDs": fsenderids})

def test_register(self):
result = self.router.register("uaid", {"token": "connect_data"})
Expand Down

0 comments on commit 126203b

Please sign in to comment.