amoghrajesh commented on code in PR #67659:
URL: https://github.com/apache/airflow/pull/67659#discussion_r3635658581


##########
task-sdk/tests/task_sdk/execution_time/test_task_runner.py:
##########
@@ -5813,6 +5814,155 @@ def test_no_task_execute_span_at_detail_level_1(self):
         assert "task.execute" not in names
 
 
+class TestCallbackSpans:
+    """Tests for spans emitted around task callback / pre-post-execute 
invocations."""
+
+    @pytest.fixture(autouse=True)
+    def _sampled_carrier_provider(self):
+        """Make new_dagrun_trace_carrier produce a SAMPLED carrier (see 
TestDetailSpan)."""
+        provider = TracerProvider()
+        with mock.patch(
+            "airflow._shared.observability.traces.trace.get_tracer_provider",
+            return_value=provider,
+        ):
+            yield

Review Comment:
   Seems to be a repetition / copy of `_sampled_carrier_provider` in 
`TestDetailSpan` (docstring says so too!). BTW, I think we should re-export 
`get_tracer_provider` from 
`airflow._shared.observability.traces.trace.get_tracer_provider` into 
`task-sdk/src/airflow/sdk/observability` and use instead of mocking from an 
internal shared module.



##########
task-sdk/src/airflow/sdk/execution_time/task_runner.py:
##########
@@ -2131,10 +2140,17 @@ def _execute_task(context: Context, ti: 
RuntimeTaskInstance, log: Logger):
 
     outlet_events = context_get_outlet_events(context)
 
+    def _run_hook(span_kind: str, hook, *run_args):
+        with detail_span(
+            f"callback.{span_kind}",
+            attributes={"airflow.callback.name": getattr(hook, "__name__", 
repr(hook))},
+        ):
+            create_executable_runner(hook, outlet_events, 
logger=log).run(*run_args)
+
     if (pre_execute_hook := task._pre_execute_hook) is not None:
-        create_executable_runner(pre_execute_hook, outlet_events, 
logger=log).run(context)
+        _run_hook("pre_execute", pre_execute_hook, context)
     if getattr(pre_execute_hook := task.pre_execute, "__func__", None) is not 
BaseOperator.pre_execute:
-        create_executable_runner(pre_execute_hook, outlet_events, 
logger=log).run(context)
+        _run_hook("pre_execute", pre_execute_hook, context)

Review Comment:
   There are two independent checks here: `task._pre_execute_hook` (instance 
level hook) and `task.pre_execute` (a method override) and both if present get 
wrapped in a span named identically: "callback.pre_execute". Same for the two 
post_execute variants. If both fire on the same task we would get two sibling 
spans with the exact same name under `_execute_task`, it's a legitimate 
observation if you want it FYI.



##########
task-sdk/src/airflow/sdk/execution_time/task_runner.py:
##########
@@ -2131,10 +2140,17 @@ def _execute_task(context: Context, ti: 
RuntimeTaskInstance, log: Logger):
 
     outlet_events = context_get_outlet_events(context)
 
+    def _run_hook(span_kind: str, hook, *run_args):

Review Comment:
   Typing for hook and / or run_args pls?



-- 
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