opentelemetry-python-contrib icon indicating copy to clipboard operation
opentelemetry-python-contrib copied to clipboard

Flask Instrumentation doesn't work with "from flask import Flask"

Open jeremydvoss opened this issue 2 years ago • 9 comments

Describe your environment

Steps to reproduce Flask does not work with "from flask import Flask":

from flask import Flask
from opentelemetry.instrumentation.flask import FlaskInstrumentor

FlaskInstrumentor().instrument()
app = Flask(__name__)


@app.route("/")
def hello():
    return "Hello!"

if __name__ == "__main__":
    app.run(debug=True)

What is the expected behavior? Flask calls should be instrumented

What is the actual behavior? Flask calls are not instrumentated

Additional context The instrumentation only works if the imports are set up as such:

import flask
from opentelemetry.instrumentation.flask import FlaskInstrumentor

FlaskInstrumentor().instrument()

app = flast.Flask(__name__)

@app.route("/")
def hello():
    return "Hello!"

if __name__ == "__main__":
    app.run(debug=True)

It also works if you use the instrument_app method instead:

from flask import Flask
from opentelemetry.instrumentation.flask import FlaskInstrumentor

app = Flask(__name__)

FlaskInstrumentor().instrument_app(app)

@app.route("/")
def hello():
    return "Hello!"

if __name__ == "__main__":
    app.run(debug=True)

jeremydvoss avatar Aug 25 '23 18:08 jeremydvoss

Flask docs recommend "from flask import Flask"

jeremydvoss avatar Aug 25 '23 18:08 jeremydvoss

NOTE: A previous version of this issue wrongfully identified the docs as broken because they use "from flask import Flask". However, because those same docs use "instrument_app(app)" instead of "instrument()", it still works.

jeremydvoss avatar Aug 25 '23 20:08 jeremydvoss

Does it work if you swap the order?

from opentelemetry.instrumentation.flask import FlaskInstrumentor
FlaskInstrumentor().instrument()

from flask import Flask
# ...

aabmass avatar Aug 25 '23 20:08 aabmass

Yes. I also just confirmed that because auto-instrumentation triggers before any imports, from flask import Flask works for that as well.

jeremydvoss avatar Aug 30 '23 21:08 jeremydvoss

@jeremydvoss

Status of this?

lzchen avatar Sep 21 '23 22:09 lzchen

Sounds like we can close this then @jeremydvoss ?

aabmass avatar Oct 26 '23 21:10 aabmass

Creating a Flask app involves a series of steps. Flask is a lightweight web framework for Python that is widely used for building web applications. Here's a simple example to help you get started:

  1. Install Flask: Ensure you have Python installed on your system. You can install Flask using pip, the Python package installer. Open your terminal or command prompt and run the following command:

    pip install Flask
    
  2. Create Your Flask App: Create a new directory for your Flask app and navigate to it in the terminal.

    mkdir my_flask_app
    cd my_flask_app
    
  3. Create a Python Script: Inside your app directory, create a Python script (e.g., app.py) with the following content:

    from flask import Flask
    
    app = Flask(__name__)
    
    @app.route('/')
    def hello_world():
        return 'Hello, World!'
    
    if __name__ == '__main__':
        app.run(debug=True)
    
  4. Run Your Flask App: In the terminal, run the following command to start your Flask app:

    python app.py
    

    This will start the development server, and you should see output indicating that the server is running. By default, your app will be accessible at http://127.0.0.1:5000/ or http://localhost:5000/.

  5. Access Your App: Open a web browser and go to http://127.0.0.1:5000/ or http://localhost:5000/. You should see the "Hello, World!" message.

Congratulations! You've just created a simple Flask app. From here, you can start building more complex applications by defining additional routes, handling form submissions, and connecting to databases.

Remember that this is a basic example, and as your app grows, you may want to structure it differently, use templates for HTML rendering, and add more features based on your requirements. Refer to the Flask documentation for more detailed information and advanced topics.

samprit-ghosh avatar Dec 29 '23 16:12 samprit-ghosh

We just experienced this, so I think it's still open, right? We're going to explicitly use the FlaskInstrumentor call for now.

pamelafox avatar Jan 17 '24 20:01 pamelafox

I also experienced this with the FastAPI instrumentor, where I'd previously been using "from fastapi import FastAPI". Seems to work once I changed it to "import fastapi".

pamelafox avatar Jul 23 '24 18:07 pamelafox