Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor wsgi request logging, for more context #492

Merged
merged 1 commit into from Dec 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 3 additions & 7 deletions talisker/gunicorn.py
Expand Up @@ -116,13 +116,9 @@ def gunicorn_child_exit(server, worker):
def gunicorn_worker_exit(server, worker):
"""Logs any requests that are still in flight."""
now = time.time()
for env in talisker.wsgi.REQUESTS.values():
duration = now - env['start_time']
talisker.wsgi.log_response(
env,
duration=duration,
timeout=True,
)
for wsgi_request in talisker.wsgi.REQUESTS.values():
duration = now - wsgi_request.environ['start_time']
wsgi_request.log(duration, timeout=True)


class GunicornLogger(Logger):
Expand Down
9 changes: 5 additions & 4 deletions talisker/sentry.py
Expand Up @@ -53,7 +53,7 @@
'configure_testing',
'get_client',
'record_breadcrumb',
'report_wsgi_error',
'report_wsgi',
'set_client',
]

Expand Down Expand Up @@ -81,7 +81,7 @@ def record_breadcrumb(*args, **kwargs):
def record_log_breadcrumb(record):
pass

def report_wsgi_error(environ, msg=None, **kwargs):
def report_wsgi(environ, msg=None, **kwargs):
return

class TestSentryContext():
Expand Down Expand Up @@ -155,12 +155,13 @@ def processor(data):
})
raven.breadcrumbs.record(processor=processor)

def report_wsgi_error(environ, msg=None, **kwargs):
def report_wsgi(environ, msg=None, response_data=None, **kwargs):
"""Use raven to report error"""
sentry = get_client()
# reuse code from Sentry middleware, if a bit unpleasently
mw = raven.middleware.Sentry(None, sentry)
sentry.http_context(mw.get_http_context(environ))
http_context = mw.get_http_context(environ)
sentry.http_context(http_context)
if msg is None:
return sentry.captureException(**kwargs)
else:
Expand Down
2 changes: 1 addition & 1 deletion talisker/testing.py
Expand Up @@ -283,11 +283,11 @@ def __init__(self, name=None):
self.sentry_context = talisker.sentry.TestSentryContext(self.dsn)

def start(self):
Context.new()
self.old_statsd = talisker.statsd.get_client.raw_update(
self.statsd_client)
talisker.logs.add_talisker_handler(logging.NOTSET, self.handler)
self.sentry_context.start()
Context.new()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FTR - this is a driveby UX improvement - the previous lines generates logs, moving the new context to here means those logs are not captured as part of the context.

def stop(self):
logging.getLogger().handlers.remove(self.handler)
Expand Down