7474
7575DEFAULT_RETRY = False
7676DEFAULT_URL = "http://localhost:9411/api/v2/spans"
77+ DEFAULT_MAX_TAG_VALUE_LENGTH = 128
7778ZIPKIN_HEADERS = {"Content-Type" : "application/json" }
7879
7980SPAN_KIND_MAP = {
@@ -108,6 +109,7 @@ def __init__(
108109 ipv4 : Optional [str ] = None ,
109110 ipv6 : Optional [str ] = None ,
110111 retry : Optional [str ] = DEFAULT_RETRY ,
112+ max_tag_value_length : Optional [int ] = DEFAULT_MAX_TAG_VALUE_LENGTH ,
111113 ):
112114 self .service_name = service_name
113115 if url is None :
@@ -122,6 +124,7 @@ def __init__(
122124 self .ipv4 = ipv4
123125 self .ipv6 = ipv6
124126 self .retry = retry
127+ self .max_tag_value_length = max_tag_value_length
125128
126129 def export (self , spans : Sequence [Span ]) -> SpanExportResult :
127130 zipkin_spans = self ._translate_to_zipkin (spans )
@@ -141,6 +144,9 @@ def export(self, spans: Sequence[Span]) -> SpanExportResult:
141144 return SpanExportResult .FAILURE
142145 return SpanExportResult .SUCCESS
143146
147+ def shutdown (self ) -> None :
148+ pass
149+
144150 def _translate_to_zipkin (self , spans : Sequence [Span ]):
145151
146152 local_endpoint = {"serviceName" : self .service_name , "port" : self .port }
@@ -171,8 +177,10 @@ def _translate_to_zipkin(self, spans: Sequence[Span]):
171177 "duration" : duration_mus ,
172178 "localEndpoint" : local_endpoint ,
173179 "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+ ),
176184 }
177185
178186 if span .instrumentation_info is not None :
@@ -205,42 +213,44 @@ def _translate_to_zipkin(self, spans: Sequence[Span]):
205213 zipkin_spans .append (zipkin_span )
206214 return zipkin_spans
207215
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
211233
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 ))
215238 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+ )
244254
245255
246256def _nsec_to_usec_round (nsec ):
0 commit comments