74
74
75
75
DEFAULT_RETRY = False
76
76
DEFAULT_URL = "http://localhost:9411/api/v2/spans"
77
+ DEFAULT_MAX_TAG_VALUE_LENGTH = 128
77
78
ZIPKIN_HEADERS = {"Content-Type" : "application/json" }
78
79
79
80
SPAN_KIND_MAP = {
@@ -108,6 +109,7 @@ def __init__(
108
109
ipv4 : Optional [str ] = None ,
109
110
ipv6 : Optional [str ] = None ,
110
111
retry : Optional [str ] = DEFAULT_RETRY ,
112
+ max_tag_value_length = DEFAULT_MAX_TAG_VALUE_LENGTH ,
111
113
):
112
114
self .service_name = service_name
113
115
if url is None :
@@ -122,6 +124,7 @@ def __init__(
122
124
self .ipv4 = ipv4
123
125
self .ipv6 = ipv6
124
126
self .retry = retry
127
+ self .max_tag_value_length = max_tag_value_length
125
128
126
129
def export (self , spans : Sequence [Span ]) -> SpanExportResult :
127
130
zipkin_spans = self ._translate_to_zipkin (spans )
@@ -141,6 +144,9 @@ def export(self, spans: Sequence[Span]) -> SpanExportResult:
141
144
return SpanExportResult .FAILURE
142
145
return SpanExportResult .SUCCESS
143
146
147
+ def shutdown (self ) -> None :
148
+ pass
149
+
144
150
def _translate_to_zipkin (self , spans : Sequence [Span ]):
145
151
146
152
local_endpoint = {"serviceName" : self .service_name , "port" : self .port }
@@ -171,8 +177,10 @@ def _translate_to_zipkin(self, spans: Sequence[Span]):
171
177
"duration" : duration_mus ,
172
178
"localEndpoint" : local_endpoint ,
173
179
"kind" : SPAN_KIND_MAP [span .kind ],
174
- "tags" : _extract_tags_from_span (span ),
175
- "annotations" : _extract_annotations_from_events (span .events ),
180
+ "tags" : self ._extract_tags_from_span (span ),
181
+ "annotations" : self ._extract_annotations_from_events (
182
+ span .events
183
+ ),
176
184
}
177
185
178
186
if span .instrumentation_info is not None :
@@ -205,42 +213,44 @@ def _translate_to_zipkin(self, spans: Sequence[Span]):
205
213
zipkin_spans .append (zipkin_span )
206
214
return zipkin_spans
207
215
208
- def shutdown (self ) -> None :
209
- pass
210
-
216
+ def _extract_tags_from_dict (self , tags_dict ):
217
+ tags = {}
218
+ if not tags_dict :
219
+ return tags
220
+ for attribute_key , attribute_value in tags_dict .items ():
221
+ if isinstance (attribute_value , (int , bool , float )):
222
+ value = str (attribute_value )
223
+ elif isinstance (attribute_value , str ):
224
+ value = attribute_value
225
+ else :
226
+ logger .warning ("Could not serialize tag %s" , attribute_key )
227
+ continue
228
+
229
+ if self .max_tag_value_length > 0 :
230
+ value = value [: self .max_tag_value_length ]
231
+ tags [attribute_key ] = value
232
+ return tags
211
233
212
- def _extract_tags_from_dict (tags_dict ):
213
- tags = {}
214
- if not tags_dict :
234
+ def _extract_tags_from_span (self , span : Span ):
235
+ tags = self ._extract_tags_from_dict (getattr (span , "attributes" , None ))
236
+ if span .resource :
237
+ tags .update (self ._extract_tags_from_dict (span .resource .attributes ))
215
238
return tags
216
- for attribute_key , attribute_value in tags_dict .items ():
217
- if isinstance (attribute_value , (int , bool , float )):
218
- value = str (attribute_value )
219
- elif isinstance (attribute_value , str ):
220
- value = attribute_value [:128 ]
221
- else :
222
- logger .warning ("Could not serialize tag %s" , attribute_key )
223
- continue
224
- tags [attribute_key ] = value
225
- return tags
226
-
227
-
228
- def _extract_tags_from_span (span : Span ):
229
- tags = _extract_tags_from_dict (getattr (span , "attributes" , None ))
230
- if span .resource :
231
- tags .update (_extract_tags_from_dict (span .resource .attributes ))
232
- return tags
233
-
234
-
235
- def _extract_annotations_from_events (events ):
236
- return (
237
- [
238
- {"timestamp" : _nsec_to_usec_round (e .timestamp ), "value" : e .name }
239
- for e in events
240
- ]
241
- if events
242
- else None
243
- )
239
+
240
+ def _extract_annotations_from_events (
241
+ self , events
242
+ ): # pylint: disable=R0201
243
+ return (
244
+ [
245
+ {
246
+ "timestamp" : _nsec_to_usec_round (e .timestamp ),
247
+ "value" : e .name ,
248
+ }
249
+ for e in events
250
+ ]
251
+ if events
252
+ else None
253
+ )
244
254
245
255
246
256
def _nsec_to_usec_round (nsec ):
0 commit comments