Skip to content

Commit 582f023

Browse files
committed
improv: naming consistency
1 parent 3ff8a01 commit 582f023

File tree

1 file changed

+43
-39
lines changed

1 file changed

+43
-39
lines changed

aws_lambda_powertools/tracing/tracer.py

+43-39
Original file line numberDiff line numberDiff line change
@@ -158,19 +158,19 @@ def __init__(
158158
def put_annotation(self, key: str, value: Any):
159159
"""Adds annotation to existing segment or subsegment
160160
161+
Parameters
162+
----------
163+
key : str
164+
Annotation key
165+
value : any
166+
Value for annotation
167+
161168
Example
162169
-------
163170
Custom annotation for a pseudo service named payment
164171
165172
tracer = Tracer(service="payment")
166173
tracer.put_annotation("PaymentStatus", "CONFIRMED")
167-
168-
Parameters
169-
----------
170-
key : str
171-
Annotation key (e.g. PaymentStatus)
172-
value : any
173-
Value for annotation (e.g. "CONFIRMED")
174174
"""
175175
if self.disabled:
176176
logger.debug("Tracing has been disabled, aborting put_annotation")
@@ -283,15 +283,15 @@ def decorate(event, context):
283283
logger.debug("Received lambda handler response successfully")
284284
logger.debug(response)
285285
self._add_response_as_metadata(
286-
function_name=lambda_handler_name,
286+
method_name=lambda_handler_name,
287287
data=response,
288288
subsegment=subsegment,
289289
capture_response=capture_response,
290290
)
291291
except Exception as err:
292292
logger.exception(f"Exception received from {lambda_handler_name}")
293293
self._add_full_exception_as_metadata(
294-
function_name=lambda_handler_name, error=err, subsegment=subsegment
294+
method_name=lambda_handler_name, error=err, subsegment=subsegment
295295
)
296296
raise
297297

@@ -452,67 +452,73 @@ async def async_tasks():
452452
logger.debug("Decorator called with parameters")
453453
return functools.partial(self.capture_method, capture_response=capture_response)
454454

455+
method_name = f"{method.__name__}"
456+
455457
if inspect.iscoroutinefunction(method):
456-
decorate = self._decorate_async_function(method=method, capture_response=capture_response)
458+
decorate = self._decorate_async_function(
459+
method=method, capture_response=capture_response, method_name=method_name
460+
)
457461
elif inspect.isgeneratorfunction(method):
458-
decorate = self._decorate_generator_function(method=method, capture_response=capture_response)
462+
decorate = self._decorate_generator_function(
463+
method=method, capture_response=capture_response, method_name=method_name
464+
)
459465
elif hasattr(method, "__wrapped__") and inspect.isgeneratorfunction(method.__wrapped__):
460466
decorate = self._decorate_generator_function_with_context_manager(
461-
method=method, capture_response=capture_response
467+
method=method, capture_response=capture_response, method_name=method_name
462468
)
463469
else:
464-
decorate = self._decorate_sync_function(method=method, capture_response=capture_response)
470+
decorate = self._decorate_sync_function(
471+
method=method, capture_response=capture_response, method_name=method_name
472+
)
465473

466474
return decorate
467475

468-
def _decorate_async_function(self, method: Callable = None, capture_response: bool = True):
469-
method_name = f"{method.__name__}"
470-
476+
def _decorate_async_function(self, method: Callable = None, capture_response: bool = True, method_name: str = None):
471477
@functools.wraps(method)
472478
async def decorate(*args, **kwargs):
473479
async with self.provider.in_subsegment_async(name=f"## {method_name}") as subsegment:
474480
try:
475481
logger.debug(f"Calling method: {method_name}")
476482
response = await method(*args, **kwargs)
477483
self._add_response_as_metadata(
478-
function_name=method_name,
484+
method_name=method_name,
479485
data=response,
480486
subsegment=subsegment,
481487
capture_response=capture_response,
482488
)
483489
except Exception as err:
484490
logger.exception(f"Exception received from '{method_name}' method")
485-
self._add_full_exception_as_metadata(function_name=method_name, error=err, subsegment=subsegment)
491+
self._add_full_exception_as_metadata(method_name=method_name, error=err, subsegment=subsegment)
486492
raise
487493

488494
return response
489495

490496
return decorate
491497

492-
def _decorate_generator_function(self, method: Callable = None, capture_response: bool = True):
493-
method_name = f"{method.__name__}"
494-
498+
def _decorate_generator_function(
499+
self, method: Callable = None, capture_response: bool = True, method_name: str = None
500+
):
495501
@functools.wraps(method)
496502
def decorate(*args, **kwargs):
497503
with self.provider.in_subsegment(name=f"## {method_name}") as subsegment:
498504
try:
499505
logger.debug(f"Calling method: {method_name}")
500506
result = yield from method(*args, **kwargs)
501507
self._add_response_as_metadata(
502-
function_name=method_name, data=result, subsegment=subsegment, capture_response=capture_response
508+
method_name=method_name, data=result, subsegment=subsegment, capture_response=capture_response
503509
)
504510
except Exception as err:
505511
logger.exception(f"Exception received from '{method_name}' method")
506-
self._add_full_exception_as_metadata(function_name=method_name, error=err, subsegment=subsegment)
512+
self._add_full_exception_as_metadata(method_name=method_name, error=err, subsegment=subsegment)
507513
raise
508514

509515
return result
510516

511517
return decorate
512518

513-
def _decorate_generator_function_with_context_manager(self, method: Callable = None, capture_response: bool = True):
514-
method_name = f"{method.__name__}"
515-
519+
def _decorate_generator_function_with_context_manager(
520+
self, method: Callable = None, capture_response: bool = True, method_name: str = None
521+
):
516522
@functools.wraps(method)
517523
@contextlib.contextmanager
518524
def decorate(*args, **kwargs):
@@ -523,33 +529,31 @@ def decorate(*args, **kwargs):
523529
result = return_val
524530
yield result
525531
self._add_response_as_metadata(
526-
function_name=method_name, data=result, subsegment=subsegment, capture_response=capture_response
532+
method_name=method_name, data=result, subsegment=subsegment, capture_response=capture_response
527533
)
528534
except Exception as err:
529535
logger.exception(f"Exception received from '{method_name}' method")
530-
self._add_full_exception_as_metadata(function_name=method_name, error=err, subsegment=subsegment)
536+
self._add_full_exception_as_metadata(method_name=method_name, error=err, subsegment=subsegment)
531537
raise
532538

533539
return decorate
534540

535-
def _decorate_sync_function(self, method: Callable = None, capture_response: bool = True):
536-
method_name = f"{method.__name__}"
537-
541+
def _decorate_sync_function(self, method: Callable = None, capture_response: bool = True, method_name: str = None):
538542
@functools.wraps(method)
539543
def decorate(*args, **kwargs):
540544
with self.provider.in_subsegment(name=f"## {method_name}") as subsegment:
541545
try:
542546
logger.debug(f"Calling method: {method_name}")
543547
response = method(*args, **kwargs)
544548
self._add_response_as_metadata(
545-
function_name=method_name,
549+
method_name=method_name,
546550
data=response,
547551
subsegment=subsegment,
548552
capture_response=capture_response,
549553
)
550554
except Exception as err:
551555
logger.exception(f"Exception received from '{method_name}' method")
552-
self._add_full_exception_as_metadata(function_name=method_name, error=err, subsegment=subsegment)
556+
self._add_full_exception_as_metadata(method_name=method_name, error=err, subsegment=subsegment)
553557
raise
554558

555559
return response
@@ -558,7 +562,7 @@ def decorate(*args, **kwargs):
558562

559563
def _add_response_as_metadata(
560564
self,
561-
function_name: str = None,
565+
method_name: str = None,
562566
data: Any = None,
563567
subsegment: aws_xray_sdk.core.models.subsegment = None,
564568
capture_response: bool = True,
@@ -567,7 +571,7 @@ def _add_response_as_metadata(
567571
568572
Parameters
569573
----------
570-
function_name : str, optional
574+
method_name : str, optional
571575
function name to add as metadata key, by default None
572576
data : Any, optional
573577
data to add as subsegment metadata, by default None
@@ -579,23 +583,23 @@ def _add_response_as_metadata(
579583
if data is None or not capture_response or subsegment is None:
580584
return
581585

582-
subsegment.put_metadata(key=f"{function_name} response", value=data, namespace=self._config["service"])
586+
subsegment.put_metadata(key=f"{method_name} response", value=data, namespace=self._config["service"])
583587

584588
def _add_full_exception_as_metadata(
585-
self, function_name: str = None, error: Exception = None, subsegment: aws_xray_sdk.core.models.subsegment = None
589+
self, method_name: str = None, error: Exception = None, subsegment: aws_xray_sdk.core.models.subsegment = None
586590
):
587591
"""Add full exception object as metadata for given subsegment
588592
589593
Parameters
590594
----------
591-
function_name : str, optional
595+
method_name : str, optional
592596
function name to add as metadata key, by default None
593597
error : Exception, optional
594598
error to add as subsegment metadata, by default None
595599
subsegment : aws_xray_sdk.core.models.subsegment, optional
596600
existing subsegment to add metadata on, by default None
597601
"""
598-
subsegment.put_metadata(key=f"{function_name} error", value=error, namespace=self._config["service"])
602+
subsegment.put_metadata(key=f"{method_name} error", value=error, namespace=self._config["service"])
599603

600604
def __disable_tracing_provider(self):
601605
"""Forcefully disables tracing"""

0 commit comments

Comments
 (0)