Package: torch-airflow-sdk
TORCH-AIRFLOW-SDKTorch airflow sdk provides support for observability of airflow dags in torch catalog. With the use of torch airflow sdk, user can e2e observability on airflow dag run in torch UI. Every dag is associated with pipeline in torch.Make sure while configuring airflow, 4 environment variable are set up in airflow environment docker container.TORCH_CATALOG_URL - URL of the torch catalogTORCH_ACCESS_KEY - API access key generated from torch UITORCH_SECRET_KEY - API secret key generated from torch UIENABLE_VERSION_CHECK - This is used to enable or disable version compatibility check between Torch and SDK. Default Value is 'True'. To disable version check please set it to 'False'.Creating Airflow connectionIf you want to avoid using environment variables then you can create a connection in Airflow UI as described below and provide the connection id of that connection in TorchInitializer.
Set the following in connection:Conn id: Create a unique ID for the connectionConn Type: HTTPHost - URL of the torch catalogLogin - API access key generated from torch UIPassword - API secret key generated from torch UIExtra - {"ENABLE_VERSION_CHECK": "{{value}}"}. This value will be used to enable or disable version compatibility check between Torch and SDK. Default Value is 'True'. To disable version check please set it to 'False'.First of all, install below mentioned 2 pypi package to expose ETL in torch.pipinstalltorch-sdkRead more about torch-sdk fromherepipinstalltorch-airflow-sdkRead more about torch-airflow-sdk fromhereCreate TorchClientWhile creating a TorchClient connection to torch by default version compatibility checks between torch and sdk is enabled. If we want we can disable that check by passingdo_version_checkas `Falsefromtorch_sdk.torch_clientimportTorchClienttorchClient=TorchClient(url="https://torch.acceldata.local:5443",access_key="OY2VVIN2N6LJ",secret_key="da6bDBimQfXSMsyyhlPVJJfk7Zc2gs",do_version_check=False)Create PipelineTo explicitly create a pipeline in torch, the user needs to create a pipeline using torch-sdk. To know more about pipeline, check torch-sdk documentation that contains detailed information about torch pipeline usage. If TorchInitializer Operator is being used then this should not be called.fromtorch_sdk.models.pipelineimportCreatePipeline,PipelineMetadatafromtorch_sdk.torch_clientimportTorchClienttorchClient=TorchClient(url="https://torch.acceldata.local:5443",access_key="OY2VVIN2N6LJ",secret_key="da6bDBimQfXSMsyyhlPVJJfk7Zc2gs")pipeline=CreatePipeline(uid='customer.orders.monthly.agg',name='Customer Orders Monthly aggregate',description='Pipeline to Aggregate the customer orders over 1 year',meta=PipelineMetadata(owner='vaishvik',team='torch',codeLocation='...'),context={'associated_tables':'pipeline.customer, pipeline.orders, pipeline.customer_orders, pipeline.customer_orders_monthly_agg'})pipeline_res=torchClient.create_pipeline(pipeline=pipeline)Create DAGThis changed in version 0.0.30In airflow DAG code, import torch dag instead of airflow dag. All the parameters will be the same as standard apache airflow dag. But there will be 2 additional parametersoverride_success_callback,override_failure_callback.Params:override_success_callback: A Boolean parameter to allow the user to override the success callback provided by the SDK. The success callback end the pipeline run when DAG ends successfully. Default value is False. It should be set to True if we do not want the pipeline run to be ended at the end of the successful run of the DAG.override_failure_callback: A Boolean parameter to allow the user to override the failure callback provided by the SDK. The failure callback ends the pipeline run with error when DAG ends in failure. Default value is False. It should be set to True if we do not want the pipeline run to be ended at the end of the unsuccessful run of the DAG.These can be useful if few steps of the pipeline are being executed outside of Airflow DAG.fromtorch_airflow_sdk.dagimportDAGdag=DAG(dag_id='pipeline_demo_final',schedule_interval='@daily',default_args=default_args,start_date=datetime(2020,2,2),catchup=False,on_failure_callback=failure_callback,on_success_callback=success_callback,override_success_callback=False,override_failure_callback=False,)Create Job and Span using decoratorThis was added in version 0.0.36To create a job and span in the pipeline, the user needs to decorate the python function with a job decorator as shown in the below example.Object ofNodeshould have either asset_uid (
{data source}.{asset path from its root}) or job_uid (Uid of next Job) as parameters.Params:span_uid: A String parameter to specify the UID of span to be created. Default value is None. Ifspan_uidis not provided a span corresponding to the job will be created with value of job_uid.job_uid: A String parameter to specify the job UID of the pipeline. Default value is None. Ifjob_uidis not provided, uid is constructed using the task id and function name.inputs: An Array parameter of Node type objects being used by job as input. Default value is empty array.outputs: An Array parameter of Node type objects being returned by job as output. Default value is empty array.metadata: Parameter of type JobMetadata specifying the metadata of the job. Default value is None.xcom_to_event_mapper_ids: A list Parameter having list of xcom keys used to send xcom variables in span event. Default value is empty list.bounded_by_span: A boolean parameter deciding whether to create a span along with the Job. Default value is True. If its is set to True to create span make sure, it has**contextparameter inside the function argument. That gives access to the context of the task. Using the context, various span events can be sent inside the function. Use span_context = context['span_context_parent'] to get you the span context.NOTE: Passing context is mandatory to the function being decorated as our decorators use context to share information through xcom.fromtorch_airflow_sdk.decorators.jobimportjobfromtorch_sdk.models.jobimportJobMetadata,Node@job(job_uid='monthly.order.aggregate.job',inputs=[Node(asset_uid='POSTGRES_LOCAL_DS.pipeline.pipeline.customer_orders')],outputs=[Node(job_uid='Job2_uid')],metadata=JobMetadata(name='Vaishvik_brahmbhatt',team='backend',code_location='https://github.com/acme/reporting/report.scala'),span_uid='customer.orders.datagen.span',xcom_to_event_mapper_ids=['run_id','event_id'],bounded_by_span=True)defmonthly_order_aggregate(**context):passCreate Span Using DecoratorThis changed in version 0.0.30To create a span for a python function, the user can decorate a python function with a span decorator that contains span uid as parameters. To decorate function with span make sure, it has**contextparameter inside the function argument. That gives access to the context of the task. Using the context, various span events can be sent inside the function.  To get the parent span context, use the key namespan_context_parentin xcom pull of the task instance. It’s value will be span context instance which can  be used to create child spans and send custom events (As shown in below example.)Params:span_uid: A String parameter to specify the UID of span to be created. Default value is None. Ifspan_uidis not provided, uid is constructed using the task id and function name.xcom_to_event_mapper_ids: A Parameter having list of xcom keys used to send xcom variables in span event. Default value is empty list.NOTE: Passing context is mandatory to the function being decorated as our decorators use context to share information through xcom.fromtorch_airflow_sdk.decorators.spanimportspanfromtorch_sdk.events.generic_eventimportGenericEvent@span(span_uid='customer.orders.datagen.span',associated_job_uids=['monthly.order.aggregate.transfer'],xcom_to_event_mapper_ids=['run_id','event_id'])defdata_gen(**context):datagen_span_context=context['span_context_parent']# Send event for current spandatagen_span_context.send_event(GenericEvent(context_data={'client_time':str(datetime.now()),'detail':'Generating data'},event_uid="order.customer.join.result"))customer_datagen_span=datagen_span_context.create_child_span(uid="customer.data.gen",context_data={'client_time':str(datetime.now())})# Send event for child spancustomer_datagen_span.send_event(GenericEvent(context_data={'client_time':str(datetime.now()),'row_count':len(rows)},event_uid="order.customer.join.result"))customer_datagen_span.end(context_data={'client_time':str(datetime.now()),'customers_count':len(customer_ids)})Custom OperatorsTorch airflow sdk contains 4 custom operators.TorchInitializer Operator:This changed in version 0.0.40The user needs to add a task with a given operator at the root of your dag. This operator will create a new pipeline. Additionally, this will create new pipeline run and root span for that dag run of the airflow dag.Params:create_pipeline: A Boolean parameter deciding whether to create a pipeline(if not exists) and pipeline run. Default Value is True. This can be useful if pipeline/pipeline run has been created outside of Airflow DAG.span_name: A string parameter specifying name of the Root Span. Default value is None. If not provided we will use the pipeline_uid.span as span name.meta: A parameter specifying the metadata for pipeline (PipelineMetadata). Default value is None. If not provided PipelineMetadata(owner='sdk/pipeline-user', team='TORCH', codeLocation='...') is set as meta.pipeline_uid: A string parameter specifying the UID of the pipeline. It is a mandatory parameter.pipeline_name: A string parameter specifying the Name of the pipeline. Default value is None. If not provided pipeline_uid will be used as name.continuation_id: A string parameter that uniquely identifies a pipeline run. This parameter can accept jinja templates as well. Default value is None. This parameter is useful when we want to have a pipeline run span over multiple DAG's. To use it we need to provide a continuation id while creating pipeline in first DAG with create_pipeline=True and then provide the same continuation id in the second DAG where we want to continue the same pipeline run with create_pipeline=False.connection_id: A string parameter that uniquely identifies a connection storing Torch credentials. Default value is None. This parameter is useful when we want to use Torch credentials from Airflow connection instead of environment variables. To get details about creating a connection refer 'Creating Airflow connection' section above.fromtorch_airflow_sdk.operators.torch_initialiser_operatorimportTorchInitializerfromtorch_sdk.models.pipelineimportPipelineMetadata# example of jinja templates being used in continuation_id# jinja template to pull value from config json# continuation_id=f"{{{{ dag_run.conf['continuation_id']  }}}}"# jinja template to pull value from xcom# continuation_id=f"{{{{ task_instance.xcom_pull(key='continuation_id') }}}}"torch_initializer_task=TorchInitializer(task_id='torch_pipeline_initializer',pipeline_uid='customer.orders.monthly.agg.demo',pipeline_name='CUSTOMERS ORDERS MONTHLY AGG',continuation_id='heterogeneous_test',create_pipeline=True,span_name='customer.orders.monthly.agg.demo.span',meta=PipelineMetadata(owner='test',team='testing',codeLocation='...'),dag=dag)SpanOperator Operator :This changed in version 0.0.30SpanOperator Operator will execute any std operator being passed asoperatorparameter and send span start and end event it. Just wrap the std operator with a span operator.
Make sure that the wrapped operator is not added in the DAG. If the operator is wrapped with a span operator, the span operator will take care of that operator task inside its execution.Params:span_uid: A string parameter specifying the UID of span to be created. Ifjob_uidis not provided, uid is constructed using the task id and operator name.xcom_to_event_mapper_ids: A list parameter having list of xcom keys used to send xcom variables in span event. Default value is empty list.operator: A parameter specifying the Standard airflow operator. It is a mandatory parameter.Other parameters will be the same as the airflow standard base operator.WARNING: Do not specify thedagparameter in std airflow operator being passed as an argument to SpanOperator as the execution of operator task is taken care of by SpanOperator.fromtorch_airflow_sdk.operators.span_operatorimportSpanOperatorget_order_agg_for_q4=PostgresOperator(task_id="get_monthly_order_aggregate_last_quarter",postgres_conn_id='example_db',sql="select * from information_schema.attributess",)get_order_agg_for_q4=SpanOperator(task_id="get_monthly_order_aggregate_last_quarter",span_uid='monthly.order.agg.q4.span',operator=get_order_agg_for_q4,associated_job_uids=['monthly.order.aggregate.transfer'],xcom_to_event_mapper_ids=['run_id','event_id'],dag=dag)JobOperator Operator :This changed in version 0.0.36JobOperator Operator will execute any std operator being passed asoperatorparameter and create a job and send span start and end event. Just wrap the std operator with a Job operator.
Make sure that the wrapped operator is not added in the DAG. If the operator is wrapped with a Job operator, the Job operator will take care of that operator task inside its execution.Object ofNodeshould have either asset_uid (
{data source}.{asset path from its root}) or job_uid (Uid of next Job) as parameters.Params:span_uid: A string parameter to specify the UID of span to be created. Default value is None. Ifspan_uidis not provided a span corresponding to the job will be created with value of job_uid.job_uid: A string parameter to specify the job UID of the pipeline. Default value is None. Ifjob_uidis not provided, uid is constructed using the task id and operator name.inputs: An array parameter of Node type objects being used by job as input. Default value is empty array.outputs: An array parameter of Node type objects being returned by job as output. Default value is empty array.metadata: A parameter of type JobMetadata specifying the metadata of the job. Default value is None.xcom_to_event_mapper_ids: A list parameter having list of xcom keys used to send xcom variables in span event. Default value is empty list.bounded_by_span: A boolean parameter deciding whether to create a span along with the Job. Default value is True. If its is set to True to create span make sure, it has**contextparameter inside the function argument. That gives access to the context of the task. Using the context, various span events can be sent inside the function. Use span_context = context['span_context_parent'] to get you the span context.operator: A Parameter specifying the Standard airflow operator. It is a mandatory parameter.Other parameters will be the same as the airflow standard base operator.
Make sure, inside a Node the type of the object which will have asset_uid (
{data source}.{asset path from its root}) or job_uid (Uid of next Job) as parameters.WARNING: Do not specify thedagparameter in std airflow operator being passed as an argument to JobOperator as the execution of operator task is taken care of by JobOperator.fromtorch_airflow_sdk.operators.job_operatorimportJobOperatorfromtorch_sdk.models.jobimportNode,JobMetadataget_order_agg_for_q4=PostgresOperator(task_id="get_monthly_order_aggregate_last_quarter",postgres_conn_id='example_db',sql="select * from information_schema.attributess",)get_order_agg_for_q4=JobOperator(task_id="get_monthly_order_aggregate_last_quarter",job_uid='customer.order.join.job',inputs=[Node(asset_uid='POSTGRES_LOCAL_DS.pipeline.pipeline.orders'),Node(asset_uid='POSTGRES_LOCAL_DS.pipeline.pipeline.customers')],outputs=[Node(job_uid='next_job_uid')],metadata=JobMetadata('name','team','code_location'),span_uid='monthly.order.agg.q4.span',operator=get_order_agg_for_q4,xcom_to_event_mapper_ids=['run_id','event_id'],bounded_by_span=True,dag=dag)This changed in version 0.0.34ExecutePolicyOperator Operator :ExecutePolicyOperatoris used to execute a policy by passingpolicytypeandpolicy_id.Params:sync: A boolean parameter used to decide if the policy should be executed synchronously or asynchronously. It is a mandatory parameter. If it is set toTrueit will return only after the execution ends. If it is set toFalseit will return immediately after starting the execution.policy_type: A PolicyType parameter used to specify the policy type. It is a mandatory parameter. It is a enum which will take values from constants as PolicyType.DATA_QUALITY or PolicyType.RECONCILIATION.policy_id: A string parameter used to specify the policy id to be executed. It is a mandatory parameter.incremental: A boolean parameter used to specify if the policy execution should be incremental or full. Default value is False.failure_strategy: An enum parameter used to decide the behaviour in case of failure. Default value is DoNotFail.failure_strategytakes enum of typeFailureStrategywhich can have 3 values DoNotFail, FailOnError and FailOnWarning.DoNotFail will never throw. In case of failure it will log the error.FailOnError will Throw exception only if it's an error. In case of warning it return without any errors.FailOnWarning will Throw exception on warning as well as error.fromtorch_airflow_sdk.operators.execute_policy_operatorimportExecutePolicyOperatorfromtorch_sdk.constantsimportFailureStrategy,PolicyTypeoperator_task=ExecutePolicyOperator(task_id='torch_pipeline_operator_test',policy_type=PolicyType.DATA_QUALITY,policy_id=46,sync=True,failure_strategy=FailureStrategy.DoNotFail,dag=dag)ExecutePolicyOperatorstores the execution id of the policy executed in xcom using the key {policy_type.name}_{policy_id}_execution_id. Replace the policy_type and policy_id based on the policy.Hence, to query the result in another task you need to pull the execution id from xcom using the same key {policy_type}_{policy_id}_execution_idget_polcy_execution_resultcan be used to query the result using the execution id pulled from xcom.
In this example the policy_type is const.PolicyType.DATA_QUALITY.name and the policy_id is 46.Params:policy_type:  A PolicyType parameter used to specify the policy type. It is a mandatory parameter. It is a enum which can take values from constants as PolicyType.DATA_QUALITY or PolicyType.RECONCILIATION.execution_id: A string parameter specifying the execution id for which we want to query the results. It is a mandatory parameter.failure_strategy: An Enum parameter used to decide the behaviour in case of failure. Default value is DoNotFail.failure_strategytakes enum of typeFailureStrategywhich can have 3 values DoNotFail, FailOnError and FailOnWarning.DoNotFail will never throw. In case of failure it will log the error.FailOnError will Throw exception only if it's an error. In case of warning it return without any errors.FailOnWarning will Throw exception on warning as well as error.fromtorch_sdk.torch_clientimportTorchClientfromtorch_airflow_sdk.initialiserimporttorch_credentialsfromtorch_sdk.constantsimportFailureStrategy,PolicyType,RuleExecutionStatusdefruleoperator_result(**context):xcom_key=f'{PolicyType.DATA_QUALITY.name}_46_execution_id'task_instance=context['ti']# pull the execution id from xcomexecution_id=task_instance.xcom_pull(key=xcom_key)ifexecution_idisnotNone:torch_client=TorchClient(**torch_credentials)result=torch_client.get_polcy_execution_result(policy_type=PolicyType.DATA_QUALITY,execution_id=execution_id,failure_strategy=FailureStrategy.DoNotFail)ifresult.execution.resultStatus==RuleExecutionStatus.ERRORED:print(result.execution.executionError)get_policy_statuscan be used to  query the current status of execution.Params:policy_type:  A PolicyType parameter used to specify the policy type. It is a mandatory parameter. It is an enum which can take values from constants as PolicyType.DATA_QUALITY or PolicyType.RECONCILIATION.execution_id: A string parameter specifying the execution id which we want to query the status. It is a mandatory parameter.You need to pull the execution id from xcom using the same key {policy_type.name}_{policy_id}_execution_id which was pushed byExecutePolicyOperator. Replace the policy_type and policy_id based on the policy. In this example the policy_type is PolicyType.DATA_QUALITY.name and the policy_id is 46.fromtorch_sdk.torch_clientimportTorchClientfromtorch_airflow_sdk.initialiserimporttorch_credentialsimporttorch_sdk.constantsasconstdefruleoperator_status(**context):xcom_key=f'{const.PolicyType.DATA_QUALITY.name}_46_execution_id'task_instance=context['ti']# pull the execution id from xcomexecution_id=task_instance.xcom_pull(key=xcom_key)ifexecution_idisnotNone:torch_client=TorchClient(**torch_credentials)result=torch_client.get_policy_status(policy_type=const.PolicyType.DATA_QUALITY,execution_id=execution_id)ifresult==const.RuleExecutionStatus.ERRORED:print("Policy execution encountered an error.")Version Log0.1.0 (17/06/2021)Torch airflow sdk - Wrapper on apache airflowTorch airflow sdk provides support for observability of airflow dags in torch catalog. With the use of torch airflow sdk, user can e2e observability on airflow dag run in torch UI.Support for airflow 1.0 and 2.0.0.1.3 (2/02/2022)Added support for pipeline creation by passing uid to the initializer operatorJob and span decorator's additional parametersMinor bug fixes