Пример #1
0
 def _fix_sentry_scope(self):
     hub = sentry_sdk.Hub.current
     with sentry_sdk.Hub(hub) as hub:
         with hub.configure_scope() as scope:
             scope.clear_breadcrumbs()
             scope.add_event_processor(self._make_sentry_event_processor())
             yield
Пример #2
0
async def webhook_event_consumer(*, connection: RedisConnection,
                                 webhook_queue: RedisWebhookQueue,
                                 queue_name: str) -> typing.NoReturn:
    """
    Worker to process incoming webhook events from redis

    1. process mergeability information and update github check status for pr
    2. enqueue pr into repo queue for merging, if mergeability passed
    """

    # We need to define a custom Hub so that we can set the scope correctly.
    # Without creating a new hub we end up overwriting the scopes of other
    # consumers.
    #
    # https://github.com/getsentry/sentry-python/issues/147#issuecomment-432959196
    # https://github.com/getsentry/sentry-python/blob/0da369f839ee2c383659c91ea8858abcac04b869/sentry_sdk/integrations/aiohttp.py#L80-L83
    # https://github.com/getsentry/sentry-python/blob/464ca8dda09155fcc43dfbb6fa09cf00313bf5b8/sentry_sdk/integrations/asgi.py#L90-L113
    with sentry_sdk.Hub(sentry_sdk.Hub.current) as hub:
        with hub.configure_scope() as scope:
            scope.set_tag("queue", queue_name)
        log = logger.bind(queue=queue_name)
        log.info("start webhook event consumer")
        while True:
            await process_webhook_event(connection, webhook_queue, queue_name,
                                        log)
Пример #3
0
def _create_noop_hub():
    def transport(event):
        with capture_internal_exceptions():
            metrics.incr('internal.uncaptured.events', skip_internal=False)
            sdk_logger.warn('internal-error.noop-hub')

    return sentry_sdk.Hub(sentry_sdk.Client(transport=transport))
Пример #4
0
 def capture_event(self, event):
     # Disable the SDK while processing our own events. This fixes some
     # recursion issues when the view crashes without including any
     # UNSAFE_FILES
     #
     # NOTE: UNSAFE_FILES still exists because the hub does not follow the
     # execution flow into the celery job triggered by StoreView. In other
     # words, UNSAFE_FILES is used in case the celery job for crashes and
     # that error is captured by the SDK.
     with sentry_sdk.Hub(NOOP_HUB):
         return self._capture_event(event)
Пример #5
0
 async def __call__(self, scope, receive, send):
     hub = sentry_sdk.Hub.current
     with sentry_sdk.Hub(hub) as hub:
         with hub.configure_scope() as sentry_scope:
             processor = functools.partial(self.event_processor,
                                           asgi_scope=scope)
             sentry_scope.add_event_processor(processor)
             try:
                 await self.app(scope, receive, send)
             except Exception as exc:
                 hub.capture_exception(exc)
                 raise exc from None
Пример #6
0
def get_exception_event():
    event = {}

    def transport(e):
        nonlocal event
        event = e

    client = sentry_sdk.Client(transport=transport)
    hub = sentry_sdk.Hub(client)
    hub.capture_exception()

    assert event
    return event
Пример #7
0
def capsentry():
    events = []

    # This hub context isolates us from the "real" global one
    with sentry_sdk.Hub():

        def _collect_events(event):
            events.append(event)

        config = build_sentry_configuration(dsn="whatever", environment="ci")
        sentry_sdk.init(**config, transport=_collect_events)

        yield events
Пример #8
0
    def inner(event):
        if not SEMAPHORE:
            return

        # Disable subprocess integration
        with sentry_sdk.Hub(None):
            # not dealing with the subprocess API right now
            file = tmpdir.join("event")
            file.write(json.dumps(dict(event)))
            output = json.loads(
                subprocess.check_output([SEMAPHORE, "process-event"],
                                        stdin=file.open()).decode("utf-8"))
            _no_errors_in_semaphore_response(output)
            output.pop("_meta", None)
Пример #9
0
def sentry_init(monkeypatch_test_transport, request):
    def inner(*a, **kw):
        hub = sentry_sdk.Hub.current
        client = sentry_sdk.Client(*a, **kw)
        hub.bind_client(client)
        monkeypatch_test_transport(sentry_sdk.Hub.current.client)

    if request.node.get_closest_marker("forked"):
        # Do not run isolation if the test is already running in
        # ultimate isolation (seems to be required for celery tests that
        # fork)
        yield inner
    else:
        with sentry_sdk.Hub(None):
            yield inner
Пример #10
0
    async def __call__(self, scope: Scope, receive: Receive,
                       send: Send) -> NoReturn:
        hub = sentry_sdk.Hub.current
        with sentry_sdk.Hub(hub) as hub:
            with hub.configure_scope() as sentry_scope:
                processor = functools.partial(self.__event_processor,
                                              scope=scope)
                sentry_scope.add_event_processor(processor)

                with sentry_sdk.start_transaction():
                    try:
                        await self.app(scope, receive, send)
                    except Exception as exc:
                        hub.capture_exception(exc)
                        raise exc from None
Пример #11
0
    def inner(event):
        if not SENTRY_RELAY:
            return

        # Disable subprocess integration
        with sentry_sdk.Hub(None):
            # not dealing with the subprocess API right now
            file = tmpdir.join("event-{}".format(uuid.uuid4().hex))
            file.write(json.dumps(dict(event)))
            with file.open() as f:
                output = json.loads(
                    subprocess.check_output([SENTRY_RELAY, "process-event"],
                                            stdin=f).decode("utf-8"))
            _no_errors_in_relay_response(output)
            output.pop("_meta", None)
            return output
Пример #12
0
async def repo_queue_consumer(*, queue_name: str,
                              connection: RedisConnection) -> typing.NoReturn:
    """
    Worker for a repo given by :queue_name:

    Pull webhook events off redis queue and process for mergeability.

    We only run one of these per repo as we can only merge one PR at a time
    to be efficient. This also alleviates the need of locks.
    """
    with sentry_sdk.Hub(sentry_sdk.Hub.current) as hub:
        with hub.configure_scope() as scope:
            scope.set_tag("queue", queue_name)
        log = logger.bind(queue=queue_name)
        log.info("start repo_consumer")
        while True:
            await process_repo_queue(log, connection, queue_name)
Пример #13
0
        passed=passed,
        messages=messages,
        awaiting_input=awaiting_input,
        output=output,
        output_parts=output_parts,
        birdseye_objects=birdseye_objects,
        error=error,
    )
    # Check that JSON encoding works here
    # because failures in the queue pickling are silent
    json_pickler.dumps(result)
    return result


# Import eagerly
sentry_sdk.Hub(sentry_sdk.Client(transport=lambda e: None))


def get_exception_event():
    event = {}

    def transport(e):
        nonlocal event
        event = e

    client = sentry_sdk.Client(transport=transport)
    hub = sentry_sdk.Hub(client)
    hub.capture_exception()

    assert event
    return event
Пример #14
0
    client_id_file_path = util.cli_config_dir() / "client-id.txt"
    if not client_id_file_path.is_file():
        client_id_file_path.parent.mkdir(parents=True, exist_ok=True)
        client_id_file_path.write_text(str(uuid4()))
    user_id = client_id_file_path.read_text()

    if user_id:
        scope.set_user({"id": user_id})

    for k, v in optional_tags.items():
        scope.set_tag(k, v)

    return scope


# only one instance of this is required
hub = sentry_sdk.Hub(_sentry_client(), _create_default_scope())


def sentry_wrapper(func):
    def wrapper(*args, **kwargs):
        with hub:
            try:
                return func(*args, **kwargs)
            except:
                sentry_sdk.capture_exception()
                sentry_sdk.flush()
                raise

    return wrapper
Пример #15
0
import inspect
from types import ModuleType
from typing import Any, Callable, Dict, List, Optional

import sentry_sdk
import wrapt
from sentry_sdk.integrations import aws_lambda

from . import config

# https://docs.sentry.io/error-reporting/configuration/?platform=python#common-options
sentry_client = sentry_sdk.Hub(
    sentry_sdk.Client(
        dsn=
        "https://[email protected]/5469393",
        release=f"sentiment_flanders@{config.__version__}",
        environment=config.get_workspace(),
        traces_sample_rate=1.0,
        # https://github.com/getsentry/sentry-python/issues/227
        integrations=[aws_lambda.AwsLambdaIntegration()],
    ))


@wrapt.decorator
def log_function_with_sentry(wrapped: Callable[..., Any], instance: Any,
                             args: List[Any], kwargs: Dict[str, Any]) -> Any:
    """Attaches Sentry integrations to a function."""
    with sentry_client:
        try:
            return wrapped(*args, **kwargs)
        except Exception as e:
            sentry_sdk.capture_exception(e)