GithubHelp home page GithubHelp logo

getsentry / sentry-python Goto Github PK

View Code? Open in Web Editor NEW
1.7K 72.0 454.0 40.51 MB

The official Python SDK for Sentry.io

Home Page: https://sentry.io/for/python/

License: MIT License

Python 99.26% Shell 0.25% Makefile 0.09% HTML 0.01% Jinja 0.39%
sentry python sentry-python crash-reporting hacktoberfest tag-production

sentry-python's Introduction

Sentry

Bad software is everywhere, and we're tired of it. Sentry is on a mission to help developers write better software faster, so we can get back to enjoying technology. If you want to join us Check out our open positions

Official Sentry SDK for Python

Build Status PyPi page link -- version Discord

This is the official Python SDK for Sentry


Getting Started

Install

pip install --upgrade sentry-sdk

Configuration

import sentry_sdk

sentry_sdk.init(
    "https://[email protected]/1",

    # Set traces_sample_rate to 1.0 to capture 100%
    # of transactions for performance monitoring.
    traces_sample_rate=1.0,
)

Usage

from sentry_sdk import capture_message
capture_message("Hello World")  # Will create an event in Sentry.

raise ValueError()  # Will also create an event in Sentry.

Integrations

(If you want to create a new integration, have a look at the Adding a new integration checklist.)

See the documentation for an up-to-date list of libraries and frameworks we support. Here are some examples:

Migrating From raven-python

The old raven-python client has entered maintenance mode and was moved here.

If you're using raven-python, we recommend you to migrate to this new SDK. You can find the benefits of migrating and how to do it in our migration guide.

Contributing to the SDK

Please refer to CONTRIBUTING.md.

Getting Help/Support

If you need help setting up or configuring the Python SDK (or anything else in the Sentry universe) please head over to the Sentry Community on Discord. There is a ton of great people in our Discord community ready to help you!

Resources

  • Documentation
  • Forum
  • Discord
  • Stack Overflow
  • Twitter Follow

License

Licensed under the MIT license, see LICENSE

sentry-python's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

sentry-python's Issues

TypeError: getresponse() got an unexpected keyword argument 'buffering'

Traceback (most recent call last):
  File "/app/.heroku/python/lib/python3.7/site-packages/botocore/vendored/requests/packages/urllib3/connectionpool.py", line 372, in _make_request
    httplib_response = conn.getresponse(buffering=True)
  File "/app/.heroku/python/lib/python3.7/site-packages/sentry_sdk/integrations/stdlib.py", line 38, in getresponse
    rv = real_getresponse(self, *args, **kwargs)
TypeError: getresponse() got an unexpected keyword argument 'buffering'

Tornado support

Tracking interest at the moment. If you're interested in this please give this post a ๐Ÿ‘ reaction. Don't post your ๐Ÿ‘ as comment, this makes it harder to track.

(Note: The raven-python SDK supports Tornado too)

Problem with breadcrumbs

We've just started using sentry, and we are occasionally seeing this error:

AttributeError
'list' object has no attribute 'popleft'
hub.py in add_breadcrumb at line 206
        if crumb is not None:
            scope._breadcrumbs.append(crumb)
        else:
            logger.info("before breadcrumb dropped breadcrumb (%s)", original_crumb)
        while len(scope._breadcrumbs) >= client.options["max_breadcrumbs"]:
            scope._breadcrumbs.popleft()
    def push_scope(self):
        """Pushes a new layer on the scope stack. Returns a context manager
        that should be used to pop the scope again."""
        client, scope = self._stack[-1]

And, furthermore in sentry it says:

client | <sentry_sdk.client.Client object at 0x7fd665dcbcc0>
-- | --
crumb | {category: 'httplib', data: {method: 'POST', reason: 'OK', status_code: 200, url: 'https://us-west-2.queue.amazonaws.com/'}, timestamp: datetime.datetime(2018, 9, 25, 16, 20, 20, 89233), type: 'http'}
hint | {httplib_response: <http.client.HTTPResponse object at 0x7fd645fd7e80>}
kwargs | {category: 'httplib', data: {method: 'POST', reason: 'OK', status_code: 200, url: 'https://us-west-2.queue.amazonaws.com/'}, type: 'http'}
original_crumb | {category: 'httplib', data: {method: 'POST', reason: 'OK', status_code: 200, url: 'https://us-west-2.queue.amazonaws.com/'}, timestamp: datetime.datetime(2018, 9, 25, 16, 20, 20, 89233), type: 'http'}
scope | <sentry_sdk.scope.Scope object at 0x7fd665b41d68>
self | <sentry_sdk.hub.Hub object at 0x7fd665aeb320>

Any thoughts on what is going on?
Thanks!
-- Samson

Pyramid support

Pyramid was previously supported by raven. It would be great to see support for it in this client. Is this perhaps already on the roadmap?

Also if there is any documentation on how to write an integration I might be tempted to contribute.

Attribute error

AttributeError
'Identifier' object has no attribute 'postponed_alerts_counts'
ID: 8099fd1f8815496eaa1f815657918b9d
Sept. 26, 2018, 7:37:17 p.m. UTC
Exception

AttributeError: 'Identifier' object has no attribute 'postponed_alerts_counts'
File "interfaces.py", line 43, in start
self.startInternal()
File "identifier.py", line 63, in startInternal
send_time=send_time)
File "identifier.py", line 128, in match_batch
accepted = self.correct_by_past_history(match.person_guid, confident_score)
File "identifier.py", line 91, in correct_by_past_history
self.postponed_alerts_counts[guid] += 1

Transport does not use client's hub

The default HTTP Transport lives on its own thread and will not inherit the debug settings of the hub where the message was captured. It's basically impossible to enable debugging for threaded transports unless you make your hub the main one

[Tech Support] How to use this with django?

Where exactly am I supposed to put sentry_sdk.init()?

If I put it in settings.py, I see no error messages being reported.

The docs say nothing about dajngo + new sentry-sdk, except the fact that raven one is deprecated!

Breadcrumbs type error

We're having an issue with v0.3.2 where popleft is called on a list type:

File "/srv/frontend/project/lib/python3.5/site-packages/sentry_sdk/hub.py" in add_breadcrumb
  209.             scope._breadcrumbs.popleft()

Exception Type: AttributeError at /
Exception Value: 'list' object has no attribute 'popleft'

capture WSGI-level errors

from #19

Basically if Django/Flask itself crashes (or some wsgi middleware) we won't notice.

This is slightly related to #20 because we need to wrap the WSGI app last if somebody decided to use WSGI middleware in Django.

In Flask we can wrap Flask.__call__

An unsolved question is whether we want to attach basic request info for such events, and if we're better off doing the entire request parsing in the wsgi middleware at that point.

AWS Lambda integration not working with AWS Chalice

When trying to deploy a "Chalice" serverless App, getting exception:

    File "/Users/cookie/.virtualenvs/abc/lib/python3.6/site-packages/sentry_sdk/integrations/aws_lambda.py", line 21, in install
    old_make_final_handler = lambda_bootstrap.make_final_handler
AttributeError: module '__main__' has no attribute 'make_final_handler'

sentry-python version: 0.3.9
Code in app.py:

import sentry_sdk
from sentry_sdk.integrations.aws_lambda import AwsLambdaIntegration

sentry_sdk.init(
    dsn="https://[email protected]/12345678",
    integrations=[AwsLambdaIntegration()]
)

Database connections being left open

Python: 3.6.5
Django: 2.1.2
sentry-sdk: 0.4.2

I upgraded from raven to sentry-sdk yesterday and noticed my app was now keeping database connections open rather than closing them per request.

I wrote a tiny view to test this:

def sentry_test(request):
    from django.contrib.auth.models import User
    print(User.objects.all())
    return HttpResponse("<h1>Hello</h1>")

and when GET the URL repeatedly I see the subsequent connections remain idle after the request has completed:

=> SELECT state, query FROM pg_stat_activity WHERE client_addr IS NOT NULL;
 state  | query
--------+-----------------------------------------------------
 idle   | SELECT "auth_user"."id", "auth_user"."password", ...
 active | SELECT state, query FROM pg_stat_activity WHERE client_addr IS NOT NULL;
 idle   | SELECT "auth_user"."id", "auth_user"."password", ...
 idle   | SELECT "django_migrations"."app", "django_migrations"."name" FROM "django_migrations"
 idle   | SELECT "auth_user"."id", "auth_user"."password", ...
 idle   | SELECT "auth_user"."id", "auth_user"."password", ...
 idle   | SELECT "auth_user"."id", "auth_user"."password", ...

As you can see the connection from the migrate query on startup has remained open too.

If I use raven this behaviour is not exhibited.

I suspect this won't help much, but if I use sentry-sdk and comment out:

        WSGIHandler.__call__ = sentry_patched_wsgi_handler

Then the problem doesn't manifest itself but I've basically turned off sentry at this stage.

Notes for Django

  • Probably should try to find an earlier entry point other than settings.py (but I'm out of touch w/ Django's load process)
  • sentry_sdk.init is better than init on our suggest import process

Test Plan:

  • catches wsgi error?
  • catches manage.py error?
  • catches sys.except_hook (non manage.py) error?
  • catches middleware error?
  • catches view error?

Traceback line number does not match error report line number

Pull Request #30

Sometimes an error report line is reported incorrectly

tb.tb_fame.f_lineno may differ in some way.
Must use tb.tb_lineno

sentry-sdk/event.py

class Event(Mapping):
    __slots__ = ("_data", "_exc_value")

    def __init__(self, data={}):
        self._data = {
            "event_id": uuid.uuid4().hex,
            "timestamp": datetime.datetime.utcnow(),
            "level": "error",
        }

        self._data.update(data)

        self._exc_value = None

    def set_exception(self, exc_type, exc_value, tb, with_locals):
        print("----------------- trace back --------------------")
        print("f_lineno:  " + str(tb.tb_next.tb_next.tb_frame.f_lineno))
        print("tb_lineno: " + str(tb.tb_next.tb_next.tb_lineno))
        print("f_lineno:  " + str(tb.tb_next.tb_next.tb_next.tb_frame.f_lineno))
        print("tb_lineno: " + str(tb.tb_next.tb_next.tb_next.tb_lineno))
        print("f_lineno:  " + str(tb.tb_next.tb_next.tb_next.tb_next.tb_frame.f_lineno))
        print("tb_lineno: " + str(tb.tb_next.tb_next.tb_next.tb_next.tb_lineno))
        print("f_lineno:  " + str(tb.tb_next.tb_next.tb_next.tb_next.tb_next.tb_frame.f_lineno))
        print("tb_lineno: " + str(tb.tb_next.tb_next.tb_next.tb_next.tb_next.tb_lineno))
        print("f_lineno:  " + str(tb.tb_next.tb_next.tb_next.tb_next.tb_next.tb_next.tb_frame.f_lineno))
        print("tb_lineno: " + str(tb.tb_next.tb_next.tb_next.tb_next.tb_next.tb_next.tb_lineno))
        print("f_lineno:  " + str(tb.tb_next.tb_next.tb_next.tb_next.tb_next.tb_next.tb_next.tb_frame.f_lineno))
        print("tb_lineno: " + str(tb.tb_next.tb_next.tb_next.tb_next.tb_next.tb_next.tb_next.tb_lineno))
        print("f_lineno:  " + str(tb.tb_next.tb_next.tb_next.tb_next.tb_next.tb_next.tb_next.tb_next.tb_frame.f_lineno))
        print("tb_lineno: " + str(tb.tb_next.tb_next.tb_next.tb_next.tb_next.tb_next.tb_next.tb_next.tb_lineno))
        print("----------------- trace back --------------------")
        self["exception"] = {
            "values": exceptions_from_error_tuple(exc_type, exc_value, tb, with_locals)
        }
        self._exc_value = exc_value

trace back

        File "example.py", line 128, in _get_response
          response = self.process_exception_by_middleware(e, request)
        File "example.py", line 126, in _get_response
          response = wrapped_callback(request, *callback_args, **callback_kwargs)
        File "example.py", line 54, in wrapped_view
          return view_func(*args, **kwargs)
        File "example.py", line 69, in view
          return self.dispatch(request, *args, **kwargs)
        File "example.py", line 483, in dispatch
          response = self.handle_exception(exc)
        File "example.py", line 443, in handle_exception
          self.raise_uncaught_exception(exc)
        File "example.py", line 480, in dispatch
          response = handler(request, *args, **kwargs)
        File "example.py", line 210, in post

output

----------------- trace back --------------------
f_lineno:  128
tb_lineno: 126
f_lineno:  54
tb_lineno: 54
f_lineno:  69
tb_lineno: 69
f_lineno:  483
tb_lineno: 483
f_lineno:  443
tb_lineno: 443
f_lineno:  483
tb_lineno: 480
f_lineno:  213
tb_lineno: 210
----------------- trace back --------------------

Add extra data to event or message

With raven-python we could add extra param to captureMessage. Now with sentry-python capture_message does not accept extra param.

Can we add extra data to event or message ?

Celery - double errors logged when task decorated

I have some tasks that require distributed lock mechanism and it is implemented as decorator for celery tasks:

@app.task
@celery_lock('receive_tasks', ttl=40)
def receive_tasks(ad_id):
    raise Exception('sentry')

When exception is raised i get two error events logged, one is with tags: handled=no,level=error,mechanism=celery,server_name=,transaction=.receive_tasks
and warning text:

There was 1 error encountered while processing this event
Discarded invalid parameter '' Expand

Second event is almost equal to first one, but with tags: handled=yes, level=error, logger=celery.worker.job, mechanism=logging, server_name=
And contains message from celery worker stdout:

Task .receive_tasks[f209f4c9-61cb-47b8-94ea-b15e69933897] raised unexpected: Exception('sentry',)

If i remove @celery_lock decorator from task or pass lock_id as named argument @celery_lock(lock_id='receive_tasks', ttl=40), then all works fine - just one event received and no warnings.

Seems that this problem shows up because of non-keyword decorator args.

celery_lock implemented as simple decorator:

def celery_lock(lock_id, ttl):
    def decorator(func):
        @wraps(func)
        def lock_decorator(*args, **kwargs):
            with CeleryLock(lock_id, ttl) as aq:
                if not aq:
                    return 'locked'
                return func(*args, **kwargs)
        return lock_decorator
    return decorator

Looks like the raven module still needs to be installed ?

Hello !

I just switched to using sentry-sdk instead of raven.

However I was getting the following error:

2018-10-17 13:14:54,907: Error running WSGI application
2018-10-17 13:14:54,913: ValueError: Unable to configure handler 'sentry': Cannot resolve 'raven.contrib.django.raven_compat.handlers.SentryHandler': No module named 'raven'
2018-10-17 13:14:54,915:   File "/var/www/www_mysite_com_wsgi.py", line 18, in <module>
2018-10-17 13:14:54,917:     application = get_wsgi_application()

Which was fixed by reinstalling raven via pip.

Is it intended for raven to be a requirement ? It's not mentioned in either the Python or Django related pages: https://docs.sentry.io/platforms/python/django/

Aiohttp support

I don't see a clear way to integrate with aiohttp (server) just yet. I can still use raven at present, but I imagine sentry-python/sentry-sdk is the proposed way forward longer term?

Logging customizations

In the previous version of the sdk, with django I've specialized logging handlers for sentry, this allows for example, that some specific logging always target sentry.

Now I see that only the level control if the message ends in sentry.

Is there any plan of adding the previous feature to the new client?

Django Template Tag {% sentry_public_dsn %} missing

While moving a project from raven-python to sentry-python I noticed, that there is no documentation (and maybe implementation?) for the template tag {% sentry_public_dsn %}.
It was available in raven-python - I found it to be quiet handy.
I hope I am not missing something - or is there a reason that this wasn't implemented?

Catch crashing threads

since sys.excepthook is not called for threads that crash, we should write a separate integration for this (turned on by default)

How to extract more info from django request?

The user data that's logged by default (with sending of PII enabled) looks something like

{'username': teeberg', 'ip_address': '127.0.0.1', 'email': [email protected]'}

I'm trying to add some fields to this, but there doesn't seem to be an official documented way to do this.

Is there a cleaner way than monkey-patching sentry_sdk.integrations.django._set_user_info?

Maybe the request could be sent along as another hint to before_send handlers?

Invalid project in DSN

I have a sentry server set up on premises. I did this a couple weeks ago, just before you guys updated the docs with the new unified SDK.

Now I'm updating our project (Django) to use the python sdk. The problem is that in our project in sentry, the DSN I see has this format:

http://[email protected]:9000//2

Now the problem comes when initializing the integration. This DSN format (note: the double slash there) makes the initialization fail in sentry_sdk/utils.py line 77, when int('/2') is attempted.

This is easily fixed by simply removing the extra slash, but might be a headache for anyone who doesn't notice that.

Not sure if the SDK should handle this or if this is an issue with Sentry itself, but at least this didn't use to happen with Raven.

Possible double reporting in 0.3.7

We have a simple test that verifies the Sentry SDK is capturing Flask errors because we had some configuration/PyPy issues in the past:

https://github.com/getsentry/snuba/blob/fd44ec8880240575ee2cb05bfc87e4cfd1d0e853/tests/test_api.py#L928-L936

As of this writing we don't pin the sentry-sdk version:

https://github.com/getsentry/snuba/blob/fd44ec8880240575ee2cb05bfc87e4cfd1d0e853/setup.py#L80

The test started failing because it seems that as of 0.3.7 (I tried 0.3.6 and wasn't able to reproduce the issue) the transport is being called twice for the same exception, causing the number of reported issues to be 2 instead of 1.

Maybe this is a false alarm but I think our integration is relatively simple so this may be signs of a larger issue?

CeleryIntegration is not capturing locals

sentry-sdk==0.3.9
When Exception happens inside celery task, I can see traceback in sentry, but without local variables.
As a workaround, I am capturing exception directly, in BaseTask and it works.

class BaseTask(Task):
    def on_failure(self, exc, task_id, *args, **kwargs):
        """When passing raw exception to sentry we will get local variable values instead of just message"""
        with push_scope() as scope:
            scope.set_extra('task_id', task_id)
            scope.set_extra('retries', self.request.retries)
            capture_exception(exc)
        super().on_failure(exc, task_id, *args, **kwargs)

usage: @shared_task(base=BaseTask)

In CeleryIntegration def _process_failure_signal(self, sender, task_id, einfo, **kw): gets einfo, so I don't know why it's not working out of the box.

Getting Attribute Error 'list' object has no attribute 'popleft' in hub.py add_breadcrumb

I have followed installation instructions mentioned in this page
https://docs.sentry.io/quickstart?platform=python

After a while, sentry fails and gives this error message.

File "/app/.heroku/python/lib/python3.6/site-packages/sentry_sdk/api.py", line 91, in add_breadcrumb
   return hub.add_breadcrumb(*args, **kwargs)
File "/app/.heroku/python/lib/python3.6/site-packages/sentry_sdk/hub.py", line 206, in add_breadcrumb
   scope._breadcrumbs.popleft()
AttributeError: 'list' object has no attribute 'popleft'

https://github.com/getsentry/sentry-python/blob/master/sentry_sdk/hub.py#L209

ModuleNotFoundError: No module named 'runtime'

When trying to import AwsLambdaIntegration I get following Error:

ModuleNotFoundError: No module named 'runtime'

Traceback:

ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-1-f0a35f42fdce> in <module>()
----> 1 from sentry_sdk.integrations.aws_lambda import AwsLambdaIntegration

~/Envs/gds/lib/python3.6/site-packages/sentry_sdk/integrations/aws_lambda.py in <module>()
     13 
     14 import __main__ as lambda_bootstrap
---> 15 import runtime as lambda_runtime
     16 
     17 

ModuleNotFoundError: No module named 'runtime'

Version:

sentry-sdk==0.3.9

I tried install runtime package, but it didn't help, because then I get:

~/Envs/gds/lib/python3.6/site-packages/runtime.py in <module>()
     26         from thread import get_ident
     27     except ImportError:  # pragma: no cover
---> 28         from dummy_thread import get_ident
     29 
     30 __all__ = (

ModuleNotFoundError: No module named 'dummy_thread'

And pip install thread returns:

No matching distribution found for thread

Please, fix AttributeError: 'list' object has no attribute 'popleft'

Python 3.7.0, sentry_sdk 0.3.2

2018-09-21 18:08:21,575 DEBUG Internal error in sentry_sdk
Traceback (most recent call last):
  File "/home/dmugtasimov/.virtualenvs/packy-agent2-eBU3dgqa/lib/python3.7/site-packages/sentry_sdk/utils.py", line 28, in capture_internal_exceptions
    yield
  File "/home/dmugtasimov/.virtualenvs/packy-agent2-eBU3dgqa/lib/python3.7/site-packages/sentry_sdk/integrations/logging.py", line 102, in _emit
    self._breadcrumb_from_record(record), hint={"log_record": record}
  File "/home/dmugtasimov/.virtualenvs/packy-agent2-eBU3dgqa/lib/python3.7/site-packages/sentry_sdk/hub.py", line 209, in add_breadcrumb
    scope._breadcrumbs.popleft()
AttributeError: 'list' object has no attribute 'popleft'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.