Skip to content

Commit b0ec0f9

Browse files
committed
add spans to class methods
1 parent 9eff274 commit b0ec0f9

File tree

1 file changed

+14
-2
lines changed
  • packages/service-library/src/servicelib/fastapi

1 file changed

+14
-2
lines changed

packages/service-library/src/servicelib/fastapi/tracing.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ def setup_httpx_client_tracing(client: AsyncClient | Client):
138138
HTTPXClientInstrumentor.instrument_client(client)
139139

140140

141-
def _create_opentelemetry_function_span(func: Callable):
141+
def _opentelemetry_function_span(func: Callable):
142142
"""Decorator that wraps a function call in an OpenTelemetry span."""
143143
tracer = trace.get_tracer(__name__)
144144

@@ -158,6 +158,15 @@ async def async_wrapper(*args, **kwargs):
158158
return wrapper
159159

160160

161+
def _opentelemetry_method_span(cls):
162+
for name, value in cls.__dict__.items():
163+
if callable(value) and not name.startswith("_"):
164+
setattr(
165+
cls, name, _opentelemetry_function_span(value)
166+
) # Apply the decorator
167+
return cls
168+
169+
161170
class _AddTracingSpansLoader(Loader):
162171
def __init__(self, loader: Loader):
163172
self.loader = loader
@@ -167,7 +176,10 @@ def exec_module(self, module: ModuleType):
167176
self.loader.exec_module(module)
168177
for name, func in inspect.getmembers(module, inspect.isfunction):
169178
if name in module.__dict__:
170-
setattr(module, name, _create_opentelemetry_function_span(func))
179+
setattr(module, name, _opentelemetry_function_span(func))
180+
for name, cls in inspect.getmembers(module, inspect.isclass):
181+
if name in module.__dict__ and cls.__module__ == module.__name__:
182+
setattr(module, name, _opentelemetry_method_span(cls))
171183

172184

173185
class _AddTracingSpansFinder(MetaPathFinder):

0 commit comments

Comments
 (0)