ferruzzi commented on code in PR #43941: URL: https://github.com/apache/airflow/pull/43941#discussion_r1980653665
########## tests/integration/otel/dags/otel_test_dag.py: ########## @@ -0,0 +1,103 @@ +# 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 logging +from datetime import datetime + +from opentelemetry import trace + +from airflow import DAG +from airflow.providers.standard.operators.python import PythonOperator +from airflow.traces import otel_tracer +from airflow.traces.tracer import Trace + +logger = logging.getLogger("airflow.otel_test_dag") + +args = { + "owner": "airflow", + "start_date": datetime(2024, 9, 1), + "retries": 0, +} + +# DAG definition. +with DAG( + "otel_test_dag", + default_args=args, + schedule=None, + catchup=False, +) as dag: + # Tasks. + def task1_func(**dag_context): + logger.info("Starting Task_1.") + + ti = dag_context["ti"] + context_carrier = ti.context_carrier + + otel_task_tracer = otel_tracer.get_otel_tracer_for_task(Trace) + tracer_provider = otel_task_tracer.get_otel_tracer_provider() + + if context_carrier is not None: + logger.info("Found ti.context_carrier: %s.", context_carrier) + logger.info("Extracting the span context from the context_carrier.") + parent_context = otel_task_tracer.extract(context_carrier) + with otel_task_tracer.start_child_span( + span_name=f"{ti.task_id}_sub_span1", + parent_context=parent_context, + component="dag", + ) as s1: + s1.set_attribute("attr1", "val1") + logger.info("From task sub_span1.") + + with otel_task_tracer.start_child_span(f"{ti.task_id}_sub_span2") as s2: + s2.set_attribute("attr2", "val2") + logger.info("From task sub_span2.") + + tracer = trace.get_tracer("trace_test.tracer", tracer_provider=tracer_provider) + with tracer.start_as_current_span(name=f"{ti.task_id}_sub_span3") as s3: + s3.set_attribute("attr3", "val3") + logger.info("From task sub_span3.") + + with otel_task_tracer.start_child_span( + span_name=f"{ti.task_id}_sub_span4", + parent_context=parent_context, + component="dag", + ) as s4: + s4.set_attribute("attr4", "val4") + logger.info("From task sub_span4.") + + logger.info("Task_1 finished.") + + def task2_func(): + logger.info("Starting Task_2.") + for i in range(3): + logger.info("Task_2, iteration '%d'.", i) + logger.info("Task_2 finished.") + + # Task operators. + t1 = PythonOperator( + task_id="task_1", + python_callable=task1_func, + ) + + t2 = PythonOperator( + task_id="task_2", + python_callable=task2_func, + ) + + # Dependencies. + t1 >> t2 Review Comment: Of note with TaskFlow: - By default, the `task_id` is set to the same as the function name, so in this case there is no need to use a variable for `dag_context["ti"].task_id` - You don't need to define them as `task1 = task1_func()` with TaskFlow, you can just call them. ((note: some/all IDEs will show a type mismatch warning in the chain() method. That's a known thing, you can ignore that warning for now. It works as intended.)) - By default, the dag_context gets passed into the task and we can just pluck the part(s) we need from that like I did with `ti` in task1. so ``` @task def task1(ti): ... ``` is the same as ``` @task def task1(**dag_context): ti = dag_context["ti"] ... ``` -- 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]
