OpenTelemetry symbols marked private to pyright
How do you use Sentry?
Sentry Saas (sentry.io)
Version
1.32.0
Steps to Reproduce
- Use pyright
- Import from opentelemetry package:
from sentry_sdk.integrations.opentelemetry import SentryPropagator, SentrySpanProcessor
- pyright error:
error: "SentryPropagator" is not exported from module "sentry_sdk.integrations.opentelemetry"
Import from "sentry_sdk.integrations.opentelemetry.propagator" instead (reportPrivateImportUsage)
error: "SentrySpanProcessor" is not exported from module "sentry_sdk.integrations.opentelemetry"
Import from "sentry_sdk.integrations.opentelemetry.span_processor" instead (reportPrivateImportUsage)
Expected Result
Import without pyright errors
Actual Result
Errors as above.
By changing the imports to be more specific pyright would pass but this is not in the documentation:
from sentry_sdk.integrations.opentelemetry.propagator import SentryPropagator
from sentry_sdk.integrations.opentelemetry.span_processor import SentrySpanProcessor
Hey @quom, thanks for bringing this to our attention. We don't check our code with pyright so we weren't aware of this.
This needs a bit of looking into how to tell pyright those are not private imports. Based on my very brief investigation adding an __all__ to the __init__.py could do the trick. Some related issues I found, for the reference:
- https://github.com/yt-project/yt/issues/4683
- https://github.com/microsoft/pyright/issues/2639
Is this issue assigned ?
@khuongduy354 No, it's up for grabs!
I know this may be a beginner question, my project cant find Open Telemetry package, i followed this guide here: https://github.com/getsentry/sentry-python/blob/master/CONTRIBUTING.md, import sentry_sdk.integrations works, and i checked there's a init.py in opentelemtry folder (so it's a package). Don't know why it cant be found. This is the full error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/khuongduy354/developments/ongoing/sentry-python/sentry_sdk/integrations/opentelemetry/__init__.py", line 1, in <module>
from sentry_sdk.integrations.opentelemetry.span_processor import ( # noqa: F401
File "/home/khuongduy354/developments/ongoing/sentry-python/sentry_sdk/integrations/opentelemetry/span_processor.py", line 3, in <module>
from opentelemetry.context import get_value # type: ignore
ModuleNotFoundError: No module named 'opentelemetry'
@khuongduy354 The error is a bit misleading, it's complaining about not being able to import opentelemetry the third-party dependency, as opposed to something in the SDK. Do you have the opentelemetry-distro package installed? Running pip install 'opentelemetry-distro>=0.35b0' should do the trick.
Pyright doesn't yield error with the original code. I don't use pyright, so i don't know if i reproduced the issue correctly.
What i did was
pip install pyright , then try importing things from opentelemetry as above. Then run pyright testpy.py, no error pops up
Tried to repro too, also unsuccessfully. @quom could you help us out with more details on how you installed and ran pyright and what version you're using? I have pyright==1.1.332 and I'm not seeing the reportPrivateImportUsage errors.
$ pyright --version
pyright 1.1.332
$ pyright tracing.py
tracing.py
tracing.py:16:51 - error: "SentryPropagator" is not exported from module "sentry_sdk.integrations.opentelemetry"
Import from "sentry_sdk.integrations.opentelemetry.propagator" instead (reportPrivateImportUsage)
tracing.py:16:69 - error: "SentrySpanProcessor" is not exported from module "sentry_sdk.integrations.opentelemetry"
Import from "sentry_sdk.integrations.opentelemetry.span_processor" instead (reportPrivateImportUsage)
2 errors, 0 warnings, 0 informations
I can see why it is creating the error as per the spec for typed libraries. Are you using the normal sentry_sdk package install or a local dev dependency of it? There may be different behaviour depending on how the package is installed in your environment.
Maybe a local dev dependency. I followed setup here: https://github.com/getsentry/sentry-python/blob/master/CONTRIBUTING.md. Which is pip install in editable mode
I'm not exactly sure why pyright might not be reproducing it on your side (can't see anything specific about local dependencies that may be different). Here is an example of a package's __init__.py that explicitly defines the exports though: https://github.com/litestar-org/litestar/blob/main/litestar/middleware/init.py
Does adding add to init.py in your telemetry fix the issue on your machine?