diff --git a/swh/loader/core/loader.py b/swh/loader/core/loader.py --- a/swh/loader/core/loader.py +++ b/swh/loader/core/loader.py @@ -38,6 +38,9 @@ "max_content_size": 100 * 1024 * 1024, } +SENTRY_ORIGIN_URL_TAG_NAME = "swh.loader.origin_url" +SENTRY_VISIT_TYPE_TAG_NAME = "swh.loader.visit_type" + class BaseLoader: """Base class for (D)VCS loaders (e.g Svn, Git, Mercurial, ...) or PackageLoader (e.g @@ -116,6 +119,9 @@ _log = logging.getLogger("requests.packages.urllib3.connectionpool") _log.setLevel(logging.WARN) + sentry_sdk.set_tag(SENTRY_ORIGIN_URL_TAG_NAME, self.origin.url) + sentry_sdk.set_tag(SENTRY_VISIT_TYPE_TAG_NAME, self.visit_type) + # possibly overridden in self.prepare method self.visit_date = datetime.datetime.now(tz=datetime.timezone.utc) diff --git a/swh/loader/core/tests/test_loader.py b/swh/loader/core/tests/test_loader.py --- a/swh/loader/core/tests/test_loader.py +++ b/swh/loader/core/tests/test_loader.py @@ -11,7 +11,12 @@ import pytest -from swh.loader.core.loader import BaseLoader, DVCSLoader +from swh.loader.core.loader import ( + SENTRY_ORIGIN_URL_TAG_NAME, + SENTRY_VISIT_TYPE_TAG_NAME, + BaseLoader, + DVCSLoader, +) from swh.loader.core.metadata_fetchers import MetadataFetcherProtocol from swh.loader.exception import NotFound from swh.loader.tests import assert_last_visit_matches @@ -479,3 +484,22 @@ assert result == {"status": "uneventful"} _check_load_failure(caplog, loader, NotFound, "Unknown origin!", status="not_found") + + +class DummyLoaderWithError(DummyBaseLoader): + def prepare(self, *args, **kwargs): + raise Exception("error") + + +class DummyDVCSLoaderWithError(DummyDVCSLoader, BaseLoader): + def prepare(self, *args, **kwargs): + raise Exception("error") + + +@pytest.mark.parametrize("loader_cls", [DummyLoaderWithError, DummyDVCSLoaderWithError]) +def test_loader_sentry_tags_on_error(swh_storage, sentry_events, loader_cls): + loader = loader_cls(swh_storage) + loader.load() + sentry_tags = sentry_events[0]["tags"] + assert sentry_tags.get(SENTRY_ORIGIN_URL_TAG_NAME) == ORIGIN.url + assert sentry_tags.get(SENTRY_VISIT_TYPE_TAG_NAME) == DummyLoader.visit_type diff --git a/swh/loader/package/loader.py b/swh/loader/package/loader.py --- a/swh/loader/package/loader.py +++ b/swh/loader/package/loader.py @@ -531,6 +531,7 @@ # Prepare origin and origin_visit origin = Origin(url=self.origin.url) + try: self.storage.origin_add([origin]) visit = list( diff --git a/swh/loader/package/tests/test_loader.py b/swh/loader/package/tests/test_loader.py --- a/swh/loader/package/tests/test_loader.py +++ b/swh/loader/package/tests/test_loader.py @@ -12,6 +12,10 @@ import attr import pytest +from swh.loader.core.loader import ( + SENTRY_ORIGIN_URL_TAG_NAME, + SENTRY_VISIT_TYPE_TAG_NAME, +) from swh.loader.package.loader import BasePackageInfo, PackageLoader from swh.model.model import ( Origin, @@ -518,3 +522,20 @@ AssertionError, match="SWH_CONFIG_FILENAME environment variable is undefined" ): DummyPackageLoader.from_configfile(url="some-url") + + +class StubPackageLoaderWithError(StubPackageLoader): + def get_versions(self, *args, **kwargs): + raise Exception("error") + + +def test_loader_sentry_tags_on_error(swh_storage, sentry_events): + origin_url = "http://example.org/package/name" + loader = StubPackageLoaderWithError(swh_storage, origin_url) + loader.load() + sentry_tags = sentry_events[0]["tags"] + assert sentry_tags.get(SENTRY_ORIGIN_URL_TAG_NAME) == origin_url + assert ( + sentry_tags.get(SENTRY_VISIT_TYPE_TAG_NAME) + == StubPackageLoaderWithError.visit_type + )