Skip to content

Flask instrumentor logs an error message when used with app.test_request_context #160

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
jpmelos opened this issue Oct 27, 2020 · 0 comments · Fixed by #253
Closed

Flask instrumentor logs an error message when used with app.test_request_context #160

jpmelos opened this issue Oct 27, 2020 · 0 comments · Fixed by #253

Comments

@jpmelos
Copy link
Contributor

jpmelos commented Oct 27, 2020

Describe your environment

You should be able to see the same result regardless of what the environment is.

Steps to reproduce

Python 3.6.9 (default, Apr 15 2020, 09:31:57) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.16.1 -- An enhanced Interactive Python. Type '?' for help.

In [1]: from opentelemetry.instrumentation.flask import FlaskInstrumentor
   ...: FlaskInstrumentor().instrument()
   ...: 
   ...: import flask
   ...: 
   ...: app = flask.Flask(__name__)
   ...: 
   ...: with app.test_request_context():
   ...:     print('Hey!')
   ...: 
Hey!
Flask environ's OpenTelemetry activation missingat _teardown_flask_request(None)

What is the expected behavior?

I did not expect to see the error message in the last line of the example.

What is the actual behavior?

I see the line.

Additional context

According to Flask’s documentation, app.test_request_context does not run before_request and after_request handlers, but it does run teardown_request handlers, which means the counter-handler of before_request is after_request, not teardown_request. Source: https://flask.palletsprojects.com/en/1.1.x/testing/#other-testing-tricks (5th paragraph after the anchor).

The Flask instrumentor doesn't take that into consideration, and it registers a handler with before_request, and expects it to have run when the teardown_request handler runs, which is not guaranteed: handlers are created[1][2] and handler's are removed. Because the before_request handler won't run for app.test_request_context, it won't have flask.request.environ['_ENVIRON_ACTIVATION_KEY'], which is required here to close the span that should have been opened in the before_request handler that never ran.

srikanthccv referenced this issue in srikanthccv/opentelemetry-python Nov 1, 2020
Co-authored-by: Mayur Kale <mayurkale@google.com>
@lzchen lzchen transferred this issue from open-telemetry/opentelemetry-python Nov 9, 2020
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant