dstandish commented on code in PR #69633:
URL: https://github.com/apache/airflow/pull/69633#discussion_r3555252271


##########
airflow-core/src/airflow/models/dagrun.py:
##########
@@ -194,6 +195,33 @@ def trace_sampled_override(conf) -> bool | None:
     return None
 
 
+def parent_trace_context(conf) -> context.Context | None:
+    """
+    External parent trace context from the ``airflow/parent_trace_context`` 
run conf key.
+
+    Lets a run be embedded in a trace owned by an external system (an upstream
+    orchestrator, event pipeline, CI job, another Airflow) instead of being a 
root
+    trace. The value is a W3C ``traceparent`` string, or a carrier dict 
carrying
+    ``traceparent`` (and optionally ``tracestate``); anything else -- 
including a
+    malformed traceparent -- is ignored so it can neither silently mis-parent 
the
+    run nor fail run creation. Returns an OpenTelemetry ``Context`` when a 
valid
+    parent is present, else None (root trace, the default).
+    """
+    if not conf:
+        return None
+    raw = conf.get(PARENT_TRACE_CONTEXT_KEY)
+    if isinstance(raw, str):
+        carrier = {"traceparent": raw}
+    elif isinstance(raw, dict) and isinstance(raw.get("traceparent"), str):
+        carrier = {k: raw[k] for k in ("traceparent", "tracestate") if 
isinstance(raw.get(k), str)}

Review Comment:
   Good catch on `traceparent` — that isinstance check is redundant there since 
it's already validated a few lines above. But the `tracestate` check does 
matter: `extract()` doesn't validate tracestate before parsing it, so a 
non-string value reaches `TraceState.from_header()` directly and raises 
`TypeError: expected string or bytes-like object, got 'int'` instead of being 
treated as invalid — which would break run creation for a malformed conf. Added 
a regression test for that case in d9f9f5e4cb.
   
   ---
   Drafted-by: Claude Code (Sonnet 5) (no human review before posting)



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