open-telemetry / opentelemetry-python-contrib

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

Fix exemplar support #3031

Open fcollonval opened 2 days ago

fcollonval commented 2 days ago

Describe your environment

OS: Debian 12 Python version: Any Package version: 0.49b0 and later

What happened?

Since 1.28.0, the opentelemetry Python SDK supports exemplars. However some instrumentation libraries does not add exemplars because the measure are added/recorded outside of the active span.

For example, in the asgi instrumentation, all measures should be taken within the _usespan context rather than outside:

https://github.com/open-telemetry/opentelemetry-python-contrib/blob/ae13079df8ca150480aa870c965c7828a80e7e22/instrumentation/opentelemetry-instrumentation-asgi/src/opentelemetry/instrumentation/asgi/__init__.py#L707-L709

Steps to Reproduce

You can test the repository https://github.com/webscit/opentelemetry-demo-python with OTEL_AUTO_INSTRUMENTATION_LEVEL = 1, it uses fastapi instrumentation (but the flaw is actually in the asgi instrumentation).

Expected Result

Exemplars are recorded for all Python instrumentations

Actual Result

Some instrumentations are missing exemplars

Additional context

I don't know if all packages or only a subset of them are concerned by this. I have at least identified 3 cases:

Package Line to blame
asgi https://github.com/open-telemetry/opentelemetry-python-contrib/blob/ae13079df8ca150480aa870c965c7828a80e7e22/instrumentation/opentelemetry-instrumentation-asgi/src/opentelemetry/instrumentation/asgi/__init__.py#L709
wsgi https://github.com/open-telemetry/opentelemetry-python-contrib/blob/ae13079df8ca150480aa870c965c7828a80e7e22/instrumentation/opentelemetry-instrumentation-wsgi/src/opentelemetry/instrumentation/wsgi/__init__.py#L664
tornado https://github.com/open-telemetry/opentelemetry-python-contrib/blob/ae13079df8ca150480aa870c965c7828a80e7e22/instrumentation/opentelemetry-instrumentation-tornado/src/opentelemetry/instrumentation/tornado/__init__.py#L388

Would you like to implement a fix?

None