Source code for airflow.providers.dbt.cloud.utils.openlineage
# 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__importannotationsimportasyncioimportrefromcontextlibimportsuppressfromtypingimportTYPE_CHECKINGfromairflowimport__version__asairflow_versionifTYPE_CHECKING:fromairflow.models.taskinstanceimportTaskInstancefromairflow.providers.dbt.cloud.operators.dbtimportDbtCloudRunJobOperatorfromairflow.providers.dbt.cloud.sensors.dbtimportDbtCloudJobRunSensorfromairflow.providers.openlineage.extractors.baseimportOperatorLineagedef_get_try_number(val):# todo: remove when min airflow version >= 2.10.0frompackaging.versionimportparseifparse(parse(airflow_version).base_version)<parse("2.10.0"):returnval.try_number-1else:returnval.try_number
[docs]defgenerate_openlineage_events_from_dbt_cloud_run(operator:DbtCloudRunJobOperator|DbtCloudJobRunSensor,task_instance:TaskInstance)->OperatorLineage:""" Generate OpenLineage events from the DBT Cloud run. This function retrieves information about a DBT Cloud run, including the associated job, project, and execution details. It processes the run's artifacts, such as the manifest and run results, in parallel for many steps. Then it generates and emits OpenLineage events based on the executed DBT tasks. :param operator: Instance of DBT Cloud operator that executed DBT tasks. It already should have run_id and dbt cloud hook. :param task_instance: Currently executed task instance :return: An empty OperatorLineage object indicating the completion of events generation. """fromopenlineage.common.provider.dbtimportDbtCloudArtifactProcessor,ParentRunMetadatatry:fromairflow.providers.openlineage.confimportnamespaceexceptModuleNotFoundErrorase:fromairflow.exceptionsimportAirflowOptionalProviderFeatureExceptionmsg="Please install `apache-airflow-providers-openlineage>=1.7.0`"raiseAirflowOptionalProviderFeatureException(e,msg)fromairflow.providers.openlineage.extractorsimportOperatorLineagefromairflow.providers.openlineage.plugins.adapterimport(_PRODUCER,OpenLineageAdapter,)fromairflow.providers.openlineage.plugins.listenerimportget_openlineage_listener# if no account_id set this will fallbackjob_run=operator.hook.get_job_run(run_id=operator.run_id,account_id=operator.account_id,include_related=["run_steps,job"]).json()["data"]job=job_run["job"]# retrieve account_id from job and use that starting from this lineaccount_id=job["account_id"]project=operator.hook.get_project(project_id=job["project_id"],account_id=account_id).json()["data"]connection=project["connection"]execute_steps=job["execute_steps"]run_steps=job_run["run_steps"]# filter only dbt invocation stepssteps=[]forrun_stepinrun_steps:name=run_step["name"]ifname.startswith("Invoke dbt with `"):regex_pattern="Invoke dbt with `([^`.]*)`"m=re.search(regex_pattern,name)ifmandm.group(1)inexecute_steps:steps.append(run_step["index"])# catalog is available only if docs are generatedcatalog=Nonewithsuppress(Exception):catalog=operator.hook.get_job_run_artifact(operator.run_id,path="catalog.json").json()["data"]asyncdefget_artifacts_for_steps(steps,artifacts):"""Get artifacts for a list of steps concurrently."""tasks=[operator.hook.get_job_run_artifacts_concurrently(run_id=operator.run_id,account_id=account_id,step=step,artifacts=artifacts,)forstepinsteps]returnawaitasyncio.gather(*tasks)# get artifacts for steps concurrentlystep_artifacts=asyncio.run(get_artifacts_for_steps(steps=steps,artifacts=["manifest.json","run_results.json"]))# process each step in loop, sending generated events in the same order as stepsforartifactsinstep_artifacts:# process manifestmanifest=artifacts["manifest.json"]ifnotartifacts.get("run_results.json",None):continueprocessor=DbtCloudArtifactProcessor(producer=_PRODUCER,job_namespace=namespace(),skip_errors=False,logger=operator.log,manifest=manifest,run_result=artifacts["run_results.json"],profile=connection,catalog=catalog,)# generate same run id of current task instanceparent_run_id=OpenLineageAdapter.build_task_instance_run_id(dag_id=task_instance.dag_id,task_id=operator.task_id,execution_date=task_instance.execution_date,try_number=_get_try_number(task_instance),)parent_job=ParentRunMetadata(run_id=parent_run_id,job_name=f"{task_instance.dag_id}.{task_instance.task_id}",job_namespace=namespace(),)processor.dbt_run_metadata=parent_jobevents=processor.parse().events()client=get_openlineage_listener().adapter.get_or_create_openlineage_client()foreventinevents:client.emit(event=event)returnOperatorLineage()