@@ -82,6 +82,7 @@ def response_hook(span, service_name, operation_name, result):
8282import json
8383import io
8484import os
85+ from opentelemetry .instrumentation .botocore .utils import limit_string_size , get_payload_size_limit
8586from typing import Any , Callable , Collection , Dict , Optional , Tuple
8687
8788from botocore .client import BaseClient
@@ -134,7 +135,6 @@ def __init__(self):
134135 super ().__init__ ()
135136 self .request_hook = None
136137 self .response_hook = None
137- self .payload_size_limit = 51200
138138
139139 def instrumentation_dependencies (self ) -> Collection [str ]:
140140 return _instruments
@@ -147,12 +147,6 @@ def _instrument(self, **kwargs):
147147
148148 self .request_hook = kwargs .get ("request_hook" )
149149 self .response_hook = kwargs .get ("response_hook" )
150- try :
151- self .payload_size_limit = int (os .environ .get ("OTEL_PAYLOAD_SIZE_LIMIT" , 51200 ))
152- except ValueError :
153- logger .error (
154- "OTEL_PAYLOAD_SIZE_LIMIT is not a number"
155- )
156150 wrap_function_wrapper (
157151 "botocore.client" ,
158152 "BaseClient._make_api_call" ,
@@ -201,28 +195,28 @@ def _patched_api_call(self, original_func, instance, args, kwargs):
201195 if call_context .operation == "ListObjects" :
202196 bucket = call_context .params .get ("Bucket" )
203197 if bucket is not None :
204- attributes ["rpc.request.payload" ] = bucket
198+ attributes ["rpc.request.payload" ] = limit_string_size ( bucket )
205199 elif call_context .operation == "PutObject" :
206200 body = call_context .params .get ("Body" )
207201 if body is not None :
208- attributes ["rpc.request.payload" ] = limit_string_size (self . payload_size_limit , body .decode ('ascii' ))
202+ attributes ["rpc.request.payload" ] = limit_string_size (body .decode ('ascii' ))
209203 elif call_context .operation == "PutItem" :
210204 body = call_context .params .get ("Item" )
211205 if body is not None :
212- attributes ["rpc.request.payload" ] = limit_string_size (self . payload_size_limit , json .dumps (body , default = str ))
206+ attributes ["rpc.request.payload" ] = limit_string_size (json .dumps (body , default = str ))
213207 elif call_context .operation == "GetItem" :
214208 body = call_context .params .get ("Key" )
215209 if body is not None :
216- attributes ["rpc.request.payload" ] = limit_string_size (self . payload_size_limit , json .dumps (body , default = str ))
210+ attributes ["rpc.request.payload" ] = limit_string_size (json .dumps (body , default = str ))
217211 elif call_context .operation == "Publish" :
218212 body = call_context .params .get ("Message" )
219213 if body is not None :
220- attributes ["rpc.request.payload" ] = limit_string_size (self . payload_size_limit , json .dumps (body , default = str ))
214+ attributes ["rpc.request.payload" ] = limit_string_size (json .dumps (body , default = str ))
221215 elif call_context .service == "events" and call_context .operation == "PutEvents" :
222216 call_context .span_kind = SpanKind .PRODUCER
223- attributes ["rpc.request.payload" ] = limit_string_size (self . payload_size_limit , json .dumps (call_context .params , default = str ))
217+ attributes ["rpc.request.payload" ] = limit_string_size (json .dumps (call_context .params , default = str ))
224218 else :
225- attributes ["rpc.request.payload" ] = limit_string_size (self . payload_size_limit , json .dumps (call_context .params , default = str ))
219+ attributes ["rpc.request.payload" ] = limit_string_size (json .dumps (call_context .params , default = str ))
226220 except Exception as ex :
227221 pass
228222
@@ -303,11 +297,11 @@ def _patched_api_call(self, original_func, instance, args, kwargs):
303297 result = original_func (* args , ** kwargs )
304298 except ClientError as error :
305299 result = getattr (error , "response" , None )
306- _apply_response_attributes (span , result , self . payload_size_limit )
300+ _apply_response_attributes (span , result )
307301 _safe_invoke (extension .on_error , span , error )
308302 raise
309303 else :
310- _apply_response_attributes (span , result , self . payload_size_limit )
304+ _apply_response_attributes (span , result )
311305 _safe_invoke (extension .on_success , span , result )
312306 finally :
313307 context_api .detach (token )
@@ -337,7 +331,7 @@ def _call_response_hook(
337331 )
338332
339333
340- def _apply_response_attributes (span : Span , result , payload_size_limit ):
334+ def _apply_response_attributes (span : Span , result ):
341335 if result is None or not span .is_recording ():
342336 return
343337
@@ -380,15 +374,15 @@ def _apply_response_attributes(span: Span, result, payload_size_limit):
380374 body = result .get ("Body" )
381375 if buckets is not None :
382376 span .set_attribute (
383- "rpc.response.payload" , json .dumps ([b .get ("Name" ) for b in buckets ]))
377+ "rpc.response.payload" , limit_string_size ( json .dumps ([b .get ("Name" ) for b in buckets ]) ))
384378 elif content is not None :
385379 span .set_attribute (
386- "rpc.response.payload" , json .dumps ([b .get ("Key" ) for b in content ]))
380+ "rpc.response.payload" , limit_string_size ( json .dumps ([b .get ("Key" ) for b in content ]) ))
387381 elif body is not None :
388382 pass
389383 else :
390384 span .set_attribute (
391- "rpc.response.payload" , json .dumps (result , default = str ))
385+ "rpc.response.payload" , limit_string_size ( json .dumps (result , default = str ) ))
392386 #elif body is not None:
393387 # try:
394388 # d = {x: result[x] for x in result if x != "Body"}
@@ -406,20 +400,20 @@ def _apply_response_attributes(span: Span, result, payload_size_limit):
406400 # except Exception as ex:
407401 # pass
408402 # Lambda Invoke
409- elif result .get ("Payload" ) is not None and result .get ("Payload" )._content_length is not None and int (result .get ("Payload" )._content_length ) < payload_size_limit :
403+ elif result .get ("Payload" ) is not None and result .get ("Payload" )._content_length is not None and int (result .get ("Payload" )._content_length ) < get_payload_size_limit () :
410404 length = result .get ("Payload" )._content_length
411405 strbody = result .get ("Payload" ).read ()
412406 result .get ("Payload" ).close ()
413407 span .set_attribute (
414- "rpc.response.payload" , strbody )
408+ "rpc.response.payload" , limit_string_size ( strbody ) )
415409 result ['Payload' ] = StreamingBody (io .BytesIO (strbody ), content_length = length )
416410 # DynamoDB get item
417411 elif server == "Server" :
418412 span .set_attribute (
419- "rpc.response.payload" , json .dumps (result , default = str ))
413+ "rpc.response.payload" , limit_string_size ( json .dumps (result , default = str ) ))
420414 else :
421415 span .set_attribute (
422- "rpc.response.payload" , json .dumps (result , default = str ))
416+ "rpc.response.payload" , limit_string_size ( json .dumps (result , default = str ) ))
423417 except Exception as ex :
424418 pass
425419
@@ -471,10 +465,3 @@ def set(
471465 """
472466 val = {"DataType" : "String" , "StringValue" : value }
473467 carrier [key ] = val
474-
475- def limit_string_size (max_size : int , s : str ) -> str :
476- if len (s ) > max_size :
477- return s [:max_size ]
478- else :
479- return s
480-
0 commit comments