@@ -102,13 +102,13 @@ def response_hook(span, service_name, operation_name, result):
102
102
suppress_http_instrumentation ,
103
103
unwrap ,
104
104
)
105
- from opentelemetry .propagators .aws .aws_xray_propagator import AwsXRayPropagator
105
+ from opentelemetry .propagate import inject
106
+ from opentelemetry .propagators import textmap
106
107
from opentelemetry .semconv .trace import SpanAttributes
107
108
from opentelemetry .trace import get_tracer
108
109
from opentelemetry .trace .span import Span
109
- import copy
110
110
import base64
111
- import traceback
111
+ import typing
112
112
logger = logging .getLogger (__name__ )
113
113
114
114
@@ -148,7 +148,7 @@ def _instrument(self, **kwargs):
148
148
self .request_hook = kwargs .get ("request_hook" )
149
149
self .response_hook = kwargs .get ("response_hook" )
150
150
try :
151
- self .payload_size_limit = int (os .environ .get ("OTEL_PAYLOAD_SIZE_LIMIT" , 204800 ))
151
+ self .payload_size_limit = int (os .environ .get ("OTEL_PAYLOAD_SIZE_LIMIT" , 51200 ))
152
152
except ValueError :
153
153
logger .error (
154
154
"OTEL_PAYLOAD_SIZE_LIMIT is not a number"
@@ -201,11 +201,6 @@ def _patched_api_call(self, original_func, instance, args, kwargs):
201
201
if call_context is None :
202
202
return original_func (* args , ** kwargs )
203
203
204
- #print("parsing context")
205
- #print(call_context.service)
206
- #print(call_context.operation)
207
- #print(args[1].get("ClientContext"))
208
-
209
204
extension = _find_extension (call_context )
210
205
if not extension .should_trace_service_call ():
211
206
return original_func (* args , ** kwargs )
@@ -224,21 +219,21 @@ def _patched_api_call(self, original_func, instance, args, kwargs):
224
219
elif call_context .operation == "PutObject" :
225
220
body = call_context .params .get ("Body" )
226
221
if body is not None :
227
- attributes ["rpc.request.payload" ] = body .decode ('ascii' )
222
+ attributes ["rpc.request.payload" ] = limit_string_size ( self . payload_size_limit , body .decode ('ascii' ) )
228
223
elif call_context .operation == "PutItem" :
229
224
body = call_context .params .get ("Item" )
230
225
if body is not None :
231
- attributes ["rpc.request.payload" ] = json .dumps (body , default = str )
226
+ attributes ["rpc.request.payload" ] = limit_string_size ( self . payload_size_limit , json .dumps (body , default = str ) )
232
227
elif call_context .operation == "GetItem" :
233
228
body = call_context .params .get ("Key" )
234
229
if body is not None :
235
- attributes ["rpc.request.payload" ] = json .dumps (body , default = str )
230
+ attributes ["rpc.request.payload" ] = limit_string_size ( self . payload_size_limit , json .dumps (body , default = str ) )
236
231
elif call_context .operation == "Publish" :
237
232
body = call_context .params .get ("Message" )
238
233
if body is not None :
239
- attributes ["rpc.request.payload" ] = json .dumps (body , default = str )
234
+ attributes ["rpc.request.payload" ] = limit_string_size ( self . payload_size_limit , json .dumps (body , default = str ) )
240
235
else :
241
- attributes ["rpc.request.payload" ] = json .dumps (call_context .params , default = str )
236
+ attributes ["rpc.request.payload" ] = limit_string_size ( self . payload_size_limit , json .dumps (call_context .params , default = str ) )
242
237
except Exception as ex :
243
238
pass
244
239
@@ -267,19 +262,34 @@ def _patched_api_call(self, original_func, instance, args, kwargs):
267
262
jctx = json .dumps (ctx )
268
263
args [1 ]['ClientContext' ] = base64 .b64encode (jctx .encode ('ascii' )).decode ('ascii' )
269
264
else :
270
- #ctx = {'custom': {'traceContext':{}}}
271
- #inject(ctx['custom']['traceContext'])
272
265
ctx = {'custom' : {}}
273
266
inject (ctx ['custom' ])
274
267
jctx = json .dumps (ctx )
275
268
args [1 ]['ClientContext' ] = base64 .b64encode (jctx .encode ('ascii' )).decode ('ascii' )
276
269
277
270
except Exception as ex :
278
- #print(traceback.format_exc())
279
- #print("exception")
280
- #print(ex)
281
271
pass
282
272
273
+ try :
274
+ if call_context .service == "sqs" and call_context .operation == "SendMessage" :
275
+ if args [1 ].get ("MessageAttributes" ) is not None :
276
+ inject (carrier = args [1 ].get ("MessageAttributes" ), setter = SQSSetter ())
277
+ else :
278
+ args [1 ]['MessageAttributes' ] = {}
279
+ inject (carrier = args [1 ].get ("MessageAttributes" ), setter = SQSSetter ())
280
+
281
+ if call_context .service == "sqs" and call_context .operation == "SendMessageBatch" :
282
+ if args [1 ].get ("Entries" ) is not None :
283
+ for entry in args [1 ].get ("Entries" ):
284
+ if entry .get ("MessageAttributes" ) is not None :
285
+ inject (carrier = entry .get ("MessageAttributes" ), setter = SQSSetter ())
286
+ else :
287
+ entry ['MessageAttributes' ] = {}
288
+ inject (carrier = entry .get ("MessageAttributes" ), setter = SQSSetter ())
289
+
290
+ except Exception as ex :
291
+ pass
292
+
283
293
result = None
284
294
try :
285
295
#print("calling original func")
@@ -404,9 +414,6 @@ def _apply_response_attributes(span: Span, result, payload_size_limit):
404
414
span .set_attribute (
405
415
"rpc.response.payload" , json .dumps (result , default = str ))
406
416
except Exception as ex :
407
- #print(traceback.format_exc())
408
- #print("exception")
409
- #print(ex)
410
417
pass
411
418
412
419
@@ -440,3 +447,27 @@ def _safe_invoke(function: Callable, *args):
440
447
logger .error (
441
448
"Error when invoking function '%s'" , function_name , exc_info = ex
442
449
)
450
+
451
+ class SQSSetter ():
452
+ def set (
453
+ self ,
454
+ carrier : typing .MutableMapping [str , textmap .CarrierValT ],
455
+ key : str ,
456
+ value : textmap .CarrierValT ,
457
+ ) -> None :
458
+ """Setter implementation to set a value into a dictionary.
459
+
460
+ Args:
461
+ carrier: dictionary in which to set value
462
+ key: the key used to set the value
463
+ value: the value to set
464
+ """
465
+ val = {"DataType" : "String" , "StringValue" : value }
466
+ carrier [key ] = val
467
+
468
+ def limit_string_size (s : str , max_size : int ) -> str :
469
+ if len (s ) > max_size :
470
+ return s [:max_size ]
471
+ else :
472
+ return s
473
+
0 commit comments