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


##########
airflow/traces/otel_tracer.py:
##########
@@ -0,0 +1,316 @@
+#
+# 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
+import random
+
+from opentelemetry import trace
+from opentelemetry.context import create_key
+from opentelemetry.exporter.otlp.proto.http.trace_exporter import 
OTLPSpanExporter
+from opentelemetry.sdk.resources import HOST_NAME, SERVICE_NAME, Resource
+from opentelemetry.sdk.trace import Span, Tracer as OpenTelemetryTracer, 
TracerProvider
+from opentelemetry.sdk.trace.export import BatchSpanProcessor, 
ConsoleSpanExporter
+from opentelemetry.sdk.trace.id_generator import IdGenerator
+from opentelemetry.trace import Link, NonRecordingSpan, SpanContext, 
TraceFlags, Tracer
+from opentelemetry.trace.span import INVALID_SPAN_ID, INVALID_TRACE_ID
+
+from airflow.configuration import conf
+from airflow.traces import (
+    TRACEPARENT,
+    TRACESTATE,
+)
+from airflow.traces.utils import (
+    gen_dag_span_id,
+    gen_span_id,
+    gen_trace_id,
+    parse_traceparent,
+    parse_tracestate,
+)
+from airflow.utils.dates import datetime_to_nano
+from airflow.utils.net import get_hostname
+
+log = logging.getLogger(__name__)
+
+_NEXT_ID = create_key("next_id")
+
+
+class OtelTrace:
+    """
+    Handle all tracing requirements such as getting the tracer, and starting a 
new span.
+
+    When OTEL is enabled, the Trace class will be replaced by this class.
+    """
+
+    def __init__(self, span_exporter: ConsoleSpanExporter | OTLPSpanExporter, 
tag_string: str | None = None):
+        self.span_exporter = span_exporter
+        self.span_processor = BatchSpanProcessor(self.span_exporter)
+        self.tag_string = tag_string
+        self.otel_service = conf.get("traces", "otel_service")
+
+    def get_tracer(
+        self, component: str, trace_id: int | None = None, span_id: int | None 
= None
+    ) -> OpenTelemetryTracer | Tracer:
+        """Tracer that will use special AirflowOtelIdGenerator to control 
producing certain span and trace id."""
+        resource = Resource(attributes={HOST_NAME: get_hostname(), 
SERVICE_NAME: self.otel_service})
+        if trace_id or span_id:
+            # in case where trace_id or span_id was given
+            tracer_provider = TracerProvider(
+                resource=resource, 
id_generator=AirflowOtelIdGenerator(span_id=span_id, trace_id=trace_id)
+            )
+        else:
+            tracer_provider = TracerProvider(resource=resource)
+        tracer_provider.add_span_processor(self.span_processor)
+        tracer = tracer_provider.get_tracer(component)
+        """
+        Tracer will produce a single ID value if value is provided. Note that 
this is one-time only, so any
+        subsequent call will produce the normal random ids.
+        """
+        return tracer
+
+    def get_current_span(self):
+        return trace.get_current_span()
+
+    def use_span(self, span: Span):
+        return trace.use_span(span=span)
+
+    def start_span(
+        self,
+        span_name: str,
+        component: str | None = None,
+        parent_sc: SpanContext | None = None,
+        span_id=None,
+        links=None,
+        start_time=None,
+    ):
+        """Start a span; if service_name is not given, otel_service is used."""
+        if component is None:
+            component = self.otel_service
+
+        trace_id = self.get_current_span().get_span_context().trace_id
+        tracer = self.get_tracer(component=component, trace_id=trace_id, 
span_id=span_id)
+
+        attributes = parse_tracestate(self.tag_string) if self.tag_string else 
{}
+
+        if links is not None:
+            _links = gen_links_from_kv_list(links)
+        else:
+            _links = []
+
+        if start_time is not None:
+            start_time = datetime_to_nano(start_time)
+
+        if parent_sc is not None:
+            ctx = trace.set_span_in_context(NonRecordingSpan(parent_sc))
+            span = tracer.start_as_current_span(
+                span_name, context=ctx, attributes=attributes, links=_links, 
start_time=start_time
+            )
+        else:
+            span = tracer.start_as_current_span(
+                span_name, attributes=attributes, links=_links, 
start_time=start_time
+            )
+        return span
+
+    def start_span_from_dagrun(
+        self, dagrun, span_name: str | None = None, component: str = "dagrun", 
links=None
+    ):
+        """Produce a span from dag run."""
+        # check if dagrun has configs
+        conf = dagrun.conf
+        trace_id = int(gen_trace_id(dag_run=dagrun, as_int=True))
+        span_id = int(gen_dag_span_id(dag_run=dagrun, as_int=True))
+
+        if conf is not None:
+            traceparent = conf.get(TRACEPARENT)
+            tracestate = conf.get(TRACESTATE)
+
+        tracer = self.get_tracer(component=component, span_id=span_id, 
trace_id=trace_id)
+
+        if self.tag_string and tracestate:
+            tag_string = self.tag_string + "," + tracestate
+        else:
+            tag_string = self.tag_string or tracestate
+
+        if span_name is None:
+            span_name = dagrun.dag_id
+
+        if links is not None:
+            _links = gen_links_from_kv_list(links)
+        else:
+            _links = []
+
+        _links.append(
+            Link(
+                context=trace.get_current_span().get_span_context(),
+                attributes={"meta.annotation_type": "link", "from": 
"parenttrace"},
+            )
+        )
+
+        if traceparent is not None:
+            # add the trace parent as the link
+            _links.append(gen_link_from_traceparent(traceparent))

Review Comment:
   ```suggestion
           if traceparent:
               # add the trace parent as the link
               _links.append(gen_link_from_traceparent(traceparent))
   ```



##########
airflow/traces/otel_tracer.py:
##########
@@ -0,0 +1,316 @@
+#
+# 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
+import random
+
+from opentelemetry import trace
+from opentelemetry.context import create_key
+from opentelemetry.exporter.otlp.proto.http.trace_exporter import 
OTLPSpanExporter
+from opentelemetry.sdk.resources import HOST_NAME, SERVICE_NAME, Resource
+from opentelemetry.sdk.trace import Span, Tracer as OpenTelemetryTracer, 
TracerProvider
+from opentelemetry.sdk.trace.export import BatchSpanProcessor, 
ConsoleSpanExporter
+from opentelemetry.sdk.trace.id_generator import IdGenerator
+from opentelemetry.trace import Link, NonRecordingSpan, SpanContext, 
TraceFlags, Tracer
+from opentelemetry.trace.span import INVALID_SPAN_ID, INVALID_TRACE_ID
+
+from airflow.configuration import conf
+from airflow.traces import (
+    TRACEPARENT,
+    TRACESTATE,
+)
+from airflow.traces.utils import (
+    gen_dag_span_id,
+    gen_span_id,
+    gen_trace_id,
+    parse_traceparent,
+    parse_tracestate,
+)
+from airflow.utils.dates import datetime_to_nano
+from airflow.utils.net import get_hostname
+
+log = logging.getLogger(__name__)
+
+_NEXT_ID = create_key("next_id")
+
+
+class OtelTrace:
+    """
+    Handle all tracing requirements such as getting the tracer, and starting a 
new span.
+
+    When OTEL is enabled, the Trace class will be replaced by this class.
+    """
+
+    def __init__(self, span_exporter: ConsoleSpanExporter | OTLPSpanExporter, 
tag_string: str | None = None):
+        self.span_exporter = span_exporter
+        self.span_processor = BatchSpanProcessor(self.span_exporter)
+        self.tag_string = tag_string
+        self.otel_service = conf.get("traces", "otel_service")
+
+    def get_tracer(
+        self, component: str, trace_id: int | None = None, span_id: int | None 
= None
+    ) -> OpenTelemetryTracer | Tracer:
+        """Tracer that will use special AirflowOtelIdGenerator to control 
producing certain span and trace id."""
+        resource = Resource(attributes={HOST_NAME: get_hostname(), 
SERVICE_NAME: self.otel_service})
+        if trace_id or span_id:
+            # in case where trace_id or span_id was given
+            tracer_provider = TracerProvider(
+                resource=resource, 
id_generator=AirflowOtelIdGenerator(span_id=span_id, trace_id=trace_id)
+            )
+        else:
+            tracer_provider = TracerProvider(resource=resource)
+        tracer_provider.add_span_processor(self.span_processor)
+        tracer = tracer_provider.get_tracer(component)
+        """
+        Tracer will produce a single ID value if value is provided. Note that 
this is one-time only, so any
+        subsequent call will produce the normal random ids.
+        """
+        return tracer
+
+    def get_current_span(self):
+        return trace.get_current_span()
+
+    def use_span(self, span: Span):
+        return trace.use_span(span=span)
+
+    def start_span(
+        self,
+        span_name: str,
+        component: str | None = None,
+        parent_sc: SpanContext | None = None,
+        span_id=None,
+        links=None,
+        start_time=None,
+    ):
+        """Start a span; if service_name is not given, otel_service is used."""
+        if component is None:
+            component = self.otel_service
+
+        trace_id = self.get_current_span().get_span_context().trace_id
+        tracer = self.get_tracer(component=component, trace_id=trace_id, 
span_id=span_id)
+
+        attributes = parse_tracestate(self.tag_string) if self.tag_string else 
{}
+
+        if links is not None:
+            _links = gen_links_from_kv_list(links)
+        else:
+            _links = []
+
+        if start_time is not None:
+            start_time = datetime_to_nano(start_time)
+
+        if parent_sc is not None:
+            ctx = trace.set_span_in_context(NonRecordingSpan(parent_sc))
+            span = tracer.start_as_current_span(
+                span_name, context=ctx, attributes=attributes, links=_links, 
start_time=start_time
+            )
+        else:
+            span = tracer.start_as_current_span(
+                span_name, attributes=attributes, links=_links, 
start_time=start_time
+            )
+        return span
+
+    def start_span_from_dagrun(
+        self, dagrun, span_name: str | None = None, component: str = "dagrun", 
links=None
+    ):
+        """Produce a span from dag run."""
+        # check if dagrun has configs
+        conf = dagrun.conf
+        trace_id = int(gen_trace_id(dag_run=dagrun, as_int=True))
+        span_id = int(gen_dag_span_id(dag_run=dagrun, as_int=True))
+
+        if conf is not None:
+            traceparent = conf.get(TRACEPARENT)
+            tracestate = conf.get(TRACESTATE)
+
+        tracer = self.get_tracer(component=component, span_id=span_id, 
trace_id=trace_id)
+
+        if self.tag_string and tracestate:
+            tag_string = self.tag_string + "," + tracestate
+        else:
+            tag_string = self.tag_string or tracestate
+
+        if span_name is None:
+            span_name = dagrun.dag_id
+
+        if links is not None:
+            _links = gen_links_from_kv_list(links)
+        else:
+            _links = []
+
+        _links.append(
+            Link(
+                context=trace.get_current_span().get_span_context(),
+                attributes={"meta.annotation_type": "link", "from": 
"parenttrace"},
+            )
+        )
+
+        if traceparent is not None:
+            # add the trace parent as the link
+            _links.append(gen_link_from_traceparent(traceparent))
+
+        span_ctx = SpanContext(
+            trace_id=INVALID_TRACE_ID, span_id=INVALID_SPAN_ID, 
is_remote=True, trace_flags=TraceFlags(0x01)
+        )
+        ctx = trace.set_span_in_context(NonRecordingSpan(span_ctx))
+        span = tracer.start_as_current_span(
+            name=span_name,
+            context=ctx,
+            links=_links,
+            start_time=datetime_to_nano(dagrun.queued_at),
+            attributes=parse_tracestate(tag_string),
+        )
+        return span
+
+    def start_span_from_taskinstance(
+        self,
+        ti,
+        span_name: str | None = None,
+        component: str = "taskinstance",
+        child: bool = False,
+        links=None,
+    ):
+        """
+        Create and start span from given task instance.
+
+        Essentially the span represents the ti itself if child == True, it 
will create a 'child' span under the given span.
+        """
+        dagrun = ti.dag_run
+        trace_id = int(gen_trace_id(dag_run=dagrun, as_int=True))
+        span_id = int(gen_span_id(ti=ti, as_int=True))

Review Comment:
   I hate to say it, but if we don't come up with a way to not have to cast 
this each time, then I feel like adding the as_int was a mistake.  Could you 
remind me why this has to be re-cast here?



##########
airflow/traces/otel_tracer.py:
##########
@@ -0,0 +1,316 @@
+#
+# 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
+import random
+
+from opentelemetry import trace
+from opentelemetry.context import create_key
+from opentelemetry.exporter.otlp.proto.http.trace_exporter import 
OTLPSpanExporter
+from opentelemetry.sdk.resources import HOST_NAME, SERVICE_NAME, Resource
+from opentelemetry.sdk.trace import Span, Tracer as OpenTelemetryTracer, 
TracerProvider
+from opentelemetry.sdk.trace.export import BatchSpanProcessor, 
ConsoleSpanExporter
+from opentelemetry.sdk.trace.id_generator import IdGenerator
+from opentelemetry.trace import Link, NonRecordingSpan, SpanContext, 
TraceFlags, Tracer
+from opentelemetry.trace.span import INVALID_SPAN_ID, INVALID_TRACE_ID
+
+from airflow.configuration import conf
+from airflow.traces import (
+    TRACEPARENT,
+    TRACESTATE,
+)
+from airflow.traces.utils import (
+    gen_dag_span_id,
+    gen_span_id,
+    gen_trace_id,
+    parse_traceparent,
+    parse_tracestate,
+)
+from airflow.utils.dates import datetime_to_nano
+from airflow.utils.net import get_hostname
+
+log = logging.getLogger(__name__)
+
+_NEXT_ID = create_key("next_id")
+
+
+class OtelTrace:
+    """
+    Handle all tracing requirements such as getting the tracer, and starting a 
new span.
+
+    When OTEL is enabled, the Trace class will be replaced by this class.
+    """
+
+    def __init__(self, span_exporter: ConsoleSpanExporter | OTLPSpanExporter, 
tag_string: str | None = None):
+        self.span_exporter = span_exporter
+        self.span_processor = BatchSpanProcessor(self.span_exporter)
+        self.tag_string = tag_string
+        self.otel_service = conf.get("traces", "otel_service")
+
+    def get_tracer(
+        self, component: str, trace_id: int | None = None, span_id: int | None 
= None
+    ) -> OpenTelemetryTracer | Tracer:
+        """Tracer that will use special AirflowOtelIdGenerator to control 
producing certain span and trace id."""
+        resource = Resource(attributes={HOST_NAME: get_hostname(), 
SERVICE_NAME: self.otel_service})
+        if trace_id or span_id:
+            # in case where trace_id or span_id was given
+            tracer_provider = TracerProvider(
+                resource=resource, 
id_generator=AirflowOtelIdGenerator(span_id=span_id, trace_id=trace_id)
+            )
+        else:
+            tracer_provider = TracerProvider(resource=resource)
+        tracer_provider.add_span_processor(self.span_processor)
+        tracer = tracer_provider.get_tracer(component)
+        """
+        Tracer will produce a single ID value if value is provided. Note that 
this is one-time only, so any
+        subsequent call will produce the normal random ids.
+        """
+        return tracer
+
+    def get_current_span(self):
+        return trace.get_current_span()
+
+    def use_span(self, span: Span):
+        return trace.use_span(span=span)
+
+    def start_span(
+        self,
+        span_name: str,
+        component: str | None = None,
+        parent_sc: SpanContext | None = None,
+        span_id=None,
+        links=None,
+        start_time=None,
+    ):
+        """Start a span; if service_name is not given, otel_service is used."""
+        if component is None:
+            component = self.otel_service
+
+        trace_id = self.get_current_span().get_span_context().trace_id
+        tracer = self.get_tracer(component=component, trace_id=trace_id, 
span_id=span_id)
+
+        attributes = parse_tracestate(self.tag_string) if self.tag_string else 
{}
+
+        if links is not None:
+            _links = gen_links_from_kv_list(links)
+        else:
+            _links = []
+
+        if start_time is not None:
+            start_time = datetime_to_nano(start_time)
+
+        if parent_sc is not None:
+            ctx = trace.set_span_in_context(NonRecordingSpan(parent_sc))
+            span = tracer.start_as_current_span(
+                span_name, context=ctx, attributes=attributes, links=_links, 
start_time=start_time
+            )
+        else:
+            span = tracer.start_as_current_span(
+                span_name, attributes=attributes, links=_links, 
start_time=start_time
+            )
+        return span
+
+    def start_span_from_dagrun(
+        self, dagrun, span_name: str | None = None, component: str = "dagrun", 
links=None
+    ):
+        """Produce a span from dag run."""
+        # check if dagrun has configs
+        conf = dagrun.conf
+        trace_id = int(gen_trace_id(dag_run=dagrun, as_int=True))
+        span_id = int(gen_dag_span_id(dag_run=dagrun, as_int=True))
+
+        if conf is not None:
+            traceparent = conf.get(TRACEPARENT)
+            tracestate = conf.get(TRACESTATE)
+
+        tracer = self.get_tracer(component=component, span_id=span_id, 
trace_id=trace_id)
+
+        if self.tag_string and tracestate:
+            tag_string = self.tag_string + "," + tracestate
+        else:
+            tag_string = self.tag_string or tracestate
+
+        if span_name is None:
+            span_name = dagrun.dag_id
+
+        if links is not None:
+            _links = gen_links_from_kv_list(links)
+        else:
+            _links = []
+
+        _links.append(
+            Link(
+                context=trace.get_current_span().get_span_context(),
+                attributes={"meta.annotation_type": "link", "from": 
"parenttrace"},
+            )
+        )
+
+        if traceparent is not None:
+            # add the trace parent as the link
+            _links.append(gen_link_from_traceparent(traceparent))
+
+        span_ctx = SpanContext(
+            trace_id=INVALID_TRACE_ID, span_id=INVALID_SPAN_ID, 
is_remote=True, trace_flags=TraceFlags(0x01)
+        )
+        ctx = trace.set_span_in_context(NonRecordingSpan(span_ctx))
+        span = tracer.start_as_current_span(
+            name=span_name,
+            context=ctx,
+            links=_links,
+            start_time=datetime_to_nano(dagrun.queued_at),
+            attributes=parse_tracestate(tag_string),
+        )
+        return span
+
+    def start_span_from_taskinstance(
+        self,
+        ti,
+        span_name: str | None = None,
+        component: str = "taskinstance",
+        child: bool = False,
+        links=None,
+    ):
+        """
+        Create and start span from given task instance.
+
+        Essentially the span represents the ti itself if child == True, it 
will create a 'child' span under the given span.
+        """
+        dagrun = ti.dag_run
+        trace_id = int(gen_trace_id(dag_run=dagrun, as_int=True))
+        span_id = int(gen_span_id(ti=ti, as_int=True))
+        if span_name is None:
+            span_name = ti.task_id
+
+        if child is False:
+            parent_id = int(gen_dag_span_id(dag_run=dagrun, as_int=True))
+        else:
+            parent_id = span_id
+
+        span_ctx = SpanContext(
+            trace_id=trace_id, span_id=parent_id, is_remote=True, 
trace_flags=TraceFlags(0x01)
+        )
+
+        if links is not None:
+            _links = gen_links_from_kv_list(links)
+        else:
+            _links = []
+
+        a_link = Link(
+            context=trace.get_current_span().get_span_context(),
+            attributes={"meta.annotation_type": "link", "from": "parenttrace"},
+        )
+        _links.append(a_link)

Review Comment:
   I think maybe something happened here, I'm pretty sure you already changed 
this too....
   ```suggestion
           _links.append(
               Link(
                   context=trace.get_current_span().get_span_context(),
                   attributes={"meta.annotation_type": "link", "from": 
"parenttrace"},
                )
            )
   ```



##########
airflow/traces/otel_tracer.py:
##########
@@ -0,0 +1,316 @@
+#
+# 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
+import random
+
+from opentelemetry import trace
+from opentelemetry.context import create_key
+from opentelemetry.exporter.otlp.proto.http.trace_exporter import 
OTLPSpanExporter
+from opentelemetry.sdk.resources import HOST_NAME, SERVICE_NAME, Resource
+from opentelemetry.sdk.trace import Span, Tracer as OpenTelemetryTracer, 
TracerProvider
+from opentelemetry.sdk.trace.export import BatchSpanProcessor, 
ConsoleSpanExporter
+from opentelemetry.sdk.trace.id_generator import IdGenerator
+from opentelemetry.trace import Link, NonRecordingSpan, SpanContext, 
TraceFlags, Tracer
+from opentelemetry.trace.span import INVALID_SPAN_ID, INVALID_TRACE_ID
+
+from airflow.configuration import conf
+from airflow.traces import (
+    TRACEPARENT,
+    TRACESTATE,
+)
+from airflow.traces.utils import (
+    gen_dag_span_id,
+    gen_span_id,
+    gen_trace_id,
+    parse_traceparent,
+    parse_tracestate,
+)
+from airflow.utils.dates import datetime_to_nano
+from airflow.utils.net import get_hostname
+
+log = logging.getLogger(__name__)
+
+_NEXT_ID = create_key("next_id")
+
+
+class OtelTrace:
+    """
+    Handle all tracing requirements such as getting the tracer, and starting a 
new span.
+
+    When OTEL is enabled, the Trace class will be replaced by this class.
+    """
+
+    def __init__(self, span_exporter: ConsoleSpanExporter | OTLPSpanExporter, 
tag_string: str | None = None):
+        self.span_exporter = span_exporter
+        self.span_processor = BatchSpanProcessor(self.span_exporter)
+        self.tag_string = tag_string
+        self.otel_service = conf.get("traces", "otel_service")
+
+    def get_tracer(
+        self, component: str, trace_id: int | None = None, span_id: int | None 
= None
+    ) -> OpenTelemetryTracer | Tracer:
+        """Tracer that will use special AirflowOtelIdGenerator to control 
producing certain span and trace id."""
+        resource = Resource(attributes={HOST_NAME: get_hostname(), 
SERVICE_NAME: self.otel_service})
+        if trace_id or span_id:
+            # in case where trace_id or span_id was given
+            tracer_provider = TracerProvider(
+                resource=resource, 
id_generator=AirflowOtelIdGenerator(span_id=span_id, trace_id=trace_id)
+            )
+        else:
+            tracer_provider = TracerProvider(resource=resource)
+        tracer_provider.add_span_processor(self.span_processor)
+        tracer = tracer_provider.get_tracer(component)
+        """
+        Tracer will produce a single ID value if value is provided. Note that 
this is one-time only, so any
+        subsequent call will produce the normal random ids.
+        """
+        return tracer
+
+    def get_current_span(self):
+        return trace.get_current_span()
+
+    def use_span(self, span: Span):
+        return trace.use_span(span=span)
+
+    def start_span(
+        self,
+        span_name: str,
+        component: str | None = None,
+        parent_sc: SpanContext | None = None,
+        span_id=None,
+        links=None,
+        start_time=None,
+    ):
+        """Start a span; if service_name is not given, otel_service is used."""
+        if component is None:
+            component = self.otel_service
+
+        trace_id = self.get_current_span().get_span_context().trace_id
+        tracer = self.get_tracer(component=component, trace_id=trace_id, 
span_id=span_id)
+
+        attributes = parse_tracestate(self.tag_string) if self.tag_string else 
{}
+
+        if links is not None:
+            _links = gen_links_from_kv_list(links)
+        else:
+            _links = []
+
+        if start_time is not None:
+            start_time = datetime_to_nano(start_time)
+
+        if parent_sc is not None:
+            ctx = trace.set_span_in_context(NonRecordingSpan(parent_sc))
+            span = tracer.start_as_current_span(
+                span_name, context=ctx, attributes=attributes, links=_links, 
start_time=start_time
+            )
+        else:
+            span = tracer.start_as_current_span(
+                span_name, attributes=attributes, links=_links, 
start_time=start_time
+            )
+        return span
+
+    def start_span_from_dagrun(
+        self, dagrun, span_name: str | None = None, component: str = "dagrun", 
links=None
+    ):
+        """Produce a span from dag run."""
+        # check if dagrun has configs
+        conf = dagrun.conf
+        trace_id = int(gen_trace_id(dag_run=dagrun, as_int=True))
+        span_id = int(gen_dag_span_id(dag_run=dagrun, as_int=True))
+
+        if conf is not None:
+            traceparent = conf.get(TRACEPARENT)
+            tracestate = conf.get(TRACESTATE)
+
+        tracer = self.get_tracer(component=component, span_id=span_id, 
trace_id=trace_id)
+
+        if self.tag_string and tracestate:
+            tag_string = self.tag_string + "," + tracestate
+        else:
+            tag_string = self.tag_string or tracestate
+
+        if span_name is None:
+            span_name = dagrun.dag_id
+
+        if links is not None:
+            _links = gen_links_from_kv_list(links)
+        else:
+            _links = []
+
+        _links.append(
+            Link(
+                context=trace.get_current_span().get_span_context(),
+                attributes={"meta.annotation_type": "link", "from": 
"parenttrace"},
+            )
+        )
+
+        if traceparent is not None:
+            # add the trace parent as the link
+            _links.append(gen_link_from_traceparent(traceparent))
+
+        span_ctx = SpanContext(
+            trace_id=INVALID_TRACE_ID, span_id=INVALID_SPAN_ID, 
is_remote=True, trace_flags=TraceFlags(0x01)
+        )
+        ctx = trace.set_span_in_context(NonRecordingSpan(span_ctx))
+        span = tracer.start_as_current_span(
+            name=span_name,
+            context=ctx,
+            links=_links,
+            start_time=datetime_to_nano(dagrun.queued_at),
+            attributes=parse_tracestate(tag_string),
+        )
+        return span
+
+    def start_span_from_taskinstance(
+        self,
+        ti,
+        span_name: str | None = None,
+        component: str = "taskinstance",
+        child: bool = False,
+        links=None,
+    ):
+        """
+        Create and start span from given task instance.
+
+        Essentially the span represents the ti itself if child == True, it 
will create a 'child' span under the given span.
+        """
+        dagrun = ti.dag_run
+        trace_id = int(gen_trace_id(dag_run=dagrun, as_int=True))
+        span_id = int(gen_span_id(ti=ti, as_int=True))
+        if span_name is None:
+            span_name = ti.task_id
+
+        if child is False:
+            parent_id = int(gen_dag_span_id(dag_run=dagrun, as_int=True))
+        else:
+            parent_id = span_id

Review Comment:
   ```suggestion
           parent_id = span_id if child else 
int(gen_dag_span_id(dag_run=dagrun, as_int=True))
   ```



##########
airflow/traces/otel_tracer.py:
##########
@@ -0,0 +1,316 @@
+#
+# 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
+import random
+
+from opentelemetry import trace
+from opentelemetry.context import create_key
+from opentelemetry.exporter.otlp.proto.http.trace_exporter import 
OTLPSpanExporter
+from opentelemetry.sdk.resources import HOST_NAME, SERVICE_NAME, Resource
+from opentelemetry.sdk.trace import Span, Tracer as OpenTelemetryTracer, 
TracerProvider
+from opentelemetry.sdk.trace.export import BatchSpanProcessor, 
ConsoleSpanExporter
+from opentelemetry.sdk.trace.id_generator import IdGenerator
+from opentelemetry.trace import Link, NonRecordingSpan, SpanContext, 
TraceFlags, Tracer
+from opentelemetry.trace.span import INVALID_SPAN_ID, INVALID_TRACE_ID
+
+from airflow.configuration import conf
+from airflow.traces import (
+    TRACEPARENT,
+    TRACESTATE,
+)
+from airflow.traces.utils import (
+    gen_dag_span_id,
+    gen_span_id,
+    gen_trace_id,
+    parse_traceparent,
+    parse_tracestate,
+)
+from airflow.utils.dates import datetime_to_nano
+from airflow.utils.net import get_hostname
+
+log = logging.getLogger(__name__)
+
+_NEXT_ID = create_key("next_id")
+
+
+class OtelTrace:
+    """
+    Handle all tracing requirements such as getting the tracer, and starting a 
new span.
+
+    When OTEL is enabled, the Trace class will be replaced by this class.
+    """
+
+    def __init__(self, span_exporter: ConsoleSpanExporter | OTLPSpanExporter, 
tag_string: str | None = None):
+        self.span_exporter = span_exporter
+        self.span_processor = BatchSpanProcessor(self.span_exporter)
+        self.tag_string = tag_string
+        self.otel_service = conf.get("traces", "otel_service")
+
+    def get_tracer(
+        self, component: str, trace_id: int | None = None, span_id: int | None 
= None
+    ) -> OpenTelemetryTracer | Tracer:
+        """Tracer that will use special AirflowOtelIdGenerator to control 
producing certain span and trace id."""
+        resource = Resource(attributes={HOST_NAME: get_hostname(), 
SERVICE_NAME: self.otel_service})
+        if trace_id or span_id:
+            # in case where trace_id or span_id was given
+            tracer_provider = TracerProvider(
+                resource=resource, 
id_generator=AirflowOtelIdGenerator(span_id=span_id, trace_id=trace_id)
+            )
+        else:
+            tracer_provider = TracerProvider(resource=resource)
+        tracer_provider.add_span_processor(self.span_processor)
+        tracer = tracer_provider.get_tracer(component)
+        """
+        Tracer will produce a single ID value if value is provided. Note that 
this is one-time only, so any
+        subsequent call will produce the normal random ids.
+        """
+        return tracer
+
+    def get_current_span(self):
+        return trace.get_current_span()
+
+    def use_span(self, span: Span):
+        return trace.use_span(span=span)
+
+    def start_span(
+        self,
+        span_name: str,
+        component: str | None = None,
+        parent_sc: SpanContext | None = None,
+        span_id=None,
+        links=None,
+        start_time=None,
+    ):
+        """Start a span; if service_name is not given, otel_service is used."""
+        if component is None:
+            component = self.otel_service
+
+        trace_id = self.get_current_span().get_span_context().trace_id
+        tracer = self.get_tracer(component=component, trace_id=trace_id, 
span_id=span_id)
+
+        attributes = parse_tracestate(self.tag_string) if self.tag_string else 
{}
+
+        if links is not None:
+            _links = gen_links_from_kv_list(links)
+        else:
+            _links = []
+
+        if start_time is not None:
+            start_time = datetime_to_nano(start_time)
+
+        if parent_sc is not None:
+            ctx = trace.set_span_in_context(NonRecordingSpan(parent_sc))
+            span = tracer.start_as_current_span(
+                span_name, context=ctx, attributes=attributes, links=_links, 
start_time=start_time
+            )
+        else:
+            span = tracer.start_as_current_span(
+                span_name, attributes=attributes, links=_links, 
start_time=start_time
+            )
+        return span
+
+    def start_span_from_dagrun(
+        self, dagrun, span_name: str | None = None, component: str = "dagrun", 
links=None
+    ):
+        """Produce a span from dag run."""
+        # check if dagrun has configs
+        conf = dagrun.conf
+        trace_id = int(gen_trace_id(dag_run=dagrun, as_int=True))
+        span_id = int(gen_dag_span_id(dag_run=dagrun, as_int=True))
+
+        if conf is not None:

Review Comment:
   ```suggestion
           if conf:
   ```



##########
airflow/traces/otel_tracer.py:
##########
@@ -0,0 +1,316 @@
+#
+# 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
+import random
+
+from opentelemetry import trace
+from opentelemetry.context import create_key
+from opentelemetry.exporter.otlp.proto.http.trace_exporter import 
OTLPSpanExporter
+from opentelemetry.sdk.resources import HOST_NAME, SERVICE_NAME, Resource
+from opentelemetry.sdk.trace import Span, Tracer as OpenTelemetryTracer, 
TracerProvider
+from opentelemetry.sdk.trace.export import BatchSpanProcessor, 
ConsoleSpanExporter
+from opentelemetry.sdk.trace.id_generator import IdGenerator
+from opentelemetry.trace import Link, NonRecordingSpan, SpanContext, 
TraceFlags, Tracer
+from opentelemetry.trace.span import INVALID_SPAN_ID, INVALID_TRACE_ID
+
+from airflow.configuration import conf
+from airflow.traces import (
+    TRACEPARENT,
+    TRACESTATE,
+)
+from airflow.traces.utils import (
+    gen_dag_span_id,
+    gen_span_id,
+    gen_trace_id,
+    parse_traceparent,
+    parse_tracestate,
+)
+from airflow.utils.dates import datetime_to_nano
+from airflow.utils.net import get_hostname
+
+log = logging.getLogger(__name__)
+
+_NEXT_ID = create_key("next_id")
+
+
+class OtelTrace:
+    """
+    Handle all tracing requirements such as getting the tracer, and starting a 
new span.
+
+    When OTEL is enabled, the Trace class will be replaced by this class.
+    """
+
+    def __init__(self, span_exporter: ConsoleSpanExporter | OTLPSpanExporter, 
tag_string: str | None = None):
+        self.span_exporter = span_exporter
+        self.span_processor = BatchSpanProcessor(self.span_exporter)
+        self.tag_string = tag_string
+        self.otel_service = conf.get("traces", "otel_service")
+
+    def get_tracer(
+        self, component: str, trace_id: int | None = None, span_id: int | None 
= None
+    ) -> OpenTelemetryTracer | Tracer:
+        """Tracer that will use special AirflowOtelIdGenerator to control 
producing certain span and trace id."""
+        resource = Resource(attributes={HOST_NAME: get_hostname(), 
SERVICE_NAME: self.otel_service})
+        if trace_id or span_id:
+            # in case where trace_id or span_id was given
+            tracer_provider = TracerProvider(
+                resource=resource, 
id_generator=AirflowOtelIdGenerator(span_id=span_id, trace_id=trace_id)
+            )
+        else:
+            tracer_provider = TracerProvider(resource=resource)
+        tracer_provider.add_span_processor(self.span_processor)
+        tracer = tracer_provider.get_tracer(component)
+        """
+        Tracer will produce a single ID value if value is provided. Note that 
this is one-time only, so any
+        subsequent call will produce the normal random ids.
+        """
+        return tracer
+
+    def get_current_span(self):
+        return trace.get_current_span()
+
+    def use_span(self, span: Span):
+        return trace.use_span(span=span)
+
+    def start_span(
+        self,
+        span_name: str,
+        component: str | None = None,
+        parent_sc: SpanContext | None = None,
+        span_id=None,
+        links=None,
+        start_time=None,
+    ):
+        """Start a span; if service_name is not given, otel_service is used."""
+        if component is None:
+            component = self.otel_service
+
+        trace_id = self.get_current_span().get_span_context().trace_id
+        tracer = self.get_tracer(component=component, trace_id=trace_id, 
span_id=span_id)
+
+        attributes = parse_tracestate(self.tag_string) if self.tag_string else 
{}
+
+        if links is not None:
+            _links = gen_links_from_kv_list(links)
+        else:
+            _links = []
+
+        if start_time is not None:
+            start_time = datetime_to_nano(start_time)
+
+        if parent_sc is not None:
+            ctx = trace.set_span_in_context(NonRecordingSpan(parent_sc))
+            span = tracer.start_as_current_span(
+                span_name, context=ctx, attributes=attributes, links=_links, 
start_time=start_time
+            )
+        else:
+            span = tracer.start_as_current_span(
+                span_name, attributes=attributes, links=_links, 
start_time=start_time
+            )
+        return span
+
+    def start_span_from_dagrun(
+        self, dagrun, span_name: str | None = None, component: str = "dagrun", 
links=None
+    ):
+        """Produce a span from dag run."""
+        # check if dagrun has configs
+        conf = dagrun.conf
+        trace_id = int(gen_trace_id(dag_run=dagrun, as_int=True))
+        span_id = int(gen_dag_span_id(dag_run=dagrun, as_int=True))
+
+        if conf is not None:
+            traceparent = conf.get(TRACEPARENT)
+            tracestate = conf.get(TRACESTATE)
+
+        tracer = self.get_tracer(component=component, span_id=span_id, 
trace_id=trace_id)
+
+        if self.tag_string and tracestate:
+            tag_string = self.tag_string + "," + tracestate
+        else:
+            tag_string = self.tag_string or tracestate
+
+        if span_name is None:
+            span_name = dagrun.dag_id
+
+        if links is not None:
+            _links = gen_links_from_kv_list(links)
+        else:
+            _links = []

Review Comment:
   I thought I mentioned this one already, but maybe not.
   
   ```suggestion
           _links = gen_links_from_kv_list(links) if links else []
   ```



##########
airflow/traces/utils.py:
##########
@@ -0,0 +1,98 @@
+#
+# 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 typing import TYPE_CHECKING
+
+from airflow.utils.hashlib_wrapper import md5
+
+if TYPE_CHECKING:
+    from airflow.models import DagRun, TaskInstance
+    from airflow.models.taskinstancekey import TaskInstanceKey
+
+TRACE_ID = 0
+SPAN_ID = 16
+
+log = logging.getLogger(__name__)
+
+
+def _gen_id(seeds: list[str], as_int: bool = False, type: int = TRACE_ID) -> 
str | int:
+    seed_str = "_".join(seeds).encode("utf-8")
+    hash_hex = md5(seed_str).hexdigest()[type:]
+    return int(hash_hex, 16) if as_int else hash_hex
+
+
+def gen_trace_id(dag_run: DagRun, as_int: bool = False) -> str | int:
+    """Generate trace id from DagRun."""
+    return _gen_id([dag_run.dag_id, dag_run.run_id, 
str(dag_run.start_date.timestamp())], as_int)
+
+
+def gen_span_id_from_ti_key(ti_key: TaskInstanceKey, as_int: bool = False) -> 
str | int:
+    """Generate span id from TI key."""
+    return _gen_id([ti_key.dag_id, ti_key.run_id, ti_key.task_id, 
str(ti_key.try_number)], as_int, SPAN_ID)
+
+
+def gen_dag_span_id(dag_run: DagRun, as_int: bool = False) -> str | int:
+    """Generate dag's root span id using dag_run."""
+    return _gen_id([dag_run.dag_id, dag_run.run_id, 
str(dag_run.start_date.timestamp())], as_int, SPAN_ID)
+
+
+def gen_span_id(ti: TaskInstance, as_int: bool = False) -> str | int:
+    """Generate span id from the task instance."""
+    dag_run = ti.dag_run
+    """When this is called, the try_number of ti is already set to next(+1), 
hence the subtraction"""
+    return _gen_id([dag_run.dag_id, dag_run.run_id, ti.task_id, 
str(ti.try_number - 1)], as_int, SPAN_ID)
+
+
+def parse_traceparent(traceparent_str: str | None = None) -> dict:
+    """Parse traceparent string: 
00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01."""
+    if traceparent_str is None:
+        return {}
+    tokens = traceparent_str.split("-")
+    if len(tokens) != 4:
+        raise ValueError("The traceparent string does not have the correct 
format.")
+    return {"version": tokens[0], "trace_id": tokens[1], "parent_id": 
tokens[2], "flags": tokens[3]}
+
+
+def parse_tracestate(tracestate_str: str | None = None) -> dict:
+    """Parse tracestate string: rojo=00f067aa0ba902b7,congo=t61rcWkgMzE."""
+    if tracestate_str is None:
+        return {}
+    tokens = tracestate_str.split(",")
+    result = {}
+    for pair in tokens:
+        key, value = pair.split("=")
+        result[key.strip()] = value.strip()
+    return result
+
+
+def is_valid_trace_id(trace_id: str) -> bool:
+    """Check whether trace id is valid."""
+    if trace_id is not None and len(trace_id) == 34 and int(trace_id, 16) != 0:
+        return True
+    else:
+        return False

Review Comment:
   ```suggestion
       return trace_id and len(trace_id) == 34 and int(trace_id, 16) != 0
   ```



##########
airflow/traces/otel_tracer.py:
##########
@@ -0,0 +1,316 @@
+#
+# 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
+import random
+
+from opentelemetry import trace
+from opentelemetry.context import create_key
+from opentelemetry.exporter.otlp.proto.http.trace_exporter import 
OTLPSpanExporter
+from opentelemetry.sdk.resources import HOST_NAME, SERVICE_NAME, Resource
+from opentelemetry.sdk.trace import Span, Tracer as OpenTelemetryTracer, 
TracerProvider
+from opentelemetry.sdk.trace.export import BatchSpanProcessor, 
ConsoleSpanExporter
+from opentelemetry.sdk.trace.id_generator import IdGenerator
+from opentelemetry.trace import Link, NonRecordingSpan, SpanContext, 
TraceFlags, Tracer
+from opentelemetry.trace.span import INVALID_SPAN_ID, INVALID_TRACE_ID
+
+from airflow.configuration import conf
+from airflow.traces import (
+    TRACEPARENT,
+    TRACESTATE,
+)
+from airflow.traces.utils import (
+    gen_dag_span_id,
+    gen_span_id,
+    gen_trace_id,
+    parse_traceparent,
+    parse_tracestate,
+)
+from airflow.utils.dates import datetime_to_nano
+from airflow.utils.net import get_hostname
+
+log = logging.getLogger(__name__)
+
+_NEXT_ID = create_key("next_id")
+
+
+class OtelTrace:
+    """
+    Handle all tracing requirements such as getting the tracer, and starting a 
new span.
+
+    When OTEL is enabled, the Trace class will be replaced by this class.
+    """
+
+    def __init__(self, span_exporter: ConsoleSpanExporter | OTLPSpanExporter, 
tag_string: str | None = None):
+        self.span_exporter = span_exporter
+        self.span_processor = BatchSpanProcessor(self.span_exporter)
+        self.tag_string = tag_string
+        self.otel_service = conf.get("traces", "otel_service")
+
+    def get_tracer(
+        self, component: str, trace_id: int | None = None, span_id: int | None 
= None
+    ) -> OpenTelemetryTracer | Tracer:
+        """Tracer that will use special AirflowOtelIdGenerator to control 
producing certain span and trace id."""
+        resource = Resource(attributes={HOST_NAME: get_hostname(), 
SERVICE_NAME: self.otel_service})
+        if trace_id or span_id:
+            # in case where trace_id or span_id was given
+            tracer_provider = TracerProvider(
+                resource=resource, 
id_generator=AirflowOtelIdGenerator(span_id=span_id, trace_id=trace_id)
+            )
+        else:
+            tracer_provider = TracerProvider(resource=resource)
+        tracer_provider.add_span_processor(self.span_processor)
+        tracer = tracer_provider.get_tracer(component)
+        """
+        Tracer will produce a single ID value if value is provided. Note that 
this is one-time only, so any
+        subsequent call will produce the normal random ids.
+        """
+        return tracer
+
+    def get_current_span(self):
+        return trace.get_current_span()
+
+    def use_span(self, span: Span):
+        return trace.use_span(span=span)
+
+    def start_span(
+        self,
+        span_name: str,
+        component: str | None = None,
+        parent_sc: SpanContext | None = None,
+        span_id=None,
+        links=None,
+        start_time=None,
+    ):
+        """Start a span; if service_name is not given, otel_service is used."""
+        if component is None:
+            component = self.otel_service
+
+        trace_id = self.get_current_span().get_span_context().trace_id
+        tracer = self.get_tracer(component=component, trace_id=trace_id, 
span_id=span_id)
+
+        attributes = parse_tracestate(self.tag_string) if self.tag_string else 
{}
+
+        if links is not None:
+            _links = gen_links_from_kv_list(links)
+        else:
+            _links = []
+
+        if start_time is not None:
+            start_time = datetime_to_nano(start_time)
+
+        if parent_sc is not None:
+            ctx = trace.set_span_in_context(NonRecordingSpan(parent_sc))
+            span = tracer.start_as_current_span(
+                span_name, context=ctx, attributes=attributes, links=_links, 
start_time=start_time
+            )
+        else:
+            span = tracer.start_as_current_span(
+                span_name, attributes=attributes, links=_links, 
start_time=start_time
+            )
+        return span
+
+    def start_span_from_dagrun(
+        self, dagrun, span_name: str | None = None, component: str = "dagrun", 
links=None
+    ):
+        """Produce a span from dag run."""
+        # check if dagrun has configs
+        conf = dagrun.conf
+        trace_id = int(gen_trace_id(dag_run=dagrun, as_int=True))
+        span_id = int(gen_dag_span_id(dag_run=dagrun, as_int=True))
+
+        if conf is not None:
+            traceparent = conf.get(TRACEPARENT)
+            tracestate = conf.get(TRACESTATE)
+
+        tracer = self.get_tracer(component=component, span_id=span_id, 
trace_id=trace_id)
+
+        if self.tag_string and tracestate:
+            tag_string = self.tag_string + "," + tracestate
+        else:
+            tag_string = self.tag_string or tracestate
+
+        if span_name is None:
+            span_name = dagrun.dag_id
+
+        if links is not None:
+            _links = gen_links_from_kv_list(links)
+        else:
+            _links = []
+
+        _links.append(
+            Link(
+                context=trace.get_current_span().get_span_context(),
+                attributes={"meta.annotation_type": "link", "from": 
"parenttrace"},
+            )
+        )
+
+        if traceparent is not None:
+            # add the trace parent as the link
+            _links.append(gen_link_from_traceparent(traceparent))
+
+        span_ctx = SpanContext(
+            trace_id=INVALID_TRACE_ID, span_id=INVALID_SPAN_ID, 
is_remote=True, trace_flags=TraceFlags(0x01)
+        )
+        ctx = trace.set_span_in_context(NonRecordingSpan(span_ctx))
+        span = tracer.start_as_current_span(
+            name=span_name,
+            context=ctx,
+            links=_links,
+            start_time=datetime_to_nano(dagrun.queued_at),
+            attributes=parse_tracestate(tag_string),
+        )
+        return span
+
+    def start_span_from_taskinstance(
+        self,
+        ti,
+        span_name: str | None = None,
+        component: str = "taskinstance",
+        child: bool = False,
+        links=None,
+    ):
+        """
+        Create and start span from given task instance.
+
+        Essentially the span represents the ti itself if child == True, it 
will create a 'child' span under the given span.
+        """
+        dagrun = ti.dag_run
+        trace_id = int(gen_trace_id(dag_run=dagrun, as_int=True))
+        span_id = int(gen_span_id(ti=ti, as_int=True))
+        if span_name is None:
+            span_name = ti.task_id
+
+        if child is False:
+            parent_id = int(gen_dag_span_id(dag_run=dagrun, as_int=True))
+        else:
+            parent_id = span_id
+
+        span_ctx = SpanContext(
+            trace_id=trace_id, span_id=parent_id, is_remote=True, 
trace_flags=TraceFlags(0x01)
+        )
+
+        if links is not None:
+            _links = gen_links_from_kv_list(links)
+        else:
+            _links = []
+
+        a_link = Link(
+            context=trace.get_current_span().get_span_context(),
+            attributes={"meta.annotation_type": "link", "from": "parenttrace"},
+        )
+        _links.append(a_link)
+
+        if child is False:
+            tracer = self.get_tracer(component=component, span_id=span_id, 
trace_id=trace_id)
+        else:
+            tracer = self.get_tracer(component=component)

Review Comment:
   Yeah, we've definitely already discussed this.   Maybe double check if 
something went wrong on your last push?  I'm going to skip the rest of this 
file for now.



##########
airflow/traces/otel_tracer.py:
##########
@@ -0,0 +1,316 @@
+#
+# 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
+import random
+
+from opentelemetry import trace
+from opentelemetry.context import create_key
+from opentelemetry.exporter.otlp.proto.http.trace_exporter import 
OTLPSpanExporter
+from opentelemetry.sdk.resources import HOST_NAME, SERVICE_NAME, Resource
+from opentelemetry.sdk.trace import Span, Tracer as OpenTelemetryTracer, 
TracerProvider
+from opentelemetry.sdk.trace.export import BatchSpanProcessor, 
ConsoleSpanExporter
+from opentelemetry.sdk.trace.id_generator import IdGenerator
+from opentelemetry.trace import Link, NonRecordingSpan, SpanContext, 
TraceFlags, Tracer
+from opentelemetry.trace.span import INVALID_SPAN_ID, INVALID_TRACE_ID
+
+from airflow.configuration import conf
+from airflow.traces import (
+    TRACEPARENT,
+    TRACESTATE,
+)
+from airflow.traces.utils import (
+    gen_dag_span_id,
+    gen_span_id,
+    gen_trace_id,
+    parse_traceparent,
+    parse_tracestate,
+)
+from airflow.utils.dates import datetime_to_nano
+from airflow.utils.net import get_hostname
+
+log = logging.getLogger(__name__)
+
+_NEXT_ID = create_key("next_id")
+
+
+class OtelTrace:
+    """
+    Handle all tracing requirements such as getting the tracer, and starting a 
new span.
+
+    When OTEL is enabled, the Trace class will be replaced by this class.
+    """
+
+    def __init__(self, span_exporter: ConsoleSpanExporter | OTLPSpanExporter, 
tag_string: str | None = None):
+        self.span_exporter = span_exporter
+        self.span_processor = BatchSpanProcessor(self.span_exporter)
+        self.tag_string = tag_string
+        self.otel_service = conf.get("traces", "otel_service")
+
+    def get_tracer(
+        self, component: str, trace_id: int | None = None, span_id: int | None 
= None
+    ) -> OpenTelemetryTracer | Tracer:
+        """Tracer that will use special AirflowOtelIdGenerator to control 
producing certain span and trace id."""
+        resource = Resource(attributes={HOST_NAME: get_hostname(), 
SERVICE_NAME: self.otel_service})
+        if trace_id or span_id:
+            # in case where trace_id or span_id was given
+            tracer_provider = TracerProvider(
+                resource=resource, 
id_generator=AirflowOtelIdGenerator(span_id=span_id, trace_id=trace_id)
+            )
+        else:
+            tracer_provider = TracerProvider(resource=resource)
+        tracer_provider.add_span_processor(self.span_processor)
+        tracer = tracer_provider.get_tracer(component)
+        """
+        Tracer will produce a single ID value if value is provided. Note that 
this is one-time only, so any
+        subsequent call will produce the normal random ids.
+        """
+        return tracer
+
+    def get_current_span(self):
+        return trace.get_current_span()
+
+    def use_span(self, span: Span):
+        return trace.use_span(span=span)
+
+    def start_span(
+        self,
+        span_name: str,
+        component: str | None = None,
+        parent_sc: SpanContext | None = None,
+        span_id=None,
+        links=None,
+        start_time=None,
+    ):
+        """Start a span; if service_name is not given, otel_service is used."""
+        if component is None:
+            component = self.otel_service
+
+        trace_id = self.get_current_span().get_span_context().trace_id
+        tracer = self.get_tracer(component=component, trace_id=trace_id, 
span_id=span_id)
+
+        attributes = parse_tracestate(self.tag_string) if self.tag_string else 
{}
+
+        if links is not None:
+            _links = gen_links_from_kv_list(links)
+        else:
+            _links = []
+
+        if start_time is not None:
+            start_time = datetime_to_nano(start_time)
+
+        if parent_sc is not None:
+            ctx = trace.set_span_in_context(NonRecordingSpan(parent_sc))
+            span = tracer.start_as_current_span(
+                span_name, context=ctx, attributes=attributes, links=_links, 
start_time=start_time
+            )
+        else:
+            span = tracer.start_as_current_span(
+                span_name, attributes=attributes, links=_links, 
start_time=start_time
+            )
+        return span
+
+    def start_span_from_dagrun(
+        self, dagrun, span_name: str | None = None, component: str = "dagrun", 
links=None
+    ):
+        """Produce a span from dag run."""
+        # check if dagrun has configs
+        conf = dagrun.conf
+        trace_id = int(gen_trace_id(dag_run=dagrun, as_int=True))
+        span_id = int(gen_dag_span_id(dag_run=dagrun, as_int=True))
+
+        if conf is not None:
+            traceparent = conf.get(TRACEPARENT)
+            tracestate = conf.get(TRACESTATE)
+
+        tracer = self.get_tracer(component=component, span_id=span_id, 
trace_id=trace_id)
+
+        if self.tag_string and tracestate:
+            tag_string = self.tag_string + "," + tracestate
+        else:
+            tag_string = self.tag_string or tracestate
+
+        if span_name is None:
+            span_name = dagrun.dag_id
+
+        if links is not None:
+            _links = gen_links_from_kv_list(links)
+        else:
+            _links = []
+
+        _links.append(
+            Link(
+                context=trace.get_current_span().get_span_context(),
+                attributes={"meta.annotation_type": "link", "from": 
"parenttrace"},
+            )
+        )
+
+        if traceparent is not None:
+            # add the trace parent as the link
+            _links.append(gen_link_from_traceparent(traceparent))
+
+        span_ctx = SpanContext(
+            trace_id=INVALID_TRACE_ID, span_id=INVALID_SPAN_ID, 
is_remote=True, trace_flags=TraceFlags(0x01)
+        )
+        ctx = trace.set_span_in_context(NonRecordingSpan(span_ctx))
+        span = tracer.start_as_current_span(
+            name=span_name,
+            context=ctx,
+            links=_links,
+            start_time=datetime_to_nano(dagrun.queued_at),
+            attributes=parse_tracestate(tag_string),
+        )
+        return span
+
+    def start_span_from_taskinstance(
+        self,
+        ti,
+        span_name: str | None = None,
+        component: str = "taskinstance",
+        child: bool = False,
+        links=None,
+    ):
+        """
+        Create and start span from given task instance.
+
+        Essentially the span represents the ti itself if child == True, it 
will create a 'child' span under the given span.
+        """
+        dagrun = ti.dag_run
+        trace_id = int(gen_trace_id(dag_run=dagrun, as_int=True))
+        span_id = int(gen_span_id(ti=ti, as_int=True))
+        if span_name is None:
+            span_name = ti.task_id
+
+        if child is False:
+            parent_id = int(gen_dag_span_id(dag_run=dagrun, as_int=True))
+        else:
+            parent_id = span_id
+
+        span_ctx = SpanContext(
+            trace_id=trace_id, span_id=parent_id, is_remote=True, 
trace_flags=TraceFlags(0x01)
+        )
+
+        if links is not None:
+            _links = gen_links_from_kv_list(links)
+        else:
+            _links = []

Review Comment:
   ```suggestion
           _links = gen_links_from_kv_list(links) if links else []
   ```



##########
airflow/traces/utils.py:
##########
@@ -0,0 +1,98 @@
+#
+# 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 typing import TYPE_CHECKING
+
+from airflow.utils.hashlib_wrapper import md5
+
+if TYPE_CHECKING:
+    from airflow.models import DagRun, TaskInstance
+    from airflow.models.taskinstancekey import TaskInstanceKey
+
+TRACE_ID = 0
+SPAN_ID = 16
+
+log = logging.getLogger(__name__)
+
+
+def _gen_id(seeds: list[str], as_int: bool = False, type: int = TRACE_ID) -> 
str | int:
+    seed_str = "_".join(seeds).encode("utf-8")
+    hash_hex = md5(seed_str).hexdigest()[type:]
+    return int(hash_hex, 16) if as_int else hash_hex
+
+
+def gen_trace_id(dag_run: DagRun, as_int: bool = False) -> str | int:
+    """Generate trace id from DagRun."""
+    return _gen_id([dag_run.dag_id, dag_run.run_id, 
str(dag_run.start_date.timestamp())], as_int)
+
+
+def gen_span_id_from_ti_key(ti_key: TaskInstanceKey, as_int: bool = False) -> 
str | int:
+    """Generate span id from TI key."""
+    return _gen_id([ti_key.dag_id, ti_key.run_id, ti_key.task_id, 
str(ti_key.try_number)], as_int, SPAN_ID)
+
+
+def gen_dag_span_id(dag_run: DagRun, as_int: bool = False) -> str | int:
+    """Generate dag's root span id using dag_run."""
+    return _gen_id([dag_run.dag_id, dag_run.run_id, 
str(dag_run.start_date.timestamp())], as_int, SPAN_ID)
+
+
+def gen_span_id(ti: TaskInstance, as_int: bool = False) -> str | int:
+    """Generate span id from the task instance."""
+    dag_run = ti.dag_run
+    """When this is called, the try_number of ti is already set to next(+1), 
hence the subtraction"""
+    return _gen_id([dag_run.dag_id, dag_run.run_id, ti.task_id, 
str(ti.try_number - 1)], as_int, SPAN_ID)
+
+
+def parse_traceparent(traceparent_str: str | None = None) -> dict:
+    """Parse traceparent string: 
00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01."""
+    if traceparent_str is None:
+        return {}
+    tokens = traceparent_str.split("-")
+    if len(tokens) != 4:
+        raise ValueError("The traceparent string does not have the correct 
format.")
+    return {"version": tokens[0], "trace_id": tokens[1], "parent_id": 
tokens[2], "flags": tokens[3]}
+
+
+def parse_tracestate(tracestate_str: str | None = None) -> dict:
+    """Parse tracestate string: rojo=00f067aa0ba902b7,congo=t61rcWkgMzE."""
+    if tracestate_str is None:
+        return {}
+    tokens = tracestate_str.split(",")
+    result = {}
+    for pair in tokens:
+        key, value = pair.split("=")
+        result[key.strip()] = value.strip()
+    return result
+
+
+def is_valid_trace_id(trace_id: str) -> bool:
+    """Check whether trace id is valid."""
+    if trace_id is not None and len(trace_id) == 34 and int(trace_id, 16) != 0:
+        return True
+    else:
+        return False
+
+
+def is_valid_span_id(span_id: str) -> bool:
+    """Check whether span id is valid."""
+    if span_id is not None and len(span_id) == 18 and int(span_id, 16) != 0:
+        return True
+    else:
+        return False

Review Comment:
   ```suggestion
       return span_id and len(span_id) == 18 and int(span_id, 16) != 0
   ```



##########
airflow/traces/utils.py:
##########
@@ -0,0 +1,98 @@
+#
+# 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 typing import TYPE_CHECKING
+
+from airflow.utils.hashlib_wrapper import md5
+
+if TYPE_CHECKING:
+    from airflow.models import DagRun, TaskInstance
+    from airflow.models.taskinstancekey import TaskInstanceKey
+
+TRACE_ID = 0
+SPAN_ID = 16
+
+log = logging.getLogger(__name__)
+
+
+def _gen_id(seeds: list[str], as_int: bool = False, type: int = TRACE_ID) -> 
str | int:
+    seed_str = "_".join(seeds).encode("utf-8")
+    hash_hex = md5(seed_str).hexdigest()[type:]
+    return int(hash_hex, 16) if as_int else hash_hex
+
+
+def gen_trace_id(dag_run: DagRun, as_int: bool = False) -> str | int:
+    """Generate trace id from DagRun."""
+    return _gen_id([dag_run.dag_id, dag_run.run_id, 
str(dag_run.start_date.timestamp())], as_int)
+
+

Review Comment:
   Now that you see it in action, how do you feel about this helper method way? 
  I generally go for positional arguments like you did here, but I wonder if 
using arg names in these might help clear it up a little?   I'll leave it up to 
you, but I wonder if it would be easeier tor ead as 
   
   ```
   def gen_trace_id(dag_run: DagRun, as_int: bool = False) -> str | int:
       """Generate trace id from DagRun."""
       return _gen_id(
               seeds=[dag_run.dag_id, dag_run.run_id, 
str(dag_run.start_date.timestamp())], 
               as_int,
           )
   ```
   
   The way it is, it's easy to lose the `as_int` in the visual clutter of the 
list, and adding the trailing comma in this case enforces the linter to 
separate them by line and adding "seeds=" maybe helps a bit visually here?  If 
you change "as_int" to an integer length as you mentioned, then using the name 
there would go a long way to making it visually distinct as well, I think.....  
 
   
   I don't know, just a thought, feel free to ignore.  Style and visual tricks 
like that are pretty much up tot he individual.



##########
airflow/traces/tracer.py:
##########
@@ -0,0 +1,280 @@
+#
+# 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 inspect
+import logging
+import socket
+from typing import TYPE_CHECKING, Any, Callable
+
+from airflow.configuration import conf
+from airflow.typing_compat import Protocol
+
+log = logging.getLogger(__name__)
+
+
+def gen_context(trace_id, span_id):
+    """Generate span context from trace_id and span_id."""
+    from airflow.traces.otel_tracer import gen_context as otel_gen_context

Review Comment:
   Yeah, something is definitely up, I've already seen a commit where you moved 
these tot he top import list.  Going to skip looking ta this file as well for 
now.



##########
airflow/traces/utils.py:
##########
@@ -18,52 +18,66 @@
 from __future__ import annotations
 
 import logging
+from typing import TYPE_CHECKING
 
 from airflow.utils.hashlib_wrapper import md5
 
+if TYPE_CHECKING:
+    from airflow.models import DagRun, TaskInstance
+    from airflow.models.taskinstancekey import TaskInstanceKey
+
 log = logging.getLogger(__name__)
 
 

Review Comment:
   If you want to get complicated, you could make a constant which holds the 
acceptable values for size and only accept those values, but I think that's 
overkill here.



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