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 flask and some refactor
  • Loading branch information
liyanhui1228 authored and dhermes committed Jun 2, 2017
commit 8fd8f316fa29d26eb097642a94d1905db5373745
12 changes: 7 additions & 5 deletions logging/google/cloud/logging/handlers/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
_FLASK_TRACE_HEADER = 'X_CLOUD_TRACE_CONTEXT'
_DJANGO_TRACE_HEADER = 'HTTP_X_CLOUD_TRACE_CONTEXT'

_EMPTY_TRACE_ID = 'None'

This comment was marked as spam.

This comment was marked as spam.



def format_stackdriver_json(record, message):
"""Helper to format a LogRecord in in Stackdriver fluentd format.
Expand Down Expand Up @@ -56,12 +58,12 @@ def get_trace_id_from_flask():
:return: Trace_id in HTTP request headers.
"""
if not flask or not flask.request:

This comment was marked as spam.

This comment was marked as spam.

return None
return _EMPTY_TRACE_ID

header = flask.request.headers.get(_FLASK_TRACE_HEADER)

if not header:

This comment was marked as spam.

This comment was marked as spam.

return None
return _EMPTY_TRACE_ID

trace_id = header.split('/')[0]

This comment was marked as spam.

This comment was marked as spam.


Expand All @@ -78,12 +80,12 @@ def get_trace_id_from_django():
request = request_middleware.get_request()

if not request:

This comment was marked as spam.

This comment was marked as spam.

return None
return _EMPTY_TRACE_ID

try:
header = request.META[_DJANGO_TRACE_HEADER]

This comment was marked as spam.

This comment was marked as spam.

except KeyError:
return None
return _EMPTY_TRACE_ID

trace_id = header.split('/')[0]

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.


Expand All @@ -100,7 +102,7 @@ def get_trace_id():

for checker in checkers:
trace_id = checker()
if trace_id is not None:
if trace_id is not _EMPTY_TRACE_ID:
return trace_id

return trace_id

This comment was marked as spam.

This comment was marked as spam.

2 changes: 0 additions & 2 deletions logging/google/cloud/logging/handlers/app_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@ def get_gae_labels(self):
:returns: Labels for GAE app.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

"""
trace_id = get_trace_id()
if trace_id is None:
trace_id = 'unknown'
gae_labels = {

This comment was marked as spam.

This comment was marked as spam.

'appengine.googleapis.com/trace_id': trace_id,

This comment was marked as spam.

This comment was marked as spam.

}
Expand Down
2 changes: 1 addition & 1 deletion logging/nox.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def unit_tests(session, python_version):
session.interpreter = 'python{}'.format(python_version)

# Install all test dependencies, then install this package in-place.
session.install('mock', 'pytest', 'pytest-cov', *LOCAL_DEPS)
session.install('mock', 'pytest', 'pytest-cov', 'flask', *LOCAL_DEPS)
session.install('-e', '.')

# Run py.test against the unit tests.
Expand Down
57 changes: 57 additions & 0 deletions logging/tests/unit/handlers/test__helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# 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 TestFlaskTrace(unittest.TestCase):

This comment was marked as spam.

This comment was marked as spam.


def create_app(self):

This comment was marked as spam.

This comment was marked as spam.

import flask

app = flask.Flask(__name__)

@app.route('/')
def index():
return 'test flask trace'

return app

def setUp(self):
self.app = self.create_app()

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.


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 _EMPTY_TRACE_ID

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

self.assertEqual(trace_id, _EMPTY_TRACE_ID)

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

FLASK_TRACE_HEADER = 'X_CLOUD_TRACE_CONTEXT'

This comment was marked as spam.

This comment was marked as spam.

FLASK_TRACE_ID = 'testtraceid/testspanid'

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

EXPECTED_TRACE_ID = 'testtraceid'
self.assertEqual(trace_id, EXPECTED_TRACE_ID)
3 changes: 2 additions & 1 deletion logging/tests/unit/handlers/test_app_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def test_constructor(self):
from google.cloud.logging.handlers.app_engine import _GAE_PROJECT_ENV
from google.cloud.logging.handlers.app_engine import _GAE_SERVICE_ENV
from google.cloud.logging.handlers.app_engine import _GAE_VERSION_ENV
from google.cloud.logging.handlers._helpers import _EMPTY_TRACE_ID

TRACE_ID_LABEL = 'appengine.googleapis.com/trace_id'

Expand All @@ -46,7 +47,7 @@ def test_constructor(self):
self.assertEqual(handler.resource.labels['project_id'], 'test_project')
self.assertEqual(handler.resource.labels['module_id'], 'test_service')
self.assertEqual(handler.resource.labels['version_id'], 'test_version')
self.assertEqual(handler.labels[TRACE_ID_LABEL], 'unknown')
self.assertEqual(handler.labels[TRACE_ID_LABEL], _EMPTY_TRACE_ID)

def test_emit(self):
import mock
Expand Down