Uh oh!
There was an error while loading. Please reload this page.
Add support for logging the trace-id in webapp2 apps. - #3593
Conversation
This extends that to also support webapp2. webapp2 is not cutting edge technology anymore -- it doesn't even work on python3 -- but a lot of legacy GAE classic apps use webapp2, so I think it makes sense to support it. I tested this by running `nox` in the logging/ directory, which as far as I can tell is current state of the art for running tests.
googlebot
commented
Jul 7, 2017
Thanks for your pull request. It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). 📝 Please visit https://cla.developers.google.com/ to sign. Once you've signed, please reply here (e.g.
|
csilvers
commented
Jul 7, 2017
Our company (Khan Academy) has signed the CLA. I've been added to the group of authorized contributors. Maybe I need to be added under my github.com email and not my company email. I've done that and will try again. |
csilvers
commented
Jul 7, 2017
(I made a new comment but it doesn't look like the clabot has run again. I'm not sure what else I need to do.) |
duggelz
commented
Jul 7, 2017
@lukesneeringer How do we poke the clabot? (Craig is/was Google employee |
duggelz
commented
Jul 7, 2017
The clabot doesn't think that Khan Academy has signed the corporate CLA (unless it has some wierd legal name that doesn't include the words Khan or Academy), nor does it think that csilvers (GitHub username) or csilvers@khanacademy.org has signed the individual CLA. The docs says that corporate CLAs might take a few days to process, was it signed recently? |
csilvers
commented
Jul 7, 2017
Ah, that sounds like it must be it. I just signed the corporate CLA earlier today. I'm happy to wait (though ideally it wouldn't need to block review!) |
csilvers
commented
Jul 7, 2017
btw, hi doug! |
duggelz
commented
Jul 7, 2017
Hi Craig! :) |
| # 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. |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
liyanhui1228
left a comment
There was a problem hiding this comment.
Thanks for adding the support for webapp2. Did you test it by actually running a webapp2 application on GAE? If the log aggregation works then LGTM with the minor comments.
| _get_django_request) | ||
| _FLASK_TRACE_HEADER = 'X_CLOUD_TRACE_CONTEXT' | ||
| _WEBAPP2_TRACE_HEADER = 'x-cloud-trace-context' |
This comment was marked as spam.
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.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
| """Get trace_id from webapp2 request headers. | ||
| :rtype: str | ||
| :return: Trace_id in HTTP request headers. |
This comment was marked as spam.
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.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
| 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.
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.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
| """Get trace_id from webapp2 request headers. | ||
| :rtype: str | ||
| :return: Trace_id in HTTP request headers. |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
| """ | ||
| 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) |
This comment was marked as spam.
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.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
| if header is None: | ||
| return None | ||
| trace_id = header.split('/', 1)[0] |
This comment was marked as spam.
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.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
| session.install( | ||
| 'mock', 'pytest', 'pytest-cov', | ||
| 'flask', 'django', *LOCAL_DEPS) | ||
| 'flask', 'webapp2', 'webob', 'django', *LOCAL_DEPS) |
This comment was marked as spam.
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.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
| return app | ||
| def setUp(self): | ||
| self.app = self.create_app() |
This comment was marked as spam.
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.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
| def create_app(): | ||
| import webapp2 | ||
| class GetTraceId(webapp2.RequestHandler): |
This comment was marked as spam.
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.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
| from google.cloud.logging.handlers import _helpers | ||
| trace_id = _helpers.get_trace_id_from_webapp2() | ||
| self.response.content_type = "text/plain" |
This comment was marked as spam.
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.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
| response = req.get_response(self.app) | ||
| trace_id = response.body | ||
| self.assertEquals("None", trace_id) |
This comment was marked as spam.
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.
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.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| import sys |
This comment was marked as spam.
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.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
csilvers
left a comment
There was a problem hiding this comment.
`Thanks everyone for the super-quick feedback! I don't think I'll be able to get to the fixes tonight (though we'll see), but will do them as soon as I can.
Also, @liyanhui1228, to answer your original question: I haven't tried this in real life, just via the tests. Honestly, I'm not even sure how to test it in real life (we're just starting to play around with this library but know we'll need webapp2 support for our apps). I'm glad to try it though if I can figure out how!
| _get_django_request) | ||
| _FLASK_TRACE_HEADER = 'X_CLOUD_TRACE_CONTEXT' | ||
| _WEBAPP2_TRACE_HEADER = 'x-cloud-trace-context' |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
| """Get trace_id from webapp2 request headers. | ||
| :rtype: str | ||
| :return: Trace_id in HTTP request headers. |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
| 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.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
| if header is None: | ||
| return None | ||
| trace_id = header.split('/', 1)[0] |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
| """ | ||
| 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) |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
| class Test_get_trace_id_from_webapp2(unittest.TestCase): | ||
| @staticmethod | ||
| def create_app(): |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
| def create_app(): | ||
| import webapp2 | ||
| class GetTraceId(webapp2.RequestHandler): |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
| from google.cloud.logging.handlers import _helpers | ||
| trace_id = _helpers.get_trace_id_from_webapp2() | ||
| self.response.content_type = "text/plain" |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
| return app | ||
| def setUp(self): | ||
| self.app = self.create_app() |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
| response = req.get_response(self.app) | ||
| trace_id = response.body | ||
| self.assertEquals("None", trace_id) |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
liyanhui1228
commented
Jul 7, 2017
@csilvers Yeah there is some inconsistent in the repo for Also I have just tested your changes in real life and it works well! I know it will take some time to figure out how to test it in real life because I did the same thing when I worked on this feature. Thank you again for sending this PR :) |
csilvers
commented
Jul 7, 2017
Thanks @liyanhui1228 for doing the real-life testing! And also for your code: I got compliments on my coding style and that's entirely because I cribbed from you. :-) |
Mostly style and documentation changes, and a few code-reorganization changes; nothing contentful.
csilvers
commented
Jul 7, 2017
My latest commit addresses all the issues but these, which I'm leaving as-is unless people feel strongly:
|
liyanhui1228
commented
Jul 7, 2017
This LGTM, but wait for @dhermes for merging. Thanks! |
lukesneeringer
commented
Jul 10, 2017
Good to merge. Ignore the CLA bot. |
duggelz
commented
Jul 10, 2017
CLA bot now lists Khan Academy as a signer. |
Code was added in #3448 that set the trace-id when used in the
flask or django web frameworks.
This extends that to also support webapp2.
webapp2 is not cutting edge technology anymore -- it doesn't even work
on python3 -- but a lot of legacy GAE classic apps use webapp2, so I
think it makes sense to support it.
I tested this by running
noxin thelogging/directory, which as faras I can tell is current state of the art for running tests.