sentry-python icon indicating copy to clipboard operation
sentry-python copied to clipboard

OpenTelemetry symbols marked private to pyright

Open quom opened this issue 2 years ago • 11 comments

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

quom avatar Oct 18 '23 13:10 quom

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

sentrivana avatar Oct 18 '23 14:10 sentrivana

Is this issue assigned ?

khuongduy354 avatar Oct 23 '23 10:10 khuongduy354

@khuongduy354 No, it's up for grabs!

sentrivana avatar Oct 23 '23 10:10 sentrivana

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 avatar Oct 23 '23 15:10 khuongduy354

@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.

sentrivana avatar Oct 23 '23 15:10 sentrivana

image 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

khuongduy354 avatar Oct 24 '23 09:10 khuongduy354

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.

sentrivana avatar Oct 24 '23 10:10 sentrivana

$ 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.

quom avatar Oct 24 '23 10:10 quom

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

khuongduy354 avatar Oct 24 '23 12:10 khuongduy354

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

quom avatar Oct 24 '23 13:10 quom

Does adding add to init.py in your telemetry fix the issue on your machine?

khuongduy354 avatar Oct 24 '23 14:10 khuongduy354