mobuchowski commented on code in PR #29940:
URL: https://github.com/apache/airflow/pull/29940#discussion_r1157614375


##########
airflow/providers/openlineage/extractors/manager.py:
##########
@@ -0,0 +1,150 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+from __future__ import annotations
+
+import os
+
+from airflow.providers.openlineage.extractors import BaseExtractor, 
OperatorLineage
+from airflow.providers.openlineage.extractors.base import DefaultExtractor
+from airflow.providers.openlineage.plugins.facets import (
+    UnknownOperatorAttributeRunFacet,
+    UnknownOperatorInstance,
+)
+from airflow.providers.openlineage.utils.utils import get_operator_class
+from airflow.utils.log.logging_mixin import LoggingMixin
+
+
+class ExtractorManager(LoggingMixin):
+    """Class abstracting management of custom extractors."""
+
+    def __init__(self):
+        super().__init__()
+        self.extractors: dict[str, type[BaseExtractor]] = {}
+        self.default_extractor = DefaultExtractor
+
+        # Comma-separated extractors in OPENLINEAGE_EXTRACTORS variable.
+        # Extractors should implement BaseExtractor
+        from airflow.providers.openlineage.utils import import_from_string
+
+        # TODO: use airflow config with OL backup
+        env_extractors = os.getenv("OPENLINEAGE_EXTRACTORS")
+        if env_extractors is not None:
+            for extractor in env_extractors.split(";"):
+                extractor: type[BaseExtractor] = 
import_from_string(extractor.strip())
+                for operator_class in extractor.get_operator_classnames():
+                    self.extractors[operator_class] = extractor
+
+    def add_extractor(self, operator: str, extractor: type[BaseExtractor]):
+        self.extractors[operator] = extractor
+
+    def extract_metadata(self, dagrun, task, complete: bool = False, 
task_instance=None) -> OperatorLineage:
+        extractor = self._get_extractor(task)
+        task_info = (
+            f"task_type={task.task_type} "
+            f"airflow_dag_id={task.dag_id} "
+            f"task_id={task.task_id} "
+            f"airflow_run_id={dagrun.run_id} "
+        )
+
+        if extractor:
+            # Extracting advanced metadata is only possible when extractor for 
particular operator
+            # is defined. Without it, we can't extract any input or output 
data.
+            try:
+                self.log.debug("Using extractor %s %s", 
extractor.__class__.__name__, str(task_info))
+                if complete:
+                    task_metadata = 
extractor.extract_on_complete(task_instance)
+                else:
+                    task_metadata = extractor.extract()
+
+                self.log.debug("Found task metadata for operation %s: %s", 
task.task_id, str(task_metadata))
+                if task_metadata:
+                    if (not task_metadata.inputs) and (not 
task_metadata.outputs):
+                        inlets = task.get_inlet_defs()
+                        outlets = task.get_outlet_defs()
+                        self.extract_inlets_and_outlets(task_metadata, inlets, 
outlets)
+
+                    return task_metadata
+
+            except Exception as e:
+                self.log.exception(
+                    "Failed to extract metadata using found extractor %s - %s 
%s", extractor, e, task_info
+                )
+        else:
+            self.log.debug("Unable to find an extractor %s", task_info)
+
+            # Only include the unkonwnSourceAttribute facet if there is no 
extractor
+            task_metadata = OperatorLineage(
+                run_facets={
+                    "unknownSourceAttribute": UnknownOperatorAttributeRunFacet(
+                        unknownItems=[
+                            UnknownOperatorInstance(
+                                name=get_operator_class(task).__name__,
+                                properties={attr: value for attr, value in 
task.__dict__.items()},
+                            )
+                        ]
+                    )
+                },
+            )
+            inlets = task.get_inlet_defs()
+            outlets = task.get_outlet_defs()

Review Comment:
   Fixed.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@airflow.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to