-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Add support for logging the trace-id in webapp2 apps. #3593
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
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.
|
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.
|
|
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. |
|
(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.) |
|
@lukesneeringer How do we poke the clabot? (Craig is/was Google employee |
|
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? |
|
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!) |
|
btw, hi doug! |
|
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.
liyanhui1228
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
| """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.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
dhermes
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 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.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
| """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.
| """ | ||
| 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.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
| 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.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
| 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.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
| 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.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
| 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.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
| 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.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
| 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.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
| # 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.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
csilvers
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
`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.
| """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.
| 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.
| 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.
| """ | ||
| 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.
| 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.
| 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.
| 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.
| 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.
| 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.
|
@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 :) |
|
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.
|
My latest commit addresses all the issues but these, which I'm leaving as-is unless people feel strongly:
|
|
This LGTM, but wait for @dhermes for merging. Thanks! |
|
Good to merge. Ignore the CLA bot. |
|
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.