@@ -158,19 +158,19 @@ def __init__(
158
158
def put_annotation (self , key : str , value : Any ):
159
159
"""Adds annotation to existing segment or subsegment
160
160
161
+ Parameters
162
+ ----------
163
+ key : str
164
+ Annotation key
165
+ value : any
166
+ Value for annotation
167
+
161
168
Example
162
169
-------
163
170
Custom annotation for a pseudo service named payment
164
171
165
172
tracer = Tracer(service="payment")
166
173
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")
174
174
"""
175
175
if self .disabled :
176
176
logger .debug ("Tracing has been disabled, aborting put_annotation" )
@@ -283,15 +283,15 @@ def decorate(event, context):
283
283
logger .debug ("Received lambda handler response successfully" )
284
284
logger .debug (response )
285
285
self ._add_response_as_metadata (
286
- function_name = lambda_handler_name ,
286
+ method_name = lambda_handler_name ,
287
287
data = response ,
288
288
subsegment = subsegment ,
289
289
capture_response = capture_response ,
290
290
)
291
291
except Exception as err :
292
292
logger .exception (f"Exception received from { lambda_handler_name } " )
293
293
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
295
295
)
296
296
raise
297
297
@@ -452,67 +452,73 @@ async def async_tasks():
452
452
logger .debug ("Decorator called with parameters" )
453
453
return functools .partial (self .capture_method , capture_response = capture_response )
454
454
455
+ method_name = f"{ method .__name__ } "
456
+
455
457
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
+ )
457
461
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
+ )
459
465
elif hasattr (method , "__wrapped__" ) and inspect .isgeneratorfunction (method .__wrapped__ ):
460
466
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
462
468
)
463
469
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
+ )
465
473
466
474
return decorate
467
475
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 ):
471
477
@functools .wraps (method )
472
478
async def decorate (* args , ** kwargs ):
473
479
async with self .provider .in_subsegment_async (name = f"## { method_name } " ) as subsegment :
474
480
try :
475
481
logger .debug (f"Calling method: { method_name } " )
476
482
response = await method (* args , ** kwargs )
477
483
self ._add_response_as_metadata (
478
- function_name = method_name ,
484
+ method_name = method_name ,
479
485
data = response ,
480
486
subsegment = subsegment ,
481
487
capture_response = capture_response ,
482
488
)
483
489
except Exception as err :
484
490
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 )
486
492
raise
487
493
488
494
return response
489
495
490
496
return decorate
491
497
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
+ ):
495
501
@functools .wraps (method )
496
502
def decorate (* args , ** kwargs ):
497
503
with self .provider .in_subsegment (name = f"## { method_name } " ) as subsegment :
498
504
try :
499
505
logger .debug (f"Calling method: { method_name } " )
500
506
result = yield from method (* args , ** kwargs )
501
507
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
503
509
)
504
510
except Exception as err :
505
511
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 )
507
513
raise
508
514
509
515
return result
510
516
511
517
return decorate
512
518
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
+ ):
516
522
@functools .wraps (method )
517
523
@contextlib .contextmanager
518
524
def decorate (* args , ** kwargs ):
@@ -523,33 +529,31 @@ def decorate(*args, **kwargs):
523
529
result = return_val
524
530
yield result
525
531
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
527
533
)
528
534
except Exception as err :
529
535
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 )
531
537
raise
532
538
533
539
return decorate
534
540
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 ):
538
542
@functools .wraps (method )
539
543
def decorate (* args , ** kwargs ):
540
544
with self .provider .in_subsegment (name = f"## { method_name } " ) as subsegment :
541
545
try :
542
546
logger .debug (f"Calling method: { method_name } " )
543
547
response = method (* args , ** kwargs )
544
548
self ._add_response_as_metadata (
545
- function_name = method_name ,
549
+ method_name = method_name ,
546
550
data = response ,
547
551
subsegment = subsegment ,
548
552
capture_response = capture_response ,
549
553
)
550
554
except Exception as err :
551
555
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 )
553
557
raise
554
558
555
559
return response
@@ -558,7 +562,7 @@ def decorate(*args, **kwargs):
558
562
559
563
def _add_response_as_metadata (
560
564
self ,
561
- function_name : str = None ,
565
+ method_name : str = None ,
562
566
data : Any = None ,
563
567
subsegment : aws_xray_sdk .core .models .subsegment = None ,
564
568
capture_response : bool = True ,
@@ -567,7 +571,7 @@ def _add_response_as_metadata(
567
571
568
572
Parameters
569
573
----------
570
- function_name : str, optional
574
+ method_name : str, optional
571
575
function name to add as metadata key, by default None
572
576
data : Any, optional
573
577
data to add as subsegment metadata, by default None
@@ -579,23 +583,23 @@ def _add_response_as_metadata(
579
583
if data is None or not capture_response or subsegment is None :
580
584
return
581
585
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" ])
583
587
584
588
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
586
590
):
587
591
"""Add full exception object as metadata for given subsegment
588
592
589
593
Parameters
590
594
----------
591
- function_name : str, optional
595
+ method_name : str, optional
592
596
function name to add as metadata key, by default None
593
597
error : Exception, optional
594
598
error to add as subsegment metadata, by default None
595
599
subsegment : aws_xray_sdk.core.models.subsegment, optional
596
600
existing subsegment to add metadata on, by default None
597
601
"""
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" ])
599
603
600
604
def __disable_tracing_provider (self ):
601
605
"""Forcefully disables tracing"""
0 commit comments