Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
9a20be9
Send trace context with logs from web applications
liyanhui1228 May 22, 2017
cf283c4
Fix style
liyanhui1228 May 22, 2017
583d076
Improved code for web framework detection
liyanhui1228 May 23, 2017
5be368e
Fix year
liyanhui1228 May 23, 2017
28e3a6c
Drop module level variables
liyanhui1228 May 23, 2017
cdc96b4
Address Jon's comments and add some unit tests (not complete yet)
liyanhui1228 May 25, 2017
fb83ab6
Fix stuff
liyanhui1228 May 25, 2017
9a20c4e
Add unit test for flask and some refactor
liyanhui1228 May 25, 2017
f8b7e65
Add unit test for middleware
liyanhui1228 May 26, 2017
65efdcd
Add unit test for django
liyanhui1228 May 26, 2017
f78cc2e
Fix style
liyanhui1228 May 26, 2017
33b92dd
Address jon's comments
liyanhui1228 May 26, 2017
5cc798f
Address jon's comments
liyanhui1228 May 30, 2017
6ef3f49
Address all comments
liyanhui1228 Jun 2, 2017
14c2545
fix style
liyanhui1228 Jun 2, 2017
199ada0
Send trace context with logs from web applications
liyanhui1228 May 22, 2017
425c8ef
Fix style
liyanhui1228 May 22, 2017
1047fd5
Improved code for web framework detection
liyanhui1228 May 23, 2017
46012eb
Fix year
liyanhui1228 May 23, 2017
43148bf
Drop module level variables
liyanhui1228 May 23, 2017
de02a1d
Address Jon's comments and add some unit tests (not complete yet)
liyanhui1228 May 25, 2017
ffb57c4
Fix stuff
liyanhui1228 May 25, 2017
8fd8f31
Add unit test for flask and some refactor
liyanhui1228 May 25, 2017
4480027
Add unit test for middleware
liyanhui1228 May 26, 2017
ad00bdb
Add unit test for django
liyanhui1228 May 26, 2017
8ed81ad
Fix style
liyanhui1228 May 26, 2017
a6b25bb
Address jon's comments
liyanhui1228 May 26, 2017
6123764
Address jon's comments
liyanhui1228 May 30, 2017
3caccc7
Address all comments
liyanhui1228 Jun 2, 2017
4589a06
fix style
liyanhui1228 Jun 2, 2017
2889cfc
Tweaks for style / coverage / lint.
dhermes Jun 2, 2017
53738d7
Merge branch 'yanhuil/trace_id' of https://github.com/GoogleCloudPlat…
liyanhui1228 Jun 2, 2017
1957077
Send trace context with logs from web applications
liyanhui1228 May 22, 2017
bf683f7
Fix style
liyanhui1228 May 22, 2017
51cdb39
Improved code for web framework detection
liyanhui1228 May 23, 2017
2479d91
Fix year
liyanhui1228 May 23, 2017
6c04196
Drop module level variables
liyanhui1228 May 23, 2017
f70c84e
Address Jon's comments and add some unit tests (not complete yet)
liyanhui1228 May 25, 2017
6fc4b21
Fix stuff
liyanhui1228 May 25, 2017
2106bfc
Add unit test for flask and some refactor
liyanhui1228 May 25, 2017
c66fc31
Add unit test for middleware
liyanhui1228 May 26, 2017
035c4a7
Add unit test for django
liyanhui1228 May 26, 2017
bd8c1b4
Fix style
liyanhui1228 May 26, 2017
51bf193
Address jon's comments
liyanhui1228 May 26, 2017
6b90d1e
Address jon's comments
liyanhui1228 May 30, 2017
0c6c4f6
Address all comments
liyanhui1228 Jun 2, 2017
fe8a3d5
fix style
liyanhui1228 Jun 2, 2017
4dff904
Tweaks for style / coverage / lint.
dhermes Jun 2, 2017
9bac365
Merge branch 'yanhuil/trace_id' of https://github.com/GoogleCloudPlat…
liyanhui1228 Jun 2, 2017
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
Prev Previous commit
Next Next commit
Improved code for web framework detection
  • Loading branch information
liyanhui1228 committed May 23, 2017
commit 583d076f53d1c10eba3633e24c83a00e4e612598
37 changes: 27 additions & 10 deletions logging/google/cloud/logging/handlers/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,18 @@

import math
import json

try:
import django
except ImportError:
_USE_DJANGO = False
else:
_USE_DJANGO = True
import sys

try:
import flask
except ImportError:
_USE_FLASK = False
flask = None
else:
_USE_FLASK = True

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

from google.cloud.logging.handlers.middleware.request import get_request

_USE_FLASK = False
_USE_DJANGO = False


def format_stackdriver_json(record, message):
"""Helper to format a LogRecord in in Stackdriver fluentd format.
Expand All @@ -56,12 +50,35 @@ def format_stackdriver_json(record, message):
return json.dumps(payload)


def detect_web_framework():

This comment was marked as spam.

This comment was marked as spam.

"""Detect web framework used in this environment.

This comment was marked as spam.

This comment was marked as spam.


Detect which web framework is used by looking at the sys.modules.
If multiple or no supported web frameworks detected in the modules, then
print a warning message.
Set _USE_FLASK or _USE_DJANGO to True if one of them are detected.
"""
modules = sys.modules
global _USE_FLASK, _USE_DJANGO

if 'flask' in modules and 'django' in modules:
print('Cannot determine, found multiple web frameworks.')
elif 'flask' in modules:
_USE_FLASK = True
elif 'django' in modules:
_USE_DJANGO = True
else:
print('No supported web framework found in the modules.')


def get_trace_id_from_request_header():
"""Helper to get trace_id from web application request header.

:rtype: str
:returns: Trace_id in HTTP request headers.
"""
detect_web_framework()

if _USE_FLASK:
try:
trace_id = flask.request.headers['X_CLOUD_TRACE_CONTEXT'].split('/')[0]
Expand Down