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

Commit

Permalink
fix: add integration testing and verification of sentry reporting
Browse files Browse the repository at this point in the history
Closes #493
  • Loading branch information
bbangert committed Jul 13, 2016
1 parent bfc4f16 commit ee4e339
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 16 deletions.
17 changes: 11 additions & 6 deletions autopush/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import boto3
import raven
from raven.transport.twisted import TwistedHTTPTransport
from twisted.internet import reactor
from twisted.logger import (
formatEvent,
Expand All @@ -29,6 +30,7 @@
"failure",
"format",
"isError",
"log_failure",
"log_format",
"log_flattened",
"log_level",
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down
37 changes: 27 additions & 10 deletions autopush/tests/test_logging.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import json
import os

import cyclone.web
import twisted.internet
import twisted.trial.unittest

Expand All @@ -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)
Expand Down

0 comments on commit ee4e339

Please sign in to comment.