Skip to content

Commit 8128e62

Browse files
committed
fix: JSCPD errors
- Refactor to remove copy/paste
1 parent 1936938 commit 8128e62

File tree

1 file changed

+19
-35
lines changed

1 file changed

+19
-35
lines changed

src/a2a/utils/telemetry.py

Lines changed: 19 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,7 @@ def trace_function(
136136
f'Start tracing for {actual_span_name}, is_async_func {is_async_func}'
137137
)
138138

139-
@functools.wraps(func)
140-
async def async_wrapper(*args, **kwargs) -> any:
141-
"""Async Wrapper for the decorator."""
142-
logger.debug('Start async tracer')
139+
async def _invoke_with_tracing(*args, **kwargs):
143140
tracer = trace.get_tracer(
144141
INSTRUMENTING_MODULE_NAME, INSTRUMENTING_MODULE_VERSION
145142
)
@@ -153,7 +150,11 @@ async def async_wrapper(*args, **kwargs) -> any:
153150

154151
try:
155152
# Async wrapper, await for the function call to complete.
156-
result = await func(*args, **kwargs)
153+
if is_async_func:
154+
result = await func(*args, **kwargs)
155+
# Sync wrapper, execute the function call.
156+
else:
157+
result = func(*args, **kwargs)
157158
span.set_status(StatusCode.OK)
158159
return result
159160

@@ -173,39 +174,22 @@ async def async_wrapper(*args, **kwargs) -> any:
173174
f'attribute_extractor error in span {actual_span_name}: {attr_e}'
174175
)
175176

177+
@functools.wraps(func)
178+
async def async_wrapper(*args, **kwargs):
179+
"""Async Wrapper for the decorator."""
180+
logger.debug('Start async tracer')
181+
return await _invoke_with_tracing(
182+
*args,
183+
**kwargs,
184+
)
185+
176186
@functools.wraps(func)
177187
def sync_wrapper(*args, **kwargs):
178188
"""Sync Wrapper for the decorator."""
179-
tracer = trace.get_tracer(INSTRUMENTING_MODULE_NAME)
180-
with tracer.start_as_current_span(actual_span_name, kind=kind) as span:
181-
if attributes:
182-
for k, v in attributes.items():
183-
span.set_attribute(k, v)
184-
185-
result = None
186-
exception = None
187-
188-
try:
189-
# Sync wrapper, execute the function call.
190-
result = func(*args, **kwargs)
191-
span.set_status(StatusCode.OK)
192-
return result
193-
194-
except Exception as e:
195-
exception = e
196-
span.record_exception(e)
197-
span.set_status(StatusCode.ERROR, description=str(e))
198-
raise
199-
finally:
200-
if attribute_extractor:
201-
try:
202-
attribute_extractor(
203-
span, args, kwargs, result, exception
204-
)
205-
except Exception as attr_e:
206-
logger.error(
207-
f'attribute_extractor error in span {actual_span_name}: {attr_e}'
208-
)
189+
return _invoke_with_tracing(
190+
*args,
191+
**kwargs,
192+
)
209193

210194
return async_wrapper if is_async_func else sync_wrapper
211195

0 commit comments

Comments
 (0)