Sentry integration issue


#1

When I am following the below instructions to integrate Sentry with my Python 3.7 based Sanic app. I am running into an error. I get a timeout error and my Sanic app stops responding.

Docs: https://docs.sentry.io/platforms/python/sanic/

import sentry_sdk
from sentry_sdk.integrations.sanic import SanicIntegration

sentry_sdk.init(
    dsn="https://<key>@sentry.io/<project>",
    integrations=[SanicIntegration()]
)

app = Sanic(__name__)

Error:

[2018-11-14 12:47:12 +0600] [1] [ERROR] Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/sanic/server.py", line 178, in response_timeout_callback
    raise ServiceUnavailable('Response Timeout')
sanic.exceptions.ServiceUnavailable: Response Timeout

Exception in callback <bound method HttpProtocol.response_timeout_callback of <sanic.server.HttpProtocol object at 0x7fd39a8bdaf0>>
handle: <TimerHandle cancelled HttpProtocol.response_timeout_callback>
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/sanic/server.py", line 178, in response_timeout_callback
    raise ServiceUnavailable('Response Timeout')
sanic.exceptions.ServiceUnavailable: Response Timeout

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "uvloop/cbhandles.pyx", line 245, in uvloop.loop.TimerHandle._run
  File "/usr/local/lib/python3.7/site-packages/sanic/server.py", line 180, in response_timeout_callback
    self.write_error(exception)
  File "/usr/local/lib/python3.7/site-packages/sanic/server.py", line 429, in write_error
    self.log_response(response)
  File "/usr/local/lib/python3.7/site-packages/sanic/server.py", line 321, in log_response
    self.request.url)
  File "/usr/local/lib/python3.7/site-packages/sanic/request.py", line 253, in url
    self.scheme,
  File "/usr/local/lib/python3.7/site-packages/sanic/request.py", line 213, in scheme
    if self.app.websocket_enabled \
AttributeError: 'NoneType' object has no attribute 'websocket_enabled'

#2

I’ve never used sentry, so I do not know from experience. At what point does this error pop up? At startup or upon a request?


#3

@mashrikt

Once you register your org in sentry.io, you should get a url something similar to https://sentry_key@sentry.io/projectid (eg: https://a279c78721527b711f7d0@sentry.io/7120185) once you have this, the following code should be able to setup your sanic app under sentry for monitoring.

import sentry_sdk
from sentry_sdk.integrations.sanic import SanicIntegration
from sanic import Sanic
from sanic.response import json
from json import loads

sentry_sdk.init(
    dsn="https://a279c78721527b711f7d0@sentry.io/7120185",
    integrations=[SanicIntegration()]
)

app = Sanic(__name__)


@app.route("/", methods=frozenset({"POST"}))
def sample(request):
    body = loads(request.body)
    return json({
        "response": body["x"] + body["y"]
    })


app.run()

With the above changes, based on your API invocation and exceptions inside the method, you should see something as follows in your Sentry dashboard.

Please let me know if this works.


#4
    return json({
        "response": body["x"] + body["y"]
    })

is your problem - one of those two is an int, one is a str. you can’t add those together.

Try:

    return json({
        "response": "{}{}".format(body["x"], body["y"])
    })

#5

@sjsadowski That issue in the code is intentional.

I just wanted to show @mashrikt how to setup sentry with a simple API which can easily be invoked and it can throw an error.


#6

@harshanarayana I understand now. Too early where I am, only just started on my coffee.