Openverse is a search engine for openly-licensed media. This monorepo includes all application code.
Add verbose logging option to `ProviderDataIngester` (#4068)
Nico Eppa8a2e7633ba5
diff --git a/catalog/dags/providers/provider_api_scripts/provider_data_ingester.py b/catalog/dags/providers/provider_api_scripts/provider_data_ingester.pyindex 13c9703bb..713b1399d 100644--- a/catalog/dags/providers/provider_api_scripts/provider_data_ingester.py+++ b/catalog/dags/providers/provider_api_scripts/provider_data_ingester.py@@ -196,6 +196,13 @@ class ProviderDataIngester(ABC):self.ingestion_errors: list[IngestionError] = [] # Keep track of skipped errors+ environment = Variable.get("ENVIRONMENT", default_var="local")+ self._should_verbose_log = (+ self.dag_id+ in Variable.get("SHOULD_VERBOSE_LOG", default_var=[], deserialize_json=True)+ or environment == "local"+ )+def _init_media_stores(self, day_shift: int = None) -> dict[str, MediaStore]:"""Initialize a media store for each media type supported by this provider."""@@ -243,6 +250,8 @@ class ProviderDataIngester(ABC):logger.info(f"Begin ingestion for {self.__class__.__name__}")while should_continue:+ self._verbose_log(f"Next set of query params: {query_params}")+if query_params is None:# Break out of ingestion if no query_params are supplied. This can# happen when the final `override_query_params` is processed.@@ -477,6 +486,12 @@ class ProviderDataIngester(ABC):# Build a list of records from the responsebatch = self.get_batch_data(response_json)+ self._verbose_log(+ f"Got batch with {len(batch if batch else [])} items. The first items are:",+ batch,+ limit_batch_to=5,+ )+# Optionally, apply some logic to the response to determine whether# ingestion should continue or if should be short-circuited. By default# this will return True and ingestion continues.@@ -528,6 +543,7 @@ class ProviderDataIngester(ABC):for data in media_batch:if not (record_data := self.get_record_data(data)):+ self._verbose_log("No data could be processed from this batch item.")continuerecord_data = (@@ -538,6 +554,11 @@ class ProviderDataIngester(ABC):])+ if len(record_data) > 1:+ self._verbose_log(+ f"{len(record_data)} records were processed from this batch item."+ )+for record in record_data:# We need to know what type of record we're handling in# order to add it to the correct store@@ -552,6 +573,8 @@ class ProviderDataIngester(ABC):logger.info("Ingestion limit has been reached. Halting processing.")return processed_count+ self._verbose_log(f"{processed_count} records where processed in this batch.")+return processed_countdef get_media_type(self, record: dict) -> str:@@ -587,3 +610,12 @@ class ProviderDataIngester(ABC):total += store.commit()logger.info(f"Committed {total} records")return total++ def _verbose_log(self, msg: str, data: list = None, limit_batch_to: int = None):+ if not self._should_verbose_log:+ return+ if data and not isinstance(data, dict):+ if limit_batch_to is not None:+ data = data[:limit_batch_to]+ msg += "".join(f"\n\t{json.dumps(item)}" for item in data)+ logger.info(msg)diff --git a/catalog/tests/dags/providers/provider_api_scripts/test_provider_data_ingester.py b/catalog/tests/dags/providers/provider_api_scripts/test_provider_data_ingester.pyindex 0b9655336..624741ef4 100644--- a/catalog/tests/dags/providers/provider_api_scripts/test_provider_data_ingester.py+++ b/catalog/tests/dags/providers/provider_api_scripts/test_provider_data_ingester.py@@ -109,6 +109,8 @@ def test_batch_limit_is_capped_to_ingestion_limit():MockVariable.get.side_effect = [20, # ingestion_limit{}, # skipped_ingestion_errors+ "local", # environment+ [], # should_verbose_log]ingester = MockProviderDataIngester()@@ -300,6 +302,8 @@ def test_ingest_records_stops_after_reaching_limit():MockVariable.get.side_effect = [3, # ingestion_limit{}, # skipped_ingestion_errors+ "local", # environment+ [], # should_verbose_log]ingester = MockProviderDataIngester()@@ -665,6 +669,8 @@ def test_should_skip_ingestion_error(MockVariable.get.side_effect = [None, # ingestion_limit{"my_dag_id": skipped_ingestion_errors}, # skipped_ingestion_errors+ "local", # environment+ [], # should_verbose_log]ingester = MockProviderDataIngester(@@ -687,6 +693,8 @@ def test_ingest_records_exits_immediately_if_limit_already_reached():MockVariable.get.side_effect = [5, # ingestion_limit{}, # skipped_ingestion_errors+ "local", # environment+ [], # should_verbose_log]ingester = MockProviderDataIngester()diff --git a/catalog/variables.tsv b/catalog/variables.tsvindex 3b8dfe34a..41a16bef3 100644--- a/catalog/variables.tsv+++ b/catalog/variables.tsv@@ -2,3 +2,4 @@ key default descriptionSILENCED_SLACK_NOTIFICATIONS {} Configuration for a silencing Slack notifications from a DAG. Mapping of DAG ID to a list of dictionaries containing the following keys: "issue" (a link to a GitHub issue which describes why the notification is silenced and tracks resolving the problem), "predicate" (Slack notifications whose text or username contain the predicate will be silenced, matching is case-insensitive), and "task_id_pattern" (a regex pattern that matches the task_id of the task that triggered the notification, optional). Declaration: https://github.com/WordPress/openverse/blob/d500b7764c411f7d228ae12c57dce519c8709610/catalog/dags/common/slack.py#L72-L86 Example: { "finnish_museums_workflow": [ { "issue": "https://github.com/WordPress/openverse/issues/1605", "predicate": "AirflowTaskTimeout", "task_id_pattern": "clean_data" } ] }SKIPPED_INGESTION_ERRORS {} Configuration for silencing an ingestion error and preventing a Slack message from being sent. Mapping of DAG ID to a list of dictionaries containing the following keys: "issue" (a link to a GitHub issue which describes why the error is silenced and tracks resolving the problem), and "predicate" (errors whose classname or message contain the predicate will be skipped, matching is case-insensitive). Declaration: https://github.com/WordPress/openverse/blob/6636dcfbb57abca19ef32027975f78548e10411f/catalog/dags/providers/provider_api_scripts/provider_data_ingester.py#L53-L64 Example: { "science_museum_workflow": [ { "issue": "https://github.com/WordPress/openverse/issues/4013", "predicate": "Service unavailable for url" } ] }CONFIGURATION_OVERRIDES {} DAG configuration overrides for the provider ingestion workflows. Currently only supports overriding the execution timeout for certain tasks, but allows dynamic overrides at DAG run time. Mapping of DAG ID to a list of dictionaries containing the following keys: "task_id" (a regex pattern that matches the task_id of the task to be modified), and "timeout" (str in "%d:%H:%M:%S" format giving the amount of time the task may take, example: 6d:10h:30m). Declaration: https://github.com/WordPress/openverse/blob/2cffcb9f8da6961e84a00854a3cd472fd0f9dad8/catalog/dags/providers/provider_workflows.py#L42-L58 Example: { "brooklyn_museum_workflow": [ { "task_id_pattern": "pull_image_data", "timeout": "10h" } ] }+SHOULD_VERBOSE_LOG [] Configuration for whether a provider ingestion workflow should log verbosely. This is a list of DAG IDs that should log at a more verbose level. Example: ["brooklyn_museum_workflow", "smk_workflow"]