getsentry / raven-python

Raven is the legacy Python client for Sentry (getsentry.com) — replaced by sentry-python
https://sentry.io
BSD 3-Clause "New" or "Revised" License
1.68k stars 657 forks source link

Setting transport class in logging dict config #1199

Open aspyatkin opened 6 years ago

aspyatkin commented 6 years ago

Hello, I have encountered a problem after upgrading from raven 5.x and not sure how to deal with it better.

In older raven versions it was allowed to set Transport in DSN parameter. Since my applications make use of logging dict config, I used to set Transport there. From now on it produces a warning Transport selection via DSN is deprecated. I was not able to set Transport class explicitly in logging dict config:

handlers:
  default:
    level: DEBUG
    class: logging.StreamHandler
    formatter: default
    stream: ext://sys.stdout
  sentry:
    level: INFO
    class: raven.handlers.logging.SentryHandler
    dsn: http://*****:*****@sentry.*****.local/11
    transport: raven.transport.threaded_requests.ThreadedRequestsHTTPTransport
    environment: development

Judging by the code, SentryHandler won't be instantiated if transport is of type str and not callable. I wonder if there is an other way to set Transport class in logging dict config? Might an extra parameter transport_class be implemented in next versions so as to instruct SentryHandler to load the desired type of Transport by its string identifier?

aspyatkin commented 6 years ago

This code block in RemoteConfig __init__ seems to solve the problem, but I guess it only works in Python 2.7:

        self.store_endpoint = store_endpoint

        # begin
        if isinstance(transport, basestring):
            transport_class_module = '.'.join(transport.split('.')[0:-1])
            transport_module = importlib.import_module(transport_class_module)
            transport_class_name = transport.split('.')[-1]
            if hasattr(transport_module, transport_class_name):
                transport = getattr(transport_module, transport_class_name)
        # end

        self._transport_cls = transport or DEFAULT_TRANSPORT