Skip to content

Commit e1fc2e1

Browse files
extract span preparation logic into helper function
1 parent b74633a commit e1fc2e1

File tree

1 file changed

+23
-13
lines changed
  • instrumentation/opentelemetry-instrumentation-botocore/src/opentelemetry/instrumentation/botocore

1 file changed

+23
-13
lines changed

instrumentation/opentelemetry-instrumentation-botocore/src/opentelemetry/instrumentation/botocore/__init__.py

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -262,26 +262,17 @@ def _patched_endpoint_prepare_request(
262262

263263
return wrapped(*args, **kwargs)
264264

265+
# pylint: disable=too-many-locals
265266
# pylint: disable=too-many-branches
266267
def _patched_api_call(self, original_func, instance, args, kwargs):
267268
if not is_instrumentation_enabled():
268269
return original_func(*args, **kwargs)
269270

270-
call_context = _determine_call_context(instance, args)
271-
if call_context is None:
271+
span_data = self._prepare_span_data(instance, args)
272+
if span_data is None:
272273
return original_func(*args, **kwargs)
273274

274-
extension = _find_extension(call_context)
275-
if not extension.should_trace_service_call():
276-
return original_func(*args, **kwargs)
277-
278-
attributes = {
279-
SpanAttributes.RPC_SYSTEM: "aws-api",
280-
SpanAttributes.RPC_SERVICE: call_context.service_id,
281-
SpanAttributes.RPC_METHOD: call_context.operation,
282-
CLOUD_REGION: call_context.region,
283-
**get_server_attributes(call_context.endpoint_url),
284-
}
275+
call_context, extension, attributes = span_data
285276

286277
_safe_invoke(extension.extract_attributes, attributes)
287278
end_span_on_exit = extension.should_end_span_on_exit()
@@ -346,6 +337,25 @@ def _call_response_hook(
346337
span, call_context.service, call_context.operation, result
347338
)
348339

340+
@staticmethod
341+
def _prepare_span_data(instance, args):
342+
call_context = _determine_call_context(instance, args)
343+
if call_context is None:
344+
return None
345+
346+
extension = _find_extension(call_context)
347+
if not extension.should_trace_service_call():
348+
return None
349+
350+
attributes = {
351+
SpanAttributes.RPC_SYSTEM: "aws-api",
352+
SpanAttributes.RPC_SERVICE: call_context.service_id,
353+
SpanAttributes.RPC_METHOD: call_context.operation,
354+
CLOUD_REGION: call_context.region,
355+
**get_server_attributes(call_context.endpoint_url),
356+
}
357+
358+
return call_context, extension, attributes
349359

350360
def _apply_response_attributes(span: Span, result):
351361
if result is None or not span.is_recording():

0 commit comments

Comments
 (0)