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


##########
airflow/providers/openlineage/plugins/adapter.py:
##########
@@ -0,0 +1,302 @@
+# 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
+import uuid
+from typing import TYPE_CHECKING
+
+import requests.exceptions
+
+from airflow.providers.openlineage import version as 
OPENLINEAGE_PROVIDER_VERSION
+from airflow.providers.openlineage.extractors import OperatorLineage
+from airflow.providers.openlineage.utils import redact_with_exclusions
+from airflow.utils.log.logging_mixin import LoggingMixin
+from openlineage.client import OpenLineageClient, set_producer
+from openlineage.client.facet import (
+    BaseFacet,
+    DocumentationJobFacet,
+    ErrorMessageRunFacet,
+    NominalTimeRunFacet,
+    OwnershipJobFacet,
+    OwnershipJobFacetOwners,
+    ParentRunFacet,
+    ProcessingEngineRunFacet,
+    SourceCodeLocationJobFacet,
+)
+from openlineage.client.run import Job, Run, RunEvent, RunState
+
+if TYPE_CHECKING:
+    from airflow.models.dagrun import DagRun
+
+
+_DAG_DEFAULT_NAMESPACE = "default"
+
+_DAG_NAMESPACE = os.getenv("OPENLINEAGE_NAMESPACE", _DAG_DEFAULT_NAMESPACE)
+
+_PRODUCER = f"https://github.com/apache/airflow/tree/providers-openlineage/"; 
f"{OPENLINEAGE_PROVIDER_VERSION}"
+
+set_producer(_PRODUCER)
+
+
+class OpenLineageAdapter(LoggingMixin):
+    """
+    Adapter for translating Airflow metadata to OpenLineage events,
+    instead of directly creating them from Airflow code.
+    """
+
+    def __init__(self, client=None):
+        super().__init__()
+        self._client = client
+
+    def get_or_create_openlineage_client(self) -> OpenLineageClient:
+        if not self._client:
+            self._client = OpenLineageClient.from_environment()
+        return self._client
+
+    def build_dag_run_id(self, dag_id, dag_run_id):
+        return str(uuid.uuid3(uuid.NAMESPACE_URL, 
f"{_DAG_NAMESPACE}.{dag_id}.{dag_run_id}"))
+
+    @staticmethod
+    def build_task_instance_run_id(task_id, execution_date, try_number):
+        return str(
+            uuid.uuid3(
+                uuid.NAMESPACE_URL,
+                f"{_DAG_NAMESPACE}.{task_id}.{execution_date}.{try_number}",
+            )
+        )
+
+    def emit(self, event: RunEvent):
+        event = redact_with_exclusions(event)
+        try:
+            return self.get_or_create_openlineage_client().emit(event)
+        except requests.exceptions.RequestException:
+            self.log.exception(f"Failed to emit OpenLineage event of id 
{event.run.runId}")
+
+    def start_task(
+        self,
+        run_id: str,
+        job_name: str,
+        job_description: str,
+        event_time: str,
+        parent_job_name: str | None,
+        parent_run_id: str | None,
+        code_location: str | None,
+        nominal_start_time: str,
+        nominal_end_time: str,
+        owners: list[str],
+        task: OperatorLineage | None,
+        run_facets: dict[str, type[BaseFacet]] | None = None,  # Custom run 
facets
+    ) -> str:
+        """
+        Emits openlineage event of type START

Review Comment:
   Done.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to