Skip to content

Commit

Permalink
Use black to format code
Browse files Browse the repository at this point in the history
  • Loading branch information
drolando committed Feb 27, 2020
1 parent 86a2d7c commit 32aa1c4
Show file tree
Hide file tree
Showing 36 changed files with 2,106 additions and 2,016 deletions.
17 changes: 8 additions & 9 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
exclude: '^py_zipkin/encoding/protobuf/zipkin_pb2.py$'
repos:
- repo: git://github.com/pre-commit/pre-commit-hooks
rev: v2.1.0
rev: v2.5.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
Expand All @@ -11,17 +11,16 @@ repos:
- id: debug-statements
- id: name-tests-test
exclude: tests/test_helpers.py
- id: flake8
args:
- --max-line-length=83
exclude: docs/source/conf.py
- id: requirements-txt-fixer
- repo: https://github.com/asottile/reorder_python_imports.git
rev: v1.3.5
rev: v1.9.0
hooks:
- id: reorder-python-imports
language_version: python2.7
- repo: https://github.com/pre-commit/mirrors-autopep8
rev: v1.4.3
- repo: https://github.com/psf/black
rev: 19.10b0
hooks:
- id: autopep8
- id: black
language_version: python3.7
exclude: setup.py
args: [--target-version, py27]
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@ matrix:
- python: 3.7
env: TOXENV=py37
- python: 3.7
env: TOXENV=flake8
env: TOXENV=black
before_install: pip install -U pip==18.0
install: pip install tox coveralls
script: tox
after_success: coveralls
sudo: false
deploy:
provider: pypi
user: yelplabs
Expand Down
14 changes: 5 additions & 9 deletions py_zipkin/encoding/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def detect_span_version_and_encoding(message):
if six.PY2:
message = six.b(message) # pragma: no cover
else:
message = message.encode('utf-8') # pragma: no cover
message = message.encode("utf-8") # pragma: no cover

if len(message) < 2:
raise ZipkinError("Invalid span format. Message too short.")
Expand All @@ -44,10 +44,10 @@ def detect_span_version_and_encoding(message):
return Encoding.V2_PROTO3
return Encoding.V1_THRIFT

str_msg = message.decode('utf-8')
str_msg = message.decode("utf-8")

# JSON case for list of spans
if str_msg[0] == '[':
if str_msg[0] == "[":
span_list = json.loads(str_msg)
if len(span_list) > 0:
# Assumption: All spans in a list are the same version
Expand All @@ -57,12 +57,8 @@ def detect_span_version_and_encoding(message):
for span in span_list:
if any(word in span for word in _V2_ATTRIBUTES):
return Encoding.V2_JSON
elif (
'binaryAnnotations' in span or
(
'annotations' in span and
'endpoint' in span['annotations']
)
elif "binaryAnnotations" in span or (
"annotations" in span and "endpoint" in span["annotations"]
):
return Encoding.V1_JSON
return Encoding.V2_JSON
Expand Down
93 changes: 47 additions & 46 deletions py_zipkin/encoding/_decoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
from py_zipkin.thrift import zipkin_core

_HEX_DIGITS = "0123456789abcdef"
_DROP_ANNOTATIONS = {'cs', 'sr', 'ss', 'cr'}
_DROP_ANNOTATIONS = {"cs", "sr", "ss", "cr"}

log = logging.getLogger('py_zipkin.encoding')
log = logging.getLogger("py_zipkin.encoding")


def get_decoder(encoding):
Expand All @@ -33,12 +33,10 @@ def get_decoder(encoding):
if encoding == Encoding.V1_THRIFT:
return _V1ThriftDecoder()
if encoding == Encoding.V1_JSON:
raise NotImplementedError(
'{} decoding not yet implemented'.format(encoding))
raise NotImplementedError("{} decoding not yet implemented".format(encoding))
if encoding == Encoding.V2_JSON:
raise NotImplementedError(
'{} decoding not yet implemented'.format(encoding))
raise ZipkinError('Unknown encoding: {}'.format(encoding))
raise NotImplementedError("{} decoding not yet implemented".format(encoding))
raise ZipkinError("Unknown encoding: {}".format(encoding))


class IDecoder(object):
Expand All @@ -56,7 +54,6 @@ def decode_spans(self, spans):


class _V1ThriftDecoder(IDecoder):

def decode_spans(self, spans):
"""Decodes an encoded list of spans.
Expand Down Expand Up @@ -89,22 +86,18 @@ def _convert_from_thrift_endpoint(self, thrift_endpoint):
"""
ipv4 = None
ipv6 = None
port = struct.unpack('H', struct.pack('h', thrift_endpoint.port))[0]
port = struct.unpack("H", struct.pack("h", thrift_endpoint.port))[0]

if thrift_endpoint.ipv4 != 0:
ipv4 = socket.inet_ntop(
socket.AF_INET,
struct.pack('!i', thrift_endpoint.ipv4),
socket.AF_INET, struct.pack("!i", thrift_endpoint.ipv4),
)

if thrift_endpoint.ipv6:
ipv6 = socket.inet_ntop(socket.AF_INET6, thrift_endpoint.ipv6)

return Endpoint(
service_name=thrift_endpoint.service_name,
ipv4=ipv4,
ipv6=ipv6,
port=port,
service_name=thrift_endpoint.service_name, ipv4=ipv4, ipv6=ipv6, port=port,
)

def _decode_thrift_annotations(self, thrift_annotations):
Expand All @@ -127,17 +120,18 @@ def _decode_thrift_annotations(self, thrift_annotations):
thrift_annotation.host,
)

if 'cs' in all_annotations and 'sr' not in all_annotations:
if "cs" in all_annotations and "sr" not in all_annotations:
kind = Kind.CLIENT
timestamp = all_annotations['cs']
duration = all_annotations['cr'] - all_annotations['cs']
elif 'cs' not in all_annotations and 'sr' in all_annotations:
timestamp = all_annotations["cs"]
duration = all_annotations["cr"] - all_annotations["cs"]
elif "cs" not in all_annotations and "sr" in all_annotations:
kind = Kind.SERVER
timestamp = all_annotations['sr']
duration = all_annotations['ss'] - all_annotations['sr']
timestamp = all_annotations["sr"]
duration = all_annotations["ss"] - all_annotations["sr"]

annotations = {
name: self.seconds(ts) for name, ts in all_annotations.items()
name: self.seconds(ts)
for name, ts in all_annotations.items()
if name not in _DROP_ANNOTATIONS
}

Expand All @@ -152,7 +146,7 @@ def _convert_from_thrift_binary_annotations(self, thrift_binary_annotations):
remote_endpoint = None

for binary_annotation in thrift_binary_annotations:
if binary_annotation.key == 'sa':
if binary_annotation.key == "sa":
remote_endpoint = self._convert_from_thrift_endpoint(
thrift_endpoint=binary_annotation.host,
)
Expand All @@ -167,8 +161,10 @@ def _convert_from_thrift_binary_annotations(self, thrift_binary_annotations):
elif annotation_type == zipkin_core.AnnotationType.STRING:
tags[key] = value
else:
log.warning('Only STRING and BOOL binary annotations are '
'supported right now and can be properly decoded.')
log.warning(
"Only STRING and BOOL binary annotations are "
"supported right now and can be properly decoded."
)

if binary_annotation.host:
local_endpoint = self._convert_from_thrift_endpoint(
Expand Down Expand Up @@ -200,23 +196,28 @@ def _decode_thrift_span(self, thrift_span):
duration = None

if thrift_span.parent_id:
parent_id = self._convert_unsigned_long_to_lower_hex(
thrift_span.parent_id,
)
parent_id = self._convert_unsigned_long_to_lower_hex(thrift_span.parent_id)

if thrift_span.annotations:
annotations, local_endpoint, kind, timestamp, duration = \
self._decode_thrift_annotations(thrift_span.annotations)
(
annotations,
local_endpoint,
kind,
timestamp,
duration,
) = self._decode_thrift_annotations(thrift_span.annotations)

if thrift_span.binary_annotations:
tags, local_endpoint, remote_endpoint = \
self._convert_from_thrift_binary_annotations(
thrift_span.binary_annotations,
)
(
tags,
local_endpoint,
remote_endpoint,
) = self._convert_from_thrift_binary_annotations(
thrift_span.binary_annotations,
)

trace_id = self._convert_trace_id_to_string(
thrift_span.trace_id,
thrift_span.trace_id_high,
thrift_span.trace_id, thrift_span.trace_id_high,
)

return Span(
Expand Down Expand Up @@ -278,15 +279,15 @@ def _write_hex_long(self, data, pos, value):
:param value: the value to write
:type value: unsigned long
"""
self._write_hex_byte(data, pos + 0, (value >> 56) & 0xff)
self._write_hex_byte(data, pos + 2, (value >> 48) & 0xff)
self._write_hex_byte(data, pos + 4, (value >> 40) & 0xff)
self._write_hex_byte(data, pos + 6, (value >> 32) & 0xff)
self._write_hex_byte(data, pos + 8, (value >> 24) & 0xff)
self._write_hex_byte(data, pos + 10, (value >> 16) & 0xff)
self._write_hex_byte(data, pos + 12, (value >> 8) & 0xff)
self._write_hex_byte(data, pos + 14, (value & 0xff))
self._write_hex_byte(data, pos + 0, (value >> 56) & 0xFF)
self._write_hex_byte(data, pos + 2, (value >> 48) & 0xFF)
self._write_hex_byte(data, pos + 4, (value >> 40) & 0xFF)
self._write_hex_byte(data, pos + 6, (value >> 32) & 0xFF)
self._write_hex_byte(data, pos + 8, (value >> 24) & 0xFF)
self._write_hex_byte(data, pos + 10, (value >> 16) & 0xFF)
self._write_hex_byte(data, pos + 12, (value >> 8) & 0xFF)
self._write_hex_byte(data, pos + 14, (value & 0xFF))

def _write_hex_byte(self, data, pos, byte):
data[pos + 0] = ord(_HEX_DIGITS[int((byte >> 4) & 0xf)])
data[pos + 1] = ord(_HEX_DIGITS[int(byte & 0xf)])
data[pos + 0] = ord(_HEX_DIGITS[int((byte >> 4) & 0xF)])
data[pos + 1] = ord(_HEX_DIGITS[int(byte & 0xF)])

0 comments on commit 32aa1c4

Please sign in to comment.