Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions airflow-core/src/airflow/traces/otel_tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import logging
import random
from contextlib import AbstractContextManager
from typing import TYPE_CHECKING

from opentelemetry import trace
Expand Down Expand Up @@ -247,7 +248,7 @@ def _new_span(
links=None,
start_time=None,
start_as_current: bool = True,
):
) -> AbstractContextManager[trace.span.Span] | trace.span.Span:
if component is None:
component = self.otel_service

Expand All @@ -260,24 +261,24 @@ def _new_span(
links = []

if start_as_current:
span = tracer.start_as_current_span(
name=span_name,
context=parent_context,
links=links,
start_time=datetime_to_nano(start_time),
)
else:
span = tracer.start_span(
return tracer.start_as_current_span(
name=span_name,
context=parent_context,
links=links,
start_time=datetime_to_nano(start_time),
)
current_span_ctx = trace.set_span_in_context(NonRecordingSpan(span.get_span_context()))
# We have to manually make the span context as the active context.
# If the span needs to be injected into the carrier, then this is needed to make sure
# that the injected context will point to the span context that was just created.
attach(current_span_ctx)

span = tracer.start_span(
name=span_name,
context=parent_context,
links=links,
start_time=datetime_to_nano(start_time),
)
current_span_ctx = trace.set_span_in_context(NonRecordingSpan(span.get_span_context()))
# We have to manually make the span context as the active context.
# If the span needs to be injected into the carrier, then this is needed to make sure
# that the injected context will point to the span context that was just created.
attach(current_span_ctx)
return span

def inject(self) -> dict:
Expand Down