open-telemetry / opentelemetry-python-contrib

OpenTelemetry instrumentation for Python modules
https://opentelemetry.io
Apache License 2.0
739 stars 613 forks source link

flask: create spans for CLI commands #2926

Open codefromthecrypt opened 1 month ago

codefromthecrypt commented 1 month ago

What problem do you want to solve?

app.cli.command are not instrumented

@app.cli.command()
def create_index():
    """Create or re-create the Elasticsearch index."""
    basedir = os.path.abspath(os.path.dirname(__file__))
    sys.path.append(f"{basedir}/../")

    from data import index_data

    index_data.main()

Describe the solution you'd like

I would like to see an internal (likely root) span with the same name as the function.

Describe alternatives you've considered

I would like to have an auto-instrumentation alternative to creating a span for an app.cli.command like so:

@app.cli.command()
# Currently, flask auto-instrumentation does not trace CLI commands
@tracer.start_as_current_span("create_index")
def create_index():
    """Create or re-create the Elasticsearch index."""
    basedir = os.path.abspath(os.path.dirname(__file__))
    sys.path.append(f"{basedir}/../")

    from data import index_data

    index_data.main()

Additional Context

No response

Would you like to implement a fix?

No

codefromthecrypt commented 2 weeks ago

looks like the way to fix this is actually more about click instrumentation than flask CLI. Adjusting the description accordingly