kaxil commented on code in PR #44216:
URL: https://github.com/apache/airflow/pull/44216#discussion_r1850621820


##########
airflow/traces/tracer.py:
##########
@@ -243,41 +240,51 @@ def start_span_from_taskinstance(
         return EMPTY_SPAN
 
 
-class _Trace(type):
-    factory: Callable
+class _TraceMeta(type):
+    factory: Callable[[], Tracer] | None = None
     instance: Tracer | EmptyTrace | None = None
 
-    def __getattr__(cls, name: str) -> str:
+    def __getattr__(cls, name: str):
         if not cls.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()
+            cls._initialize_instance()
         return getattr(cls.instance, name)
 
-    def __init__(cls, *args, **kwargs) -> None:
-        super().__init__(cls)
-        if not hasattr(cls.__class__, "factory"):
-            if conf.has_option("traces", "otel_on") and 
conf.getboolean("traces", "otel_on"):
-                from airflow.traces import otel_tracer
+    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()
 
-                cls.__class__.factory = otel_tracer.get_otel_tracer
-            else:
-                cls.__class__.factory = EmptyTrace
-
-    @classmethod
-    def get_constant_tags(cls) -> str | None:
-        """Get constant tags to add to all traces."""
-        tags_in_string = conf.get("traces", "tags", fallback=None)
-        if not tags_in_string:
-            return None
-        return tags_in_string
+    def __call__(cls, *args, **kwargs):
+        """Ensure the class behaves as a singleton."""
+        if not cls.instance:
+            cls._initialize_instance()
+        return cls.instance
 
 
 if TYPE_CHECKING:
     Trace: EmptyTrace
 else:
 
-    class Trace(metaclass=_Trace):
-        """Empty class for Trace - we use metaclass to inject the right one."""
+    class Trace(metaclass=_TraceMeta):
+        """Trace class to get the OTel tracer, falls back to EmptyTrace if not 
configured."""
+
+        @classmethod
+        def configure_factory(cls):
+            """Configure the trace factory based on settings."""
+            if conf.has_option("traces", "otel_on") and 
conf.getboolean("traces", "otel_on"):
+                from airflow.traces import otel_tracer
+
+                cls.factory = otel_tracer.get_otel_tracer
+            else:
+                # Since EmptyTrace is a Class & not a callable, using lambda 
here
+                cls.factory = lambda: EmptyTrace()
+
+        @classmethod
+        def get_constant_tags(cls) -> str | None:
+            """Get constant tags to add to all traces."""
+            return conf.get("traces", "tags", fallback=None)
+
+    Trace.configure_factory()

Review Comment:
   True -- refactored in 
https://github.com/apache/airflow/pull/44216/commits/36d3f50c200ff8eda6c75fd9343538ec56909346



##########
airflow/traces/tracer.py:
##########
@@ -54,19 +55,15 @@ def wrapper(*args, **kwargs):
         else:
             component = module_name

Review Comment:
   Done in 
https://github.com/apache/airflow/pull/44216/commits/36d3f50c200ff8eda6c75fd9343538ec56909346



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