sentry-python
sentry-python copied to clipboard
Sending a test error as per "Configure Python SDK" instructions doesn't work but sending from another thread does work
Self-Hosted Version
24.1.0 and 24.1.1
CPU Architecture
x86_64
Docker Version
Docker version 24.0.7, build afdd53b
Docker Compose Version
2.11.1
Steps to Reproduce
- Clone repo
- Run
install.sh
- Add the
system.url-prefix
variablewith the value of your domain in yoursentry/config.yml
-
docker-compose up -d
Expected Result
Successful sending of test errors using the Python SDK as described in "Configure Python SDK"
Actual Result
Nothing is recorded in the logs of nginx or the web container when sending the error by following the instructions but this works:
import sentry_sdk
sentry_sdk.init(...)
# does not work
division_by_zero = 1 / 0
# works
import threading
t = threading.Thread(target=lambda: 1/0, args=())
t.start()
Our Sentry is deployed on a server behind a reverse proxy (AWS NLB). We also tried with the version 24.1.1 without the NLB.
Event ID
No response
Hey @Erokos, thanks for writing in.
What does your sentry_sdk.init
look like? If you just followed the setup example I assume you have this, correct?
sentry_sdk.init(
dsn="<YOUR DSN>",
traces_sample_rate=1.0,
profiles_sample_rate=1.0,
)
Can you try adding debug=True
to the init
and running the example without the thread part? You should get some debug logs on startup; can you see something like this?
[sentry] DEBUG: Sending envelope [envelope with 1 items (error)] project:<PROJECT_ID> host:<HOSTNAME>
Hi @sentrivana! Thank you for your response. We've tried that and dug a bit deeper with a number of experiments to both our self-hosted and SaaS. Turns out we get the same results on both. When sending via the CLI we see the errors in both Sentries but when using the Python SDK from the Python CLI that doesn't work even though it says it sent the event. However the same code WORKS when used from a file, ran as a script or a program so our services send events to our self-hosted Sentry successfully.
I don't understand this behavior and the docs should be updated accordingly.
This is indeed strange.
Do I get this right:
a) when you run the code in Python CLI then it does NOT work (but the debug log says [sentry] DEBUG: Sending envelope [envelope with 1 items (error)] project:<PROJECT_ID> host:<HOSTNAME>
b) when you run the same code in a file (something like python mytest.py
) then the error IS sent?
maybe there is an SENTRY_DSN
environment variable set somewhere?
Can you do this in both your scenarios:
from sentry_sdk import Hub
print(Hub.current.client.options.get("dsn"))
And check if the DSN is the same in both cases?
This issue has gone three weeks without activity. In another week, I will close it.
But! If you comment or otherwise update it, I will reset the clock, and if you remove the label Waiting for: Community
, I will leave it alone ... forever!
"A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀
Hi @antonpirker.
I am a colleague of @Erokos and I can answer:
a) does not work from Python CLI and there is actually no line like [sentry] DEBUG: Sending envelope [envelope with 1 items (error)] project:<PROJECT_ID> host:<HOSTNAME>
b) it works from script and the line appears.
In both scenarios, executing
from sentry_sdk import Hub
print(Hub.current.client.options.get("dsn"))
returns a proper and SAME value like https://[email protected]/12
that only appears after initializing sentry.
We are basically just confused about the first scenario why would it not work when it could be the first thing a developer might try.
I'm having a similar issue with the basic Python getting started example found here. Here's the code snippet:
import sentry_sdk
sentry_sdk.init(
dsn=DSN,
# Enable performance monitoring
enable_tracing=True,
)
division_by_zero = 1 / 0
I've tested with debug=True
and haven't found the Sending envelope
debug message. Uncaught exceptions don't get reported, but explicit reporting with sentry_sdk.capture_exception
and the logging
module work just fine. The Flask example also works fine with the same DSN (so same project, same settings).
from flask import Flask
import sentry_sdk
sentry_sdk.init(
dsn=DSN,
# Enable performance monitoring
enable_tracing=True,
)
app = Flask(__name__)
@app.route('/')
def hello_world():
1/0 # raises an error
return "<p>Hello, World!</p>"
Using python 3.10.4
and sentry-sdk 1.42.0
with the Developer pricing tier (free tier).
Hey @stefan-ptrvch !
Thanks for the sample code and explanation!
I have tried this now with this project: https://github.com/antonpirker/testing-sentry/tree/main/test-plain-python
My debug out put is this:
[sentry] DEBUG: Setting up integrations (with default = True)
[sentry] DEBUG: Did not import default integration sentry_sdk.integrations.aiohttp.AioHttpIntegration: AIOHTTP not installed
[sentry] DEBUG: Did not import default integration sentry_sdk.integrations.boto3.Boto3Integration: botocore is not installed
[sentry] DEBUG: Did not import default integration sentry_sdk.integrations.bottle.BottleIntegration: Bottle not installed
[sentry] DEBUG: Did not import default integration sentry_sdk.integrations.celery.CeleryIntegration: Celery not installed
[sentry] DEBUG: Did not import default integration sentry_sdk.integrations.django.DjangoIntegration: Django not installed
[sentry] DEBUG: Did not import default integration sentry_sdk.integrations.falcon.FalconIntegration: Falcon not installed
[sentry] DEBUG: Did not import default integration sentry_sdk.integrations.fastapi.FastApiIntegration: Starlette is not installed
[sentry] DEBUG: Did not import default integration sentry_sdk.integrations.flask.FlaskIntegration: Flask is not installed
[sentry] DEBUG: Did not import default integration sentry_sdk.integrations.httpx.HttpxIntegration: httpx is not installed
[sentry] DEBUG: Did not import default integration sentry_sdk.integrations.openai.OpenAIIntegration: OpenAI not installed
[sentry] DEBUG: Did not import default integration sentry_sdk.integrations.pyramid.PyramidIntegration: Pyramid not installed
[sentry] DEBUG: Did not import default integration sentry_sdk.integrations.rq.RqIntegration: RQ not installed
[sentry] DEBUG: Did not import default integration sentry_sdk.integrations.sanic.SanicIntegration: Sanic not installed
[sentry] DEBUG: Did not import default integration sentry_sdk.integrations.sqlalchemy.SqlalchemyIntegration: SQLAlchemy not installed.
[sentry] DEBUG: Did not import default integration sentry_sdk.integrations.starlette.StarletteIntegration: Starlette is not installed
[sentry] DEBUG: Did not import default integration sentry_sdk.integrations.tornado.TornadoIntegration: Tornado not installed
[sentry] DEBUG: Setting up previously not enabled integration argv
[sentry] DEBUG: Setting up previously not enabled integration atexit
[sentry] DEBUG: Setting up previously not enabled integration dedupe
[sentry] DEBUG: Setting up previously not enabled integration excepthook
[sentry] DEBUG: Setting up previously not enabled integration logging
[sentry] DEBUG: Setting up previously not enabled integration modules
[sentry] DEBUG: Setting up previously not enabled integration stdlib
[sentry] DEBUG: Setting up previously not enabled integration threading
[sentry] DEBUG: Setting up previously not enabled integration redis
[sentry] DEBUG: Did not enable default integration redis: Redis client not installed
[sentry] DEBUG: Enabling integration argv
[sentry] DEBUG: Enabling integration atexit
[sentry] DEBUG: Enabling integration dedupe
[sentry] DEBUG: Enabling integration excepthook
[sentry] DEBUG: Enabling integration logging
[sentry] DEBUG: Enabling integration modules
[sentry] DEBUG: Enabling integration stdlib
[sentry] DEBUG: Enabling integration threading
[sentry] DEBUG: Setting SDK name to 'sentry.python'
Traceback (most recent call last):
File "/Users/antonpirker/code/testing-sentry/test-plain-python/main.py", line 12, in <module>
division_by_zero = 1 / 0
ZeroDivisionError: division by zero
[sentry] DEBUG: atexit: got shutdown signal
[sentry] DEBUG: atexit: shutting down client
[sentry] DEBUG: Flushing HTTP transport
[sentry] DEBUG: background worker got flush request
[sentry] DEBUG: Sending envelope [envelope with 1 items (error)] project:5461230 host:o447951.ingest.sentry.io
[sentry] DEBUG: 2 event(s) pending on flush
Sentry is attempting to send 2 pending events
Waiting up to 2 seconds
Press Ctrl-C to quit
[sentry] DEBUG: background worker flushed
[sentry] DEBUG: Killing HTTP transport
[sentry] DEBUG: background worker got kill request
And the error is captured by Sentry:
So I can not reproduce your problem...
Could you post your debug output here, maybe it tells us something? Thanks!
Hi @antonpirker, thanks for the help!
I've run your example and it worked!
What I've found after further testing is that the event doesn't get sent if the error is raised interactively in the Python REPL. For instance, if I manually type out or copy-paste the code from your example, or even if I run run main.py
inside an IPython REPL, the error doesn't get reported (no sending envelope debug message). I've tested regular Python REPL and IPython REPL. Here's what I get when I start IPython, run main.py
and then manually quit.
(base) (.venv) ➜ test-plain-python git:(main) ✗ ipython
Python 3.12.2 | packaged by Anaconda, Inc. | (main, Feb 27 2024, 12:57:28) [Clang 14.0.6 ]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.22.2 -- An enhanced Interactive Python. Type '?' for help.
In [1]: run main.py
[sentry] DEBUG: Setting up integrations (with default = True)
[sentry] DEBUG: Did not import default integration sentry_sdk.integrations.aiohttp.AioHttpIntegration: AIOHTTP not installed
[sentry] DEBUG: Did not import default integration sentry_sdk.integrations.boto3.Boto3Integration: botocore is not installed
[sentry] DEBUG: Did not import default integration sentry_sdk.integrations.bottle.BottleIntegration: Bottle not installed
[sentry] DEBUG: Did not import default integration sentry_sdk.integrations.celery.CeleryIntegration: Celery not installed
[sentry] DEBUG: Did not import default integration sentry_sdk.integrations.django.DjangoIntegration: Django not installed
[sentry] DEBUG: Did not import default integration sentry_sdk.integrations.falcon.FalconIntegration: Falcon not installed
[sentry] DEBUG: Did not import default integration sentry_sdk.integrations.fastapi.FastApiIntegration: Starlette is not installed
[sentry] DEBUG: Did not import default integration sentry_sdk.integrations.httpx.HttpxIntegration: httpx is not installed
[sentry] DEBUG: Did not import default integration sentry_sdk.integrations.openai.OpenAIIntegration: OpenAI not installed
[sentry] DEBUG: Did not import default integration sentry_sdk.integrations.pyramid.PyramidIntegration: Pyramid not installed
[sentry] DEBUG: Did not import default integration sentry_sdk.integrations.rq.RqIntegration: RQ not installed
[sentry] DEBUG: Did not import default integration sentry_sdk.integrations.sanic.SanicIntegration: Sanic not installed
[sentry] DEBUG: Did not import default integration sentry_sdk.integrations.sqlalchemy.SqlalchemyIntegration: SQLAlchemy not installed.
[sentry] DEBUG: Did not import default integration sentry_sdk.integrations.starlette.StarletteIntegration: Starlette is not installed
[sentry] DEBUG: Did not import default integration sentry_sdk.integrations.tornado.TornadoIntegration: Tornado not installed
[sentry] DEBUG: Setting up previously not enabled integration argv
[sentry] DEBUG: Setting up previously not enabled integration atexit
[sentry] DEBUG: Setting up previously not enabled integration dedupe
[sentry] DEBUG: Setting up previously not enabled integration excepthook
[sentry] DEBUG: Setting up previously not enabled integration logging
[sentry] DEBUG: Setting up previously not enabled integration modules
[sentry] DEBUG: Setting up previously not enabled integration stdlib
[sentry] DEBUG: Setting up previously not enabled integration threading
[sentry] DEBUG: Setting up previously not enabled integration flask
[sentry] DEBUG: Setting up previously not enabled integration redis
[sentry] DEBUG: Did not enable default integration redis: Redis client not installed
[sentry] DEBUG: Enabling integration argv
[sentry] DEBUG: Enabling integration atexit
[sentry] DEBUG: Enabling integration dedupe
[sentry] DEBUG: Enabling integration excepthook
[sentry] DEBUG: Enabling integration logging
[sentry] DEBUG: Enabling integration modules
[sentry] DEBUG: Enabling integration stdlib
[sentry] DEBUG: Enabling integration threading
[sentry] DEBUG: Enabling integration flask
[sentry] DEBUG: Setting SDK name to 'sentry.python.flask'
---------------------------------------------------------------------------
ZeroDivisionError Traceback (most recent call last)
File ~/Desktop/projects/ikea-crawler/testing-sentry/test-plain-python/main.py:12
3 import sentry_sdk
6 sentry_sdk.init(
7 dsn=os.environ["SENTRY_DSN"],
8 enable_tracing=True,
9 debug=True,
10 )
---> 12 division_by_zero = 1 / 0
ZeroDivisionError: division by zero
In [2]:
Do you really want to exit ([y]/n)?
[sentry] DEBUG: atexit: got shutdown signal
[sentry] DEBUG: atexit: shutting down client
[sentry] DEBUG: Flushing HTTP transport
[sentry] DEBUG: background worker got flush request
[sentry] DEBUG: background worker flushed
[sentry] DEBUG: Killing HTTP transport
[sentry] DEBUG: background worker got kill request
(base) (.venv) ➜ test-plain-python git:(main) ✗
NOTE: You may see the line [sentry] DEBUG: Setting SDK name to 'sentry.python.flask'
, but I tested the example both with and without Flask installed and had the same result.
Another thing I found:
-
python main.py
-> reports error -
ipython main.py
-> doesn't report error
The way the getting started section for the plain Python setup is written(https://docs.sentry.io/platforms/python/), it kind of makes you automatically copy-paste the code into the REPL, but it seems to not work if you do it this way.
Also, maybe this is somehow related to @Erokos example. If you run his code in the REPL the first snippet doesn't report the error but the second one does.
The same thing as my colleague and I have reported, works from a script, i.e. program but not from the interactive python shell.
Thank you folks -- I think we just need to clarify that the code won't run from the (I)Python shell in the docs and in Sentry when you create a new project. I can take care of this.
That sounds great, thanks!
Added a note to the Getting Started docs. We also link to this page from the project set up wizard in Sentry, so hopefully this will be discoverable; if not, we can also add it to the wizard directly later.