-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Changes from 15 commits
9a20be9
cf283c4
583d076
5be368e
28e3a6c
cdc96b4
fb83ab6
9a20c4e
f8b7e65
65efdcd
f78cc2e
33b92dd
5cc798f
6ef3f49
14c2545
199ada0
425c8ef
1047fd5
46012eb
43148bf
de02a1d
ffb57c4
8fd8f31
4480027
ad00bdb
8ed81ad
a6b25bb
6123764
3caccc7
4589a06
2889cfc
53738d7
1957077
bf683f7
51cdb39
2479d91
6c04196
f70c84e
6fc4b21
2106bfc
c66fc31
035c4a7
bd8c1b4
51bf193
6b90d1e
0c6c4f6
fe8a3d5
4dff904
9bac365
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Copyright 2017 Google Inc. All Rights Reserved. | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from google.cloud.logging.handlers.middleware.request import RequestMiddleware | ||
|
||
__all__ = ['RequestMiddleware'] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# Copyright 2017 Google Inc. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import threading | ||
|
||
|
||
_thread_locals = threading.local() | ||
|
||
|
||
def _get_django_request(): | ||
"""Get Django request from thread local. | ||
|
||
:rtype: str | ||
:returns: Django request. | ||
""" | ||
return getattr(_thread_locals, 'request', None) | ||
|
||
|
||
class RequestMiddleware(object): | ||
"""Saves the request in thread local""" | ||
|
||
def process_request(self, request): | ||
"""Called on each request, before Django decides which view to execute. | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
|
||
:type request: :class:`~django.http.request.HttpRequest` | ||
:param request: Django http request. | ||
""" | ||
_thread_locals.request = request | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# Copyright 2017 Google Inc. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import unittest | ||
|
||
|
||
class TestRequestMiddleware(unittest.TestCase): | ||
|
||
def _get_target_class(self): | ||
from google.cloud.logging.handlers.middleware.request import RequestMiddleware | ||
|
||
return RequestMiddleware | ||
|
||
def _make_one(self, *args, **kw): | ||
return self._get_target_class()(*args, **kw) | ||
|
||
@classmethod | ||
def setUpClass(cls): | ||
from django.conf import settings | ||
from django.test.utils import setup_test_environment | ||
|
||
if not settings.configured: | ||
settings.configure() | ||
setup_test_environment() | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
|
||
@classmethod | ||
def tearDownClass(cls): | ||
from django.test.utils import teardown_test_environment | ||
|
||
teardown_test_environment() | ||
|
||
def test_get_django_request(self): | ||
from django.test import RequestFactory | ||
from google.cloud.logging.handlers.middleware.request import _get_django_request | ||
|
||
middleware = self._make_one() | ||
request = RequestFactory().get('/') | ||
middleware.process_request(request) | ||
self.assertEqual(_get_django_request(), request) |
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.