11# (c) Copyright IBM Corp. 2025
2+
3+
24try :
35 from typing import TYPE_CHECKING , Any , Callable , Dict , Sequence , Tuple , Type
46
1214 from botocore .client import BaseClient
1315
1416 from instana .span .span import InstanaSpan
17+ from instana .tracer import InstanaTracer
1518
1619 import json
1720
1821 import wrapt
1922
2023 from instana .log import logger
2124 from instana .propagators .format import Format
22- from instana .singletons import tracer
23- from instana .span .span import get_current_span
2425 from instana .util .traceutils import (
2526 extract_custom_headers ,
2627 get_tracer_tuple ,
27- tracing_is_off ,
2828 )
2929
30- def lambda_inject_context (payload : Dict [str , Any ], span : "InstanaSpan" ) -> None :
30+ def lambda_inject_context (
31+ tracer : "InstanaTracer" ,
32+ payload : Dict [str , Any ],
33+ span : "InstanaSpan" ,
34+ ) -> None :
3135 """
3236 When boto3 lambda client 'Invoke' is called, we want to inject the tracing context.
3337 boto3/botocore has specific requirements:
@@ -51,9 +55,9 @@ def emit_add_auth_with_instana(
5155 args : Tuple [object ],
5256 kwargs : Dict [str , Any ],
5357 ) -> Callable [..., None ]:
54- current_span = get_current_span ()
55- if not tracing_is_off () and current_span and current_span . is_recording () :
56- extract_custom_headers (current_span , args [0 ].headers )
58+ _ , parent_span , _ = get_tracer_tuple ()
59+ if parent_span :
60+ extract_custom_headers (parent_span , args [0 ].headers )
5761 return wrapped (* args , ** kwargs )
5862
5963 @wrapt .patch_function_wrapper ("botocore.client" , "BaseClient._make_api_call" )
@@ -63,18 +67,19 @@ def make_api_call_with_instana(
6367 args : Sequence [Dict [str , Any ]],
6468 kwargs : Dict [str , Any ],
6569 ) -> Dict [str , Any ]:
70+ tracer , parent_span , _ = get_tracer_tuple ()
6671 # If we're not tracing, just return
67- if tracing_is_off () :
72+ if not tracer :
6873 return wrapped (* args , ** kwargs )
6974
70- tracer , parent_span , _ = get_tracer_tuple ()
71-
7275 parent_context = parent_span .get_span_context () if parent_span else None
7376
7477 if instance .meta .service_model .service_name == "dynamodb" :
75- create_dynamodb_span (wrapped , instance , args , kwargs , parent_context )
78+ create_dynamodb_span (
79+ wrapped , instance , args , kwargs , parent_context , tracer
80+ )
7681 elif instance .meta .service_model .service_name == "s3" :
77- create_s3_span (wrapped , instance , args , kwargs , parent_context )
82+ create_s3_span (wrapped , instance , args , kwargs , parent_context , tracer )
7883 else :
7984 with tracer .start_as_current_span (
8085 "boto3" , span_context = parent_context
@@ -98,7 +103,7 @@ def make_api_call_with_instana(
98103
99104 # Inject context when invoking lambdas
100105 if "lambda" in instance ._endpoint .host and operation == "Invoke" :
101- lambda_inject_context (payload , span )
106+ lambda_inject_context (tracer , payload , span )
102107
103108 try :
104109 result = wrapped (* args , ** kwargs )
0 commit comments