Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1.8k
Add support for logging the trace-id in webapp2 apps.#3593
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -22,11 +22,21 @@ | ||
| except ImportError: # pragma: NO COVER | ||
| flask = None | ||
| try: | ||
| import webapp2 | ||
| except (ImportError, SyntaxError): # pragma: NO COVER | ||
| # If you try to import webapp2 under python3, you'll get a syntax | ||
| # error (since it hasn't been ported yet). We just pretend it | ||
| # doesn't exist. This is unlikely to hit in real life but does | ||
| # in the tests. | ||
| webapp2 = None | ||
| from google.cloud.logging.handlers.middleware.request import ( | ||
| _get_django_request) | ||
| _FLASK_TRACE_HEADER = 'X_CLOUD_TRACE_CONTEXT' | ||
| _DJANGO_TRACE_HEADER = 'HTTP_X_CLOUD_TRACE_CONTEXT' | ||
| _FLASK_TRACE_HEADER = 'X_CLOUD_TRACE_CONTEXT' | ||
| _WEBAPP2_TRACE_HEADER = 'X-CLOUD-TRACE-CONTEXT' | ||
| def format_stackdriver_json(record, message): | ||
| @@ -54,7 +64,7 @@ def get_trace_id_from_flask(): | ||
| """Get trace_id from flask request headers. | ||
| :rtype: str | ||
| :return: Trace_id in HTTP request headers. | ||
| :returns: TraceID in HTTP request headers. | ||
| """ | ||
| if flask is None or not flask.request: | ||
| return None | ||
| @@ -69,11 +79,38 @@ def get_trace_id_from_flask(): | ||
| return trace_id | ||
| def get_trace_id_from_webapp2(): | ||
| """Get trace_id from webapp2 request headers. | ||
| :rtype: str | ||
| :returns: TraceID in HTTP request headers. | ||
| """ | ||
| if webapp2 is None: | ||
| return None | ||
| try: | ||
| # get_request() succeeds if we're in the middle of a webapp2 | ||
| # request, or raises an assertion error otherwise: | ||
| # "Request global variable is not set". | ||
This comment was marked as spam.Sorry, something went wrong. Uh oh!There was an error while loading. Please reload this page.
This comment was marked as spam.Sorry, something went wrong. Uh oh!There was an error while loading. Please reload this page. | ||
| req = webapp2.get_request() | ||
| except AssertionError: | ||
| return None | ||
| header = req.headers.get(_WEBAPP2_TRACE_HEADER) | ||
| if header is None: | ||
| return None | ||
| trace_id = header.split('/', 1)[0] | ||
This comment was marked as spam.Sorry, something went wrong. Uh oh!There was an error while loading. Please reload this page.
This comment was marked as spam.Sorry, something went wrong. Uh oh!There was an error while loading. Please reload this page. | ||
| return trace_id | ||
| def get_trace_id_from_django(): | ||
| """Get trace_id from django request headers. | ||
| :rtype: str | ||
| :return: Trace_id in HTTP request headers. | ||
| :returns: TraceID in HTTP request headers. | ||
| """ | ||
| request = _get_django_request() | ||
| @@ -93,9 +130,11 @@ def get_trace_id(): | ||
| """Helper to get trace_id from web application request header. | ||
| :rtype: str | ||
| :returns: Trace_id in HTTP request headers. | ||
| :returns: TraceID in HTTP request headers. | ||
| """ | ||
| checkers = (get_trace_id_from_django, get_trace_id_from_flask) | ||
| checkers = (get_trace_id_from_django, | ||
| get_trace_id_from_flask, | ||
| get_trace_id_from_webapp2) | ||
| for checker in checkers: | ||
| trace_id = checker() | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -36,7 +36,7 @@ def unit_tests(session, python_version): | ||
| # Install all test dependencies, then install this package in-place. | ||
| session.install( | ||
| 'mock', 'pytest', 'pytest-cov', | ||
| 'flask', 'django', *LOCAL_DEPS) | ||
| 'flask', 'webapp2', 'webob', 'django', *LOCAL_DEPS) | ||
This comment was marked as spam.Sorry, something went wrong. Uh oh!There was an error while loading. Please reload this page.
This comment was marked as spam.Sorry, something went wrong. Uh oh!There was an error while loading. Please reload this page. | ||
| session.install('-e', '.') | ||
| # Run py.test against the unit tests. | ||
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.