Thai
Thai

Reputation: 46

Using Telemetry auto instrumentation with Python Flask app, how to avoid calling FlaskInstrumentor().instrument_app(app) manually?

I'm learning telemetry auto instrumentation with a Python Flask-based app. The issue is my app will not generate traces unless I call FlaskInstrumentor().instrument_app(app) manually.

I assumed that auto_instrumentation/sitecustomize.py will somehow do that but it doesn't seem to work that way.

My code:

from flask import Flask, request

from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import (
    SimpleSpanProcessor,
)
from opentelemetry.exporter.zipkin.json import ZipkinExporter
from opentelemetry.instrumentation.flask import FlaskInstrumentor
from opentelemetry.instrumentation.requests import RequestsInstrumentor
PORT = 8000
MESSAGE = "Hello, world!\n"

app = Flask(__name__)

# create a ZipkinExporter
zipkin_exporter = ZipkinExporter(
)

trace.set_tracer_provider(TracerProvider())
trace.get_tracer_provider().add_span_processor(
    SimpleSpanProcessor(zipkin_exporter)
)


FlaskInstrumentor().instrument_app(app)


@app.route("/")
def root():
    result = MESSAGE.encode("utf-8")
    return result


if __name__ == "__main__":
    app.run(debug=True, host="0.0.0.0", port=PORT)

Packages:

opentelemetry-api==1.2.0
opentelemetry-sdk==1.2.0
opentelemetry-instrumentation-flask==0.21b0
opentelemetry-instrumentation==0.21b0
opentelemetry-distro==0.21b0
opentelemetry-exporter-zipkin==1.2.0 

Command I used: "opentelemetry-instrument python3 ./server.py"

Thanks

Upvotes: 1

Views: 1791

Answers (1)

Dave W. Smith
Dave W. Smith

Reputation: 24966

https://github.com/open-telemetry/opentelemetry-python-contrib/blob/main/instrumentation/opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask/__init__.py suggests doing just that.

Unfortunately, https://opentelemetry-python-contrib.readthedocs.io/en/stable/instrumentation/flask/flask.html is broken as of this moment.

Upvotes: 1

Related Questions