@@ -109,7 +109,7 @@ def translate_to_cloud_trace(
109
109
{
110
110
"name" : span_name ,
111
111
"span_id" : span_id ,
112
- "display_name" : get_truncatable_str (span .name ),
112
+ "display_name" : get_truncatable_str_object (span .name ),
113
113
"start_time" : start_time ,
114
114
"end_time" : end_time ,
115
115
"parent_span_id" : parent_id ,
@@ -141,13 +141,11 @@ def get_time_from_ns(nanoseconds):
141
141
return {"seconds" : int (nanoseconds / 1e9 ), "nanos" : int (nanoseconds % 1e9 )}
142
142
143
143
144
- def get_truncatable_str (str_to_convert , max_length = MAX_LENGTH ):
144
+ def get_truncatable_str_object (str_to_convert , max_length = MAX_LENGTH ):
145
145
"""Truncate the string if it exceeds the length limit and record the truncated bytes
146
146
count.
147
147
"""
148
- truncated , truncated_byte_count = check_str_length (
149
- str_to_convert , max_length
150
- )
148
+ truncated , truncated_byte_count = truncate_str (str_to_convert , max_length )
151
149
152
150
result = {
153
151
"value" : truncated ,
@@ -156,7 +154,7 @@ def get_truncatable_str(str_to_convert, max_length=MAX_LENGTH):
156
154
return result
157
155
158
156
159
- def check_str_length (str_to_check , limit = MAX_LENGTH ):
157
+ def truncate_str (str_to_check , limit = MAX_LENGTH ):
160
158
"""Check the length of a string. If exceeds limit, then truncate it.
161
159
"""
162
160
str_bytes = str_to_check .encode ("utf-8" )
@@ -213,7 +211,7 @@ def _extract_events(events: Sequence[Event]):
213
211
{
214
212
"time" : get_time_from_ns (event .timestamp ),
215
213
"annotation" : {
216
- "description" : get_truncatable_str (event .name , 256 ),
214
+ "description" : get_truncatable_str_object (event .name , 256 ),
217
215
"attributes" : _extract_attributes (event .attributes ),
218
216
},
219
217
}
@@ -223,15 +221,15 @@ def _extract_events(events: Sequence[Event]):
223
221
224
222
def _extract_attributes (attrs : types .Attributes ):
225
223
"""Convert span.attributes to dict."""
226
- attributes_json = {}
224
+ attributes_dict = {}
227
225
228
226
for key , value in attrs .items ():
229
- key = check_str_length (key )[0 ]
227
+ key = truncate_str (key )[0 ]
230
228
value = _format_attribute_value (value )
231
229
232
230
if value is not None :
233
- attributes_json [key ] = value
234
- return {"attribute_map" : attributes_json }
231
+ attributes_dict [key ] = value
232
+ return {"attribute_map" : attributes_dict }
235
233
236
234
237
235
def _format_attribute_value (value : types .AttributeValue ):
@@ -241,10 +239,10 @@ def _format_attribute_value(value: types.AttributeValue):
241
239
value_type = "int_value"
242
240
elif isinstance (value , str ):
243
241
value_type = "string_value"
244
- value = get_truncatable_str (value )
242
+ value = get_truncatable_str_object (value )
245
243
elif isinstance (value , float ):
246
244
value_type = "string_value"
247
- value = get_truncatable_str (str (value ))
245
+ value = get_truncatable_str_object (str (value ))
248
246
else :
249
247
logger .warning (
250
248
"ignoring attribute value %s of type %s. Values type must be one of bool, int, string or float" ,
0 commit comments