@@ -61,18 +61,45 @@ def internal_method(self):
6161from collections .abc import Callable
6262from typing import Any , TypeAlias
6363
64- from opentelemetry import trace
65- from opentelemetry .trace import SpanKind as _SpanKind
66- from opentelemetry .trace import StatusCode
6764
65+ logger = logging .getLogger (__name__ )
66+
67+ try :
68+ from opentelemetry import trace
69+ from opentelemetry .trace import SpanKind as _SpanKind
70+ from opentelemetry .trace import StatusCode
71+
72+ except ImportError :
73+ logger .debug (
74+ 'OpenTelemetry not found. Tracing will be disabled. '
75+ 'Install with: \' pip install "a2a-sdk[telemetry]"\' '
76+ )
77+
78+ class _NoOp :
79+ """A no-op object that absorbs all tracing calls when OpenTelemetry is not installed."""
80+
81+ def __call__ (self , * args : Any , ** kwargs : Any ) -> '_NoOp' :
82+ return self
83+
84+ def __enter__ (self ) -> '_NoOp' :
85+ return self
86+
87+ def __exit__ (self , * args : object , ** kwargs : Any ) -> None :
88+ pass
89+
90+ def __getattr__ (self , name : str ) -> '_NoOp' :
91+ return self
92+
93+ trace = _NoOp ()
94+ _SpanKind = _NoOp ()
95+ StatusCode = _NoOp ()
6896
6997SpanKind : TypeAlias = _SpanKind
7098__all__ = ['SpanKind' ]
99+
71100INSTRUMENTING_MODULE_NAME = 'a2a-python-sdk'
72101INSTRUMENTING_MODULE_VERSION = '1.0.0'
73102
74- logger = logging .getLogger (__name__ )
75-
76103
77104def trace_function ( # noqa: PLR0915
78105 func : Callable | None = None ,
@@ -280,22 +307,15 @@ def not_traced_method(self):
280307 exclude_list = exclude_list or []
281308
282309 def decorator (cls : Any ) -> Any :
283- all_methods = {}
284310 for name , method in inspect .getmembers (cls , inspect .isfunction ):
285- # Skip Dunders
286311 if name .startswith ('__' ) and name .endswith ('__' ):
287312 continue
288-
289- # Skip if include list is defined but the method not included.
290313 if include_list and name not in include_list :
291314 continue
292- # Skip if include list is not defined but the method is in excludes.
293315 if not include_list and name in exclude_list :
294316 continue
295317
296- all_methods [name ] = method
297318 span_name = f'{ cls .__module__ } .{ cls .__name__ } .{ name } '
298- # Set the decorator on the method.
299319 setattr (
300320 cls ,
301321 name ,
0 commit comments