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

[Fix niteoweb/pyramid_heroku#22] Add request_id... #38

Open
wants to merge 3 commits into
base: main
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
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ structlog = "*"
black = "==18.9b0"
mypy = "*"
typecov = "*"
sentry-sdk = "*"

[requires]
python_version = "3.7"
22 changes: 15 additions & 7 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 60 additions & 0 deletions pyramid_heroku/request_id.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
"""Set request ID in Sentry logs if sentry is imported, and in structlog logs
if structlog is imported.
"""

IS_SENTRY_INSTALLED: bool
IS_STRUCTLOG_INSTALLED: bool

try:
import sentry_sdk

IS_SENTRY_INSTALLED = True
except ImportError:
IS_SENTRY_INSTALLED = False

try:
import structlog

IS_STRUCTLOG_INSTALLED = True
except ImportError:
IS_STRUCTLOG_INSTALLED = False


def includeme(config):
config.add_tween("pyramid_heroku.request_id.RequestIDLogger")


class RequestIDLogger(object):
"""Set request ID in sentry and structlog logs.

If the request headers contain a request ID (X-Request-ID), it should be
logged for better debugging. In case sentry is being used, log the request
ID along with other info. Similarly, if structlog is being used, add the
request ID in the logs.
"""

def __init__(self, handler, _):
self.handler = handler

def __call__(self, request):
request_id = request.headers.get("X-Request-ID")

if request_id is None:
return self.handler(request)

if IS_SENTRY_INSTALLED:
self.set_sentry_scope(request_id)

if IS_STRUCTLOG_INSTALLED:
self.set_structlog_context(request_id)

return self.handler(request)

def set_sentry_scope(self, request_id: int) -> None:
with sentry_sdk.configure_scope() as scope:
scope.set_tag("request_id", request_id)

def set_structlog_context(self, request_id: int) -> None:
WrappedDictClass = structlog.threadlocal.wrap_dict(dict)
_ = WrappedDictClass({"request_id": request_id})
structlog.configure(context_class=WrappedDictClass)
1 change: 1 addition & 0 deletions pyramid_heroku/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

120 changes: 120 additions & 0 deletions pyramid_heroku/tests/test_request_id.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
"""Tests for RequestIDLogger tween"""

from pyramid import request
from pyramid import testing

from importlib import reload
import unittest
from unittest import mock
import structlog
import sentry_sdk
import sys


class TestRequestIDLogger(unittest.TestCase):
def setUp(self):
self.config = testing.setUp()
self.request = request.Request({})

self.handler = mock.Mock()
self.registry = None

self.request_id = "some-random-requestid"

self.some_random_dsn = "https://[email protected]/12345"

def tearDown(self):
testing.tearDown()

def test_request_id_in_sentry(self):
sentry_sdk.Hub.current._stack[-1][1].clear()
sentry_sdk.init(self.some_random_dsn)
from pyramid_heroku.request_id import RequestIDLogger

self.request.headers["X-Request-ID"] = self.request_id
RequestIDLogger(self.handler, self.registry)(self.request)
self.handler.assert_called_with(self.request)
self.assertEqual(
sentry_sdk.Hub.current._stack[-1][1]._tags, {"request_id": self.request_id}
)

def test_request_id_in_structlog(self):
structlog.reset_defaults()
from pyramid_heroku.request_id import RequestIDLogger

context_class = structlog.get_config().get("context_class")
self.assertEqual(context_class, dict)

self.request.headers["X-Request-ID"] = self.request_id
RequestIDLogger(self.handler, self.registry)(self.request)
self.handler.assert_called_with(self.request)

context_class = structlog.get_config().get("context_class")
self.assertEqual(context_class._tl.dict_, {"request_id": self.request_id})

def test_request_id_not_in_header_sentry(self):
sentry_sdk.Hub.current._stack[-1][1].clear()
sentry_sdk.init(self.some_random_dsn)
from pyramid_heroku.request_id import RequestIDLogger

RequestIDLogger(self.handler, self.registry)(self.request)
self.handler.assert_called_with(self.request)
self.assertEqual(sentry_sdk.Hub.current._stack[-1][1]._tags, {})

def test_request_id_not_in_header_structlog(self):
structlog.reset_defaults()
from pyramid_heroku.request_id import RequestIDLogger

context_class = structlog.get_config().get("context_class")
self.assertEqual(context_class, dict)

RequestIDLogger(self.handler, self.registry)(self.request)
self.handler.assert_called_with(self.request)

context_class = structlog.get_config().get("context_class")
self.assertEqual(context_class, dict)

def test_smooth_run_without_sentry(self):

structlog.reset_defaults()

# remove sentry_sdk from modules to disable its import,
# in order to cover the code that runs without sentry
from pyramid_heroku import request_id

sys.modules["sentry_sdk"] = None
reload(request_id)

self.request.headers["X-Request-ID"] = self.request_id
request_id.RequestIDLogger(self.handler, self.registry)(self.request)
self.handler.assert_called_with(self.request)

# structlog is still there
context_class = structlog.get_config().get("context_class")
self.assertEqual(context_class._tl.dict_, {"request_id": self.request_id})

# revert sys.modules to its original state
sys.modules["sentry_sdk"] = sentry_sdk

def test_smooth_run_without_structlog(self):
sentry_sdk.Hub.current._stack[-1][1].clear()
sentry_sdk.init(self.some_random_dsn)

# remove structlog from modules to disable its import,
# in order to cover the code that runs without structlog
from pyramid_heroku import request_id

sys.modules["structlog"] = None
reload(request_id)

from pyramid_heroku import request_id

self.request.headers["X-Request-ID"] = self.request_id
request_id.RequestIDLogger(self.handler, self.registry)(self.request)
self.handler.assert_called_with(self.request)
self.assertEqual(
sentry_sdk.Hub.current._stack[-1][1]._tags, {"request_id": self.request_id}
)

# revert sys.modules to its original state
sys.modules["structlog"] = structlog