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

Send trace context with logs from web applications #3448

Merged
merged 49 commits into from
Jun 2, 2017
Merged
Show file tree
Hide file tree
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
Add unit test for django
  • Loading branch information
liyanhui1228 committed May 26, 2017
commit 65efdcd79fbcae278c0ad925c367d902b408f9ab
15 changes: 12 additions & 3 deletions logging/tests/unit/handlers/middleware/test_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,24 @@ def _get_target_class(self):
def _make_one(self, *args, **kw):
return self._get_target_class()(*args, **kw)

def test_get_request(self):
def setUp(self):
from django.conf import settings
from django.test.utils import setup_test_environment
from django.test import RequestFactory

settings.configure()
if not settings.configured:
settings.configure()
setup_test_environment()

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.


def test_get_request(self):

This comment was marked as spam.

This comment was marked as spam.

from django.test import RequestFactory

self.middleware = self._make_one()
self.request = RequestFactory().get('/')

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

self.middleware.process_request(self.request)
self.assertEqual(self.middleware.get_request(), self.request)


def tearDown(self):

This comment was marked as spam.

This comment was marked as spam.

from django.test.utils import teardown_test_environment

teardown_test_environment()
67 changes: 65 additions & 2 deletions logging/tests/unit/handlers/test__helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,88 @@ def setUp(self):

def test_trace_id_no_context_header(self):
from google.cloud.logging.handlers._helpers import get_trace_id_from_flask

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

from google.cloud.logging.handlers._helpers import get_trace_id
from google.cloud.logging.handlers._helpers import _EMPTY_TRACE_ID

with self.app.test_request_context(
path='/',
headers={}):
trace_id = get_trace_id_from_flask()
trace_id_returned = get_trace_id()

self.assertEqual(trace_id, _EMPTY_TRACE_ID)
self.assertEqual(trace_id_returned, _EMPTY_TRACE_ID)

def test_trace_id_valid_context_header(self):
from google.cloud.logging.handlers._helpers import get_trace_id_from_flask
from google.cloud.logging.handlers._helpers import get_trace_id

FLASK_TRACE_HEADER = 'X_CLOUD_TRACE_CONTEXT'

This comment was marked as spam.

This comment was marked as spam.

FLASK_TRACE_ID = 'testtraceid/testspanid'
FLASK_TRACE_ID = 'testtraceidflask/testspanid'

with self.app.test_request_context(
path='/',
headers={FLASK_TRACE_HEADER:FLASK_TRACE_ID}):
trace_id = get_trace_id_from_flask()
trace_id_returned = get_trace_id()

EXPECTED_TRACE_ID = 'testtraceid'
EXPECTED_TRACE_ID = 'testtraceidflask'
self.assertEqual(trace_id, EXPECTED_TRACE_ID)
self.assertEqual(trace_id, trace_id_returned)


class TestDjangoTrace(unittest.TestCase):

This comment was marked as spam.

This comment was marked as spam.


def setUp(self):
from django.conf import settings
from django.test.utils import setup_test_environment

if not settings.configured:
settings.configure()
setup_test_environment()

def test_trace_id_no_context_header(self):
import mock
from django.test import RequestFactory
from google.cloud.logging.handlers._helpers import get_trace_id_from_django

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

from google.cloud.logging.handlers._helpers import get_trace_id
from google.cloud.logging.handlers._helpers import _EMPTY_TRACE_ID

request = RequestFactory().get('/')

with mock.patch(
'google.cloud.logging.handlers.middleware.RequestMiddleware.get_request',
return_value=request):
trace_id = get_trace_id_from_django()
trace_id_returned = get_trace_id()

self.assertEqual(trace_id, _EMPTY_TRACE_ID)
self.assertEqual(trace_id_returned, _EMPTY_TRACE_ID)

def test_trace_id_valid_context_header(self):
import mock
from django.test import RequestFactory
from google.cloud.logging.handlers._helpers import get_trace_id_from_django
from google.cloud.logging.handlers._helpers import get_trace_id

DJANGO_TRACE_HEADER = 'HTTP_X_CLOUD_TRACE_CONTEXT'
DJANGO_TRACE_ID = 'testtraceiddjango/testspanid'

request = RequestFactory().get(
'/',
**{DJANGO_TRACE_HEADER:DJANGO_TRACE_ID})

with mock.patch(
'google.cloud.logging.handlers.middleware.RequestMiddleware.get_request',
return_value=request):
trace_id = get_trace_id_from_django()
trace_id_returned = get_trace_id()

EXPECTED_TRACE_ID = 'testtraceiddjango'
self.assertEqual(trace_id, EXPECTED_TRACE_ID)
self.assertEqual(trace_id, trace_id_returned)

def tearDown(self):
from django.test.utils import teardown_test_environment

teardown_test_environment()