nickstenning commented on code in PR #62554:
URL: https://github.com/apache/airflow/pull/62554#discussion_r2867590193
##########
task-sdk/src/airflow/sdk/observability/trace.py:
##########
@@ -17,106 +17,46 @@
from __future__ import annotations
import logging
-from collections.abc import Callable
+import warnings
from functools import wraps
-from socket import socket
-from typing import TYPE_CHECKING
-from airflow.sdk._shared.observability.traces.base_tracer import EmptyTrace,
Tracer
-from airflow.sdk.configuration import conf
+from opentelemetry import trace
+
+from airflow.sdk.exceptions import RemovedInAirflow4Warning
log = logging.getLogger(__name__)
+tracer = trace.get_tracer(__name__)
+
def add_debug_span(func):
"""Decorate a function with span."""
- func_name = func.__name__
- qual_name = func.__qualname__
- module_name = func.__module__
- component = qual_name.rsplit(".", 1)[0] if "." in qual_name else
module_name
+ warnings.warn(
+ "The `add_debug_span` class is deprecated. Do not use it.",
+ category=RemovedInAirflow4Warning,
+ stacklevel=1,
+ )
@wraps(func)
def wrapper(*args, **kwargs):
- with DebugTrace.start_span(span_name=func_name, component=component):
+ with tracer.start_as_current_span(func.__name__):
return func(*args, **kwargs)
return wrapper
-class _TraceMeta(type):
- factory: Callable[[], Tracer] | None = None
- instance: Tracer | EmptyTrace | None = None
-
- def __new__(cls, name, bases, attrs):
- # Read the debug flag from the class body.
- if "check_debug_traces_flag" not in attrs:
- raise TypeError(f"Class '{name}' must define
'check_debug_traces_flag'.")
-
- return super().__new__(cls, name, bases, attrs)
-
- def __getattr__(cls, name: str):
- if not cls.factory:
- # Lazy initialization of the factory
- cls.configure_factory()
- if not cls.instance:
- cls._initialize_instance()
- return getattr(cls.instance, name)
-
- def _initialize_instance(cls):
- """Initialize the trace instance."""
- try:
- cls.instance = cls.factory()
- except (socket.gaierror, ImportError) as e:
- log.error("Could not configure Trace: %s. Using EmptyTrace
instead.", e)
- cls.instance = EmptyTrace()
-
- def __call__(cls, *args, **kwargs):
- """Ensure the class behaves as a singleton."""
- if not cls.instance:
- cls._initialize_instance()
- return cls.instance
-
- def configure_factory(cls):
- """Configure the trace factory based on settings."""
- otel_on = conf.getboolean("traces", "otel_on")
-
- if cls.check_debug_traces_flag:
- debug_traces_on = conf.getboolean("traces", "otel_debug_traces_on")
- else:
- # Set to true so that it will be ignored during the evaluation for
the factory instance.
- # If this is true, then (otel_on and debug_traces_on) will always
evaluate to
- # whatever value 'otel_on' has and therefore it will be ignored.
- debug_traces_on = True
-
- if otel_on and debug_traces_on:
- from airflow.sdk.observability.traces import otel_tracer
-
- cls.factory = staticmethod(
- lambda use_simple_processor=False:
otel_tracer.get_otel_tracer(cls, use_simple_processor)
- )
- else:
- # EmptyTrace is a class and not inherently callable.
- # Using a lambda ensures it can be invoked as a callable factory.
- # staticmethod ensures the lambda is treated as a standalone
function
- # and avoids passing `cls` as an implicit argument.
- cls.factory = staticmethod(lambda: EmptyTrace())
-
- def get_constant_tags(cls) -> str | None:
- """Get constant tags to add to all traces."""
- return conf.get("traces", "tags", fallback=None)
-
-
-if TYPE_CHECKING:
- Trace: EmptyTrace
- DebugTrace: EmptyTrace
-else:
-
- class Trace(metaclass=_TraceMeta):
- """Empty class for Trace - we use metaclass to inject the right one."""
-
- check_debug_traces_flag = False
-
- class DebugTrace(metaclass=_TraceMeta):
- """Empty class for Trace and in case the debug traces flag is
enabled."""
-
- check_debug_traces_flag = True
+def __getattr__(name: str):
+ if name == "add_debug_span":
+ return add_debug_span
+ if name in ("Trace", "DebugTrace"):
+ from airflow.sdk._shared.observability.traces.otel_tracer import
OtelTrace
+
+ warnings.warn(
+ "You are trying to import Trace or DebugTrace from
airflow.sdk.observability.trace."
+ "These classes are deprecated. Do not use them! Instead, create
traces traces with "
+ "`from airflow.sdk.opentelemetry import trace; tracer =
trace.get_tracer(__name__)`.",
Review Comment:
There is already an observability package, right? If so I wouldn't confuse
things by butting it somewhere else.
--
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]