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


##########
airflow/traces/tracer.py:
##########
@@ -0,0 +1,256 @@
+#
+# 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 socket
+import typing
+from typing import (
+    TYPE_CHECKING,
+    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
+
+    return otel_gen_context(trace_id, span_id)
+
+
+def gen_links_from_kv_list(list):
+    """Generate links from kv list of {trace_id:int, span_id:int}."""
+    from airflow.traces.otel_tracer import gen_links_from_kv_list
+
+    return gen_links_from_kv_list(list)
+
+
+def span(func):
+    """Decorate a function with span."""
+
+    def wrapper(*args, **kwargs):
+        func_name = func.__name__
+        qual_name = func.__qualname__
+        module_name = func.__module__
+        if "." in qual_name:
+            component = f"{qual_name.rsplit('.', 1)[0]}"
+        else:
+            component = module_name
+        with Trace.start_span(span_name=func_name, component=component):
+            return func(*args, **kwargs)
+
+    return wrapper
+
+
+class DummyContext:
+    """If no Tracer is configured, DummyContext is used as a fallback."""
+
+    def __init__(self):
+        self.trace_id = 1
+
+
+class DummySpan:
+    """If no Tracer is configured, DummySpan is used as a fallback."""
+
+    def __enter__(self):
+        """Enter."""
+        return self
+
+    def __exit__(self, *args, **kwargs):
+        """Exit."""
+        pass
+
+    def __call__(self, obj):
+        """Call."""
+        return obj
+
+    def get_span_context(self):
+        """Get span context."""
+        return DUMMY_CTX
+
+    def set_attribute(self, key, value) -> None:
+        """Set an attribute to the span."""
+        pass
+
+    def set_attributes(self, attributes) -> None:
+        """Set multiple attributes at once."""
+        pass
+
+    def add_event(
+        self,
+        name: str,
+        attributes=None,

Review Comment:
   Maybe a suggestion... shouldn't OtelTrace inherit Tracer?  If so, you can 
add a field tot eh Tracer class which holds the type hint and override that in 
the various tracers as they are implemented.   For example:
   
   ```class Tracer(Protocol):
       """This class is only used for TypeChecking (for IDEs, mypy, etc)."""
   
       instance: Tracer | DummyTrace | None = None
       attribute_type = NewType(Any, None)
   ```    
   
   Then in OtelTrace
   
   ```
   class OtelTrace:
           attribute_type = NewType(OtelAttribute, None)  # or whatever it is 
supposed to be...
   ```        
   
   That should let you define the type hint as `attribute_type` and it should 
be enforced depending on which tracer is being used.....
   
   
   example showing the inheritence in action:
   
   ```
   In [1]: class A:
      ...:     banana = "banana"
      ...: 
   
   In [2]: class B(A):
      ...:     banana = "apple"
      ...: 
   
   In [3]: class C(A):
      ...:     # banana not defined
      ...: 
   
   In [4]: A.banana
   Out[4]: 'banana'
   
   In [5]: B.banana
   Out[5]: 'apple'
   
   In [6]: C.banana
   Out[6]: 'banana'
   ```



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