17
17
from py_zipkin .thrift import zipkin_core
18
18
19
19
_HEX_DIGITS = "0123456789abcdef"
20
- _DROP_ANNOTATIONS = {'cs' , 'sr' , 'ss' , 'cr' }
20
+ _DROP_ANNOTATIONS = {"cs" , "sr" , "ss" , "cr" }
21
21
22
- log = logging .getLogger (' py_zipkin.encoding' )
22
+ log = logging .getLogger (" py_zipkin.encoding" )
23
23
24
24
25
25
def get_decoder (encoding ):
@@ -33,12 +33,10 @@ def get_decoder(encoding):
33
33
if encoding == Encoding .V1_THRIFT :
34
34
return _V1ThriftDecoder ()
35
35
if encoding == Encoding .V1_JSON :
36
- raise NotImplementedError (
37
- '{} decoding not yet implemented' .format (encoding ))
36
+ raise NotImplementedError ("{} decoding not yet implemented" .format (encoding ))
38
37
if encoding == Encoding .V2_JSON :
39
- raise NotImplementedError (
40
- '{} decoding not yet implemented' .format (encoding ))
41
- raise ZipkinError ('Unknown encoding: {}' .format (encoding ))
38
+ raise NotImplementedError ("{} decoding not yet implemented" .format (encoding ))
39
+ raise ZipkinError ("Unknown encoding: {}" .format (encoding ))
42
40
43
41
44
42
class IDecoder (object ):
@@ -56,7 +54,6 @@ def decode_spans(self, spans):
56
54
57
55
58
56
class _V1ThriftDecoder (IDecoder ):
59
-
60
57
def decode_spans (self , spans ):
61
58
"""Decodes an encoded list of spans.
62
59
@@ -89,22 +86,18 @@ def _convert_from_thrift_endpoint(self, thrift_endpoint):
89
86
"""
90
87
ipv4 = None
91
88
ipv6 = None
92
- port = struct .unpack ('H' , struct .pack ('h' , thrift_endpoint .port ))[0 ]
89
+ port = struct .unpack ("H" , struct .pack ("h" , thrift_endpoint .port ))[0 ]
93
90
94
91
if thrift_endpoint .ipv4 != 0 :
95
92
ipv4 = socket .inet_ntop (
96
- socket .AF_INET ,
97
- struct .pack ('!i' , thrift_endpoint .ipv4 ),
93
+ socket .AF_INET , struct .pack ("!i" , thrift_endpoint .ipv4 ),
98
94
)
99
95
100
96
if thrift_endpoint .ipv6 :
101
97
ipv6 = socket .inet_ntop (socket .AF_INET6 , thrift_endpoint .ipv6 )
102
98
103
99
return Endpoint (
104
- service_name = thrift_endpoint .service_name ,
105
- ipv4 = ipv4 ,
106
- ipv6 = ipv6 ,
107
- port = port ,
100
+ service_name = thrift_endpoint .service_name , ipv4 = ipv4 , ipv6 = ipv6 , port = port ,
108
101
)
109
102
110
103
def _decode_thrift_annotations (self , thrift_annotations ):
@@ -127,17 +120,18 @@ def _decode_thrift_annotations(self, thrift_annotations):
127
120
thrift_annotation .host ,
128
121
)
129
122
130
- if 'cs' in all_annotations and 'sr' not in all_annotations :
123
+ if "cs" in all_annotations and "sr" not in all_annotations :
131
124
kind = Kind .CLIENT
132
- timestamp = all_annotations ['cs' ]
133
- duration = all_annotations ['cr' ] - all_annotations ['cs' ]
134
- elif 'cs' not in all_annotations and 'sr' in all_annotations :
125
+ timestamp = all_annotations ["cs" ]
126
+ duration = all_annotations ["cr" ] - all_annotations ["cs" ]
127
+ elif "cs" not in all_annotations and "sr" in all_annotations :
135
128
kind = Kind .SERVER
136
- timestamp = all_annotations ['sr' ]
137
- duration = all_annotations ['ss' ] - all_annotations ['sr' ]
129
+ timestamp = all_annotations ["sr" ]
130
+ duration = all_annotations ["ss" ] - all_annotations ["sr" ]
138
131
139
132
annotations = {
140
- name : self .seconds (ts ) for name , ts in all_annotations .items ()
133
+ name : self .seconds (ts )
134
+ for name , ts in all_annotations .items ()
141
135
if name not in _DROP_ANNOTATIONS
142
136
}
143
137
@@ -152,7 +146,7 @@ def _convert_from_thrift_binary_annotations(self, thrift_binary_annotations):
152
146
remote_endpoint = None
153
147
154
148
for binary_annotation in thrift_binary_annotations :
155
- if binary_annotation .key == 'sa' :
149
+ if binary_annotation .key == "sa" :
156
150
remote_endpoint = self ._convert_from_thrift_endpoint (
157
151
thrift_endpoint = binary_annotation .host ,
158
152
)
@@ -167,8 +161,10 @@ def _convert_from_thrift_binary_annotations(self, thrift_binary_annotations):
167
161
elif annotation_type == zipkin_core .AnnotationType .STRING :
168
162
tags [key ] = value
169
163
else :
170
- log .warning ('Only STRING and BOOL binary annotations are '
171
- 'supported right now and can be properly decoded.' )
164
+ log .warning (
165
+ "Only STRING and BOOL binary annotations are "
166
+ "supported right now and can be properly decoded."
167
+ )
172
168
173
169
if binary_annotation .host :
174
170
local_endpoint = self ._convert_from_thrift_endpoint (
@@ -200,23 +196,28 @@ def _decode_thrift_span(self, thrift_span):
200
196
duration = None
201
197
202
198
if thrift_span .parent_id :
203
- parent_id = self ._convert_unsigned_long_to_lower_hex (
204
- thrift_span .parent_id ,
205
- )
199
+ parent_id = self ._convert_unsigned_long_to_lower_hex (thrift_span .parent_id ,)
206
200
207
201
if thrift_span .annotations :
208
- annotations , local_endpoint , kind , timestamp , duration = \
209
- self ._decode_thrift_annotations (thrift_span .annotations )
202
+ (
203
+ annotations ,
204
+ local_endpoint ,
205
+ kind ,
206
+ timestamp ,
207
+ duration ,
208
+ ) = self ._decode_thrift_annotations (thrift_span .annotations )
210
209
211
210
if thrift_span .binary_annotations :
212
- tags , local_endpoint , remote_endpoint = \
213
- self ._convert_from_thrift_binary_annotations (
214
- thrift_span .binary_annotations ,
215
- )
211
+ (
212
+ tags ,
213
+ local_endpoint ,
214
+ remote_endpoint ,
215
+ ) = self ._convert_from_thrift_binary_annotations (
216
+ thrift_span .binary_annotations ,
217
+ )
216
218
217
219
trace_id = self ._convert_trace_id_to_string (
218
- thrift_span .trace_id ,
219
- thrift_span .trace_id_high ,
220
+ thrift_span .trace_id , thrift_span .trace_id_high ,
220
221
)
221
222
222
223
return Span (
@@ -278,15 +279,15 @@ def _write_hex_long(self, data, pos, value):
278
279
:param value: the value to write
279
280
:type value: unsigned long
280
281
"""
281
- self ._write_hex_byte (data , pos + 0 , (value >> 56 ) & 0xff )
282
- self ._write_hex_byte (data , pos + 2 , (value >> 48 ) & 0xff )
283
- self ._write_hex_byte (data , pos + 4 , (value >> 40 ) & 0xff )
284
- self ._write_hex_byte (data , pos + 6 , (value >> 32 ) & 0xff )
285
- self ._write_hex_byte (data , pos + 8 , (value >> 24 ) & 0xff )
286
- self ._write_hex_byte (data , pos + 10 , (value >> 16 ) & 0xff )
287
- self ._write_hex_byte (data , pos + 12 , (value >> 8 ) & 0xff )
288
- self ._write_hex_byte (data , pos + 14 , (value & 0xff ))
282
+ self ._write_hex_byte (data , pos + 0 , (value >> 56 ) & 0xFF )
283
+ self ._write_hex_byte (data , pos + 2 , (value >> 48 ) & 0xFF )
284
+ self ._write_hex_byte (data , pos + 4 , (value >> 40 ) & 0xFF )
285
+ self ._write_hex_byte (data , pos + 6 , (value >> 32 ) & 0xFF )
286
+ self ._write_hex_byte (data , pos + 8 , (value >> 24 ) & 0xFF )
287
+ self ._write_hex_byte (data , pos + 10 , (value >> 16 ) & 0xFF )
288
+ self ._write_hex_byte (data , pos + 12 , (value >> 8 ) & 0xFF )
289
+ self ._write_hex_byte (data , pos + 14 , (value & 0xFF ))
289
290
290
291
def _write_hex_byte (self , data , pos , byte ):
291
- data [pos + 0 ] = ord (_HEX_DIGITS [int ((byte >> 4 ) & 0xf )])
292
- data [pos + 1 ] = ord (_HEX_DIGITS [int (byte & 0xf )])
292
+ data [pos + 0 ] = ord (_HEX_DIGITS [int ((byte >> 4 ) & 0xF )])
293
+ data [pos + 1 ] = ord (_HEX_DIGITS [int (byte & 0xF )])
0 commit comments