ferruzzi commented on code in PR #43941:
URL: https://github.com/apache/airflow/pull/43941#discussion_r1980618565


##########
tests/core/test_otel_tracer.py:
##########
@@ -131,27 +146,38 @@ def test_traskinstance_tracer(self, conf_a, exporter):
             in_mem_exporter = InMemorySpanExporter()
             exporter.return_value = in_mem_exporter
 
-            now = datetime.now()
-            # magic mock
-            ti = MagicMock()
-            ti.dag_run.conf = {}
-            ti.task_id = "task_id"
-            ti.start_date = now
-            ti.dag_run.dag_id = "dag_id"
-            ti.dag_run.run_id = "run_id"
-            ti.dag_run.dag_hash = "hashcode"
-            ti.dag_run.run_type = "manual"
-            ti.dag_run.queued_at = now
-            ti.dag_run.start_date = now
+            # Method that represents another service which is
+            #  - getting the carrier
+            #  - extracting the context
+            #  - using the context to create a new span
+            # The new span should be associated with the span from the 
injected context carrier.
+            def _task_func(otel_tr, carrier):
+                parent_context = otel_tr.extract(carrier)
+
+                with otel_tr.start_child_span(span_name="sub_span", 
parent_context=parent_context) as span:
+                    span.set_attribute("attr2", "val2")
+                    json_span = json.loads(span.to_json())
+                return json_span
 
             tracer = otel_tracer.get_otel_tracer(Trace)
-            with tracer.start_span_from_taskinstance(ti=ti, span_name="mydag") 
as s1:
-                with tracer.start_span(span_name="span2") as s2:
-                    s2.set_attribute("attr2", "val2")
-                    span2 = json.loads(s2.to_json())
-                span1 = json.loads(s1.to_json())
 
-            log.info(span1)
-            log.info(span2)
-            assert span1["context"]["trace_id"] == 
f"0x{utils.gen_trace_id(ti.dag_run)}"
-            assert span1["context"]["span_id"] == f"0x{utils.gen_span_id(ti)}"
+            root_span = tracer.start_root_span(span_name="root_span", 
start_as_current=False)
+            # The context is available, it can be injected into the carrier.
+            context_carrier = tracer.inject()
+
+            # Some function that uses the carrier to create a new span.
+            json_span2 = _task_func(otel_tr=tracer, carrier=context_carrier)
+
+            json_span1 = json.loads(root_span.to_json())
+            # Manually end the span.
+            root_span.end()
+
+            log.info(json_span1)
+            log.info(json_span2)

Review Comment:
   Is there a reason we're logging these?



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