diff --git a/autopush/logging.py b/autopush/logging.py index 545679c9..8f2f15f1 100644 --- a/autopush/logging.py +++ b/autopush/logging.py @@ -11,6 +11,7 @@ import boto3 import raven +from raven.transport.twisted import TwistedHTTPTransport from twisted.internet import reactor from twisted.logger import ( formatEvent, @@ -29,6 +30,7 @@ "failure", "format", "isError", + "log_failure", "log_format", "log_flattened", "log_level", @@ -74,7 +76,9 @@ def __init__(self, logger_name, log_level="debug", log_format="json", self.format_event = formatEventAsClassicLogText if sentry_dsn: self.raven_client = raven.Client( - release=raven.fetch_package_version("autopush")) + release=raven.fetch_package_version("autopush"), + transport=TwistedHTTPTransport, + ) else: self.raven_client = None if firehose_delivery_stream: @@ -84,16 +88,17 @@ def __init__(self, logger_name, log_level="debug", log_format="json", self.firehose = None def __call__(self, event): - if event["log_level"] < self.log_level: - return - - if self.raven_client and 'failure' in event: - f = event["failure"] + if self.raven_client and ('failure' in event or + 'log_failure' in event): + f = event.get("log_failure", event["failure"]) reactor.callFromThread( self.raven_client.captureException, exc_info=(f.type, f.value, f.getTracebackObject()) ) + if event["log_level"] < self.log_level: + return + text = self.format_event(event) if self.firehose: diff --git a/autopush/tests/test_logging.py b/autopush/tests/test_logging.py index 3a8a81d1..701d006c 100644 --- a/autopush/tests/test_logging.py +++ b/autopush/tests/test_logging.py @@ -1,5 +1,7 @@ +import json import os +import cyclone.web import twisted.internet import twisted.trial.unittest @@ -15,29 +17,44 @@ log = Logger() +class LocalSentryChomper(cyclone.web.RequestHandler): + def post(self): + self.logged.append(json.loads(self.request.body.decode("zlib"))) + return "" + + class SentryLogTestCase(twisted.trial.unittest.TestCase): def setUp(self): + from autopush.main import skip_request_logging twisted.internet.base.DelayedCall.debug = True - raven_patcher = patch("autopush.logging.raven") - self.mock_raven = raven_patcher.start() - self.mock_client = Mock() - self.mock_raven.Client.return_value = self.mock_client + sentry = LocalSentryChomper + sentry.logged = [] + site = cyclone.web.Application([ + (r"/.*", sentry), + ], + log_function=skip_request_logging + ) + self.sentry = sentry + self._site = site + self._port = reactor.listenTCP(9999, site) def tearDown(self): - self.mock_raven.stop() + reactor.removeAll() def test_sentry_logging(self): - os.environ["SENTRY_DSN"] = "some_locale" - PushLogger.setup_logging("Autopush", sentry_dsn=True) - eq_(len(self.mock_raven.mock_calls), 2) + dsn = "http://PUBKEY:SECKEY@localhost:9999/1" + os.environ["SENTRY_DSN"] = dsn + pl = PushLogger.setup_logging("Autopush", sentry_dsn=True) log.failure("error", failure.Failure(Exception("eek"))) self.flushLoggedErrors() d = Deferred() def check(): - if len(self.mock_client.mock_calls): - eq_(len(self.mock_client.mock_calls), 1) + if len(self.sentry.logged): + eq_(len(self.sentry.logged), 1) + self._port.stopListening() + pl.stop() d.callback(True) else: # pragma: nocover reactor.callLater(0, check)