Module scrapfly.reporter

Sub-modules

scrapfly.reporter.sentry

Sentry integration for the Scrapfly Python SDK …

Classes

class ChainReporter (*args: Tuple[Callable])
Expand source code
class ChainReporter:
    reporters: Tuple[Callable]

    def __init__(self, *args:Tuple[Callable]):
        self.reporters = args

    def __call__(self, error:Optional[Exception]=None, scrape_api_response:Optional[ScrapeApiResponse]=None):
        [reporter(error, scrape_api_response) for reporter in self.reporters]

Class variables

var reporters : Tuple[Callable]
class NoopReporter
Expand source code
class NoopReporter:

    def __call__(self, error:Optional[Exception]=None, scrape_api_response:Optional[ScrapeApiResponse]=None):
        pass
class PrintReporter
Expand source code
class PrintReporter:

    def __call__(self, error:Optional[Exception]=None, scrape_api_response:Optional[ScrapeApiResponse]=None):
        debug_data = {
            'scrape_config': None,
            'log_url': None,
            'scrape_error': None,
            'error': None
        }

        if scrape_api_response:
            debug_data['scrape_config'] = scrape_api_response.config

            if scrape_api_response.error is not None:
                debug_data['scrape_error'] = scrape_api_response.error

            if scrape_api_response.scrape_result:
                debug_data['log_url'] = scrape_api_response.scrape_result['log_url']

        if error is not None:
            debug_data['error'] = {
                'message': str(error),
                'type': error.__class__.__name__
            }

        pprint(debug_data)
class SentryReporter
Expand source code
class SentryReporter:
    """
    A reporter callable that forwards Scrapfly errors to Sentry with
    contextual tags and extras.

    Raises:
        ImportError: If ``sentry-sdk`` is not installed when the reporter
            is instantiated. Install with ``pip install sentry-sdk``.
    """

    def __init__(self):
        try:
            import sentry_sdk  # noqa: F401  # validate the dependency is present
        except ImportError as e:
            raise ImportError(
                "SentryReporter requires the 'sentry-sdk' package. "
                "Install it with: pip install sentry-sdk"
            ) from e

    def __call__(
        self,
        error: Optional[Exception] = None,
        scrape_api_response: Optional['ScrapeApiResponse'] = None,  # noqa: F821
    ):
        # Late import — already validated in __init__, safe to import here.
        from sentry_sdk import capture_exception, push_scope

        with push_scope() as scope:
            if scrape_api_response is not None:
                scope.set_tag('scrapfly_project', scrape_api_response.config['project'])
                scope.set_tag('scrapfly_env', scrape_api_response.config['env'])
                scope.set_extra('scrape_config', scrape_api_response.config)

                if scrape_api_response.scrape_result:
                    scope.set_extra('log_url', scrape_api_response.scrape_result['log_url'])
                    scope.set_extra('upstream_url', scrape_api_response.scrape_result['url'])
                    scope.set_tag(
                        'scrapfly_upstream_status_code',
                        scrape_api_response.scrape_result['status_code'],
                    )

                if scrape_api_response.error is not None:
                    scope.set_tag('scrapfly_error_code', scrape_api_response.error['code'])

            if error is not None:
                capture_exception(error)

A reporter callable that forwards Scrapfly errors to Sentry with contextual tags and extras.

Raises

ImportError
If sentry-sdk is not installed when the reporter is instantiated. Install with pip install sentry-sdk.