def generate_single_thrift_span(): trace_id = generate_random_128bit_string() span_id = generate_random_64bit_string() timestamp_s = round(time.time(), 3) duration_s = 2.0 host = thrift.create_endpoint(port=8000, service_name='host') host.ipv4 = 2130706433 span = thrift.create_span( span_id=span_id, parent_span_id=None, trace_id=trace_id, span_name='foo', annotations=[ thrift.create_annotation(1472470996199000, "cs", host), ], binary_annotations=[ thrift.create_binary_annotation( "key", "value", zipkin_core.AnnotationType.STRING, host, ), ], timestamp_s=timestamp_s, duration_s=duration_s, ) return thrift.span_to_bytes(span)
async def async_zipkin_log_span(span_id, parent_span_id, trace_id, span_name, annotations, binary_annotations, timestamp_s, duration_s, **kwargs): span = create_span(span_id, parent_span_id, trace_id, span_name, annotations, binary_annotations, timestamp_s, duration_s) await async_http_transport(thrift_objs_in_bytes([span]))
def add_span( self, span_id, parent_span_id, trace_id, span_name, annotations, binary_annotations, timestamp_s, duration_s, ): thrift_span = create_span( span_id, parent_span_id, trace_id, span_name, annotations, binary_annotations, timestamp_s, duration_s, ) self.queue.append(thrift_span) if len(self.queue) >= self.max_portion_size: self.flush()
def log_span( span_id, parent_span_id, trace_id, span_name, annotations, binary_annotations, timestamp_s, duration_s, transport_handler, ): """Creates a span and logs it using the given transport_handler.""" # Be defensive about the lack of a transport handler if not transport_handler: return span = create_span( span_id, parent_span_id, trace_id, span_name, annotations, binary_annotations, timestamp_s, duration_s, ) message = thrift_obj_in_bytes(span) transport_handler(message)
def test_create_span(Span): # Not much logic here so this is just a smoke test. The only # substantive thing is that hex IDs get converted to ints. thrift.create_span( span_id='0000000000000001', parent_span_id='0000000000000002', trace_id='000000000000000f', span_name='foo', annotations='ann', binary_annotations='binary_ann', ) Span.assert_called_once_with( **{ 'id': 1, 'parent_id': 2, 'name': 'foo', 'trace_id': 15, 'name': 'foo', 'annotations': 'ann', 'binary_annotations': 'binary_ann', })
def test_create_span_with_128_bit_trace_ids(): span = thrift.create_span( span_id="0000000000000001", parent_span_id="0000000000000002", trace_id="000000000000000f000000000000000e", span_name="foo", annotations="ann", binary_annotations="binary_ann", timestamp_s=1485920381.2, duration_s=2.0, ) assert span.trace_id == 14 assert span.trace_id_high == 15
def test_create_span_with_128_bit_trace_ids(): span = thrift.create_span( span_id='0000000000000001', parent_span_id='0000000000000002', trace_id='000000000000000f000000000000000e', span_name='foo', annotations='ann', binary_annotations='binary_ann', timestamp_s=1485920381.2, duration_s=2.0, ) assert span.trace_id == 14 assert span.trace_id_high == 15
def test__convert_unsigned_long_to_lower_hex(self): decoder = _V1ThriftDecoder() span_id = generate_random_64bit_string() span = thrift.create_span( span_id, None, generate_random_64bit_string(), 'test_span', [], [], None, None, ) assert decoder._convert_unsigned_long_to_lower_hex(span.id) == span_id
def encode_span(self, span_builder): """Encodes the current span to thrift.""" span = span_builder.build_v1_span() thrift_endpoint = thrift.create_endpoint( span.endpoint.port, span.endpoint.service_name, span.endpoint.ipv4, span.endpoint.ipv6, ) thrift_annotations = thrift.annotation_list_builder( span.annotations, thrift_endpoint, ) thrift_binary_annotations = thrift.binary_annotation_list_builder( span.binary_annotations, thrift_endpoint, ) # Add sa binary annotation if span.sa_endpoint is not None: thrift_sa_endpoint = thrift.create_endpoint( span.sa_endpoint.port, span.sa_endpoint.service_name, span.sa_endpoint.ipv4, span.sa_endpoint.ipv6, ) thrift_binary_annotations.append(thrift.create_binary_annotation( key=thrift.zipkin_core.SERVER_ADDR, value=thrift.SERVER_ADDR_VAL, annotation_type=thrift.zipkin_core.AnnotationType.BOOL, host=thrift_sa_endpoint, )) thrift_span = thrift.create_span( span.id, span.parent_id, span.trace_id, span.name, thrift_annotations, thrift_binary_annotations, span.timestamp, span.duration, ) encoded_span = thrift.span_to_bytes(thrift_span) return encoded_span
def test_create_span_fails_with_wrong_128_bit_trace_id_length(): with pytest.raises(AssertionError): thrift.create_span( span_id="0000000000000001", parent_span_id="0000000000000002", trace_id="000000000000000f000000000000000", span_name="foo", annotations="ann", binary_annotations="binary_ann", timestamp_s=1485920381.2, duration_s=2.0, ) with pytest.raises(AssertionError): thrift.create_span( span_id="0000000000000001", parent_span_id="0000000000000002", trace_id="000000000000000f000000000000000f0", span_name="foo", annotations="ann", binary_annotations="binary_ann", timestamp_s=1485920381.2, duration_s=2.0, )
def test_create_span_fails_with_wrong_128_bit_trace_id_length(): with pytest.raises(AssertionError): thrift.create_span( span_id='0000000000000001', parent_span_id='0000000000000002', trace_id='000000000000000f000000000000000', span_name='foo', annotations='ann', binary_annotations='binary_ann', timestamp_s=1485920381.2, duration_s=2.0, ) with pytest.raises(AssertionError): thrift.create_span( span_id='0000000000000001', parent_span_id='0000000000000002', trace_id='000000000000000f000000000000000f0', span_name='foo', annotations='ann', binary_annotations='binary_ann', timestamp_s=1485920381.2, duration_s=2.0, )
def test__convert_trace_id_to_string(self, trace_id_generator): decoder = _V1ThriftDecoder() trace_id = trace_id_generator() span = thrift.create_span( generate_random_64bit_string(), None, trace_id, 'test_span', [], [], None, None, ) assert decoder._convert_trace_id_to_string( span.trace_id, span.trace_id_high, ) == trace_id
def encode_span(self, v2_span): """Encodes the current span to thrift.""" span = v2_span.build_v1_span() thrift_endpoint = thrift.create_endpoint( span.endpoint.port, span.endpoint.service_name, span.endpoint.ipv4, span.endpoint.ipv6, ) thrift_annotations = thrift.annotation_list_builder( span.annotations, thrift_endpoint, ) thrift_binary_annotations = thrift.binary_annotation_list_builder( span.binary_annotations, thrift_endpoint, ) # Add sa/ca binary annotations if v2_span.remote_endpoint: self.encode_remote_endpoint( v2_span.remote_endpoint, v2_span.kind, thrift_binary_annotations, ) thrift_span = thrift.create_span( span.id, span.parent_id, span.trace_id, span.name, thrift_annotations, thrift_binary_annotations, span.timestamp, span.duration, ) encoded_span = thrift.span_to_bytes(thrift_span) return encoded_span
def log_span( span_id, parent_span_id, trace_id, span_name, annotations, binary_annotations, transport_handler, ): """Creates a span and logs it using the given transport_handler.""" span = create_span( span_id, parent_span_id, trace_id, span_name, annotations, binary_annotations, ) message = thrift_obj_in_bytes(span) transport_handler(message)
def test_create_span(): # Not much logic here so this is just a smoke test. The only # substantive thing is that hex IDs get converted to ints. span = thrift.create_span( span_id='0000000000000001', parent_span_id='0000000000000002', trace_id='000000000000000f', span_name='foo', annotations='ann', binary_annotations='binary_ann', timestamp_s=1485920381.2, duration_s=2.0, ) assert span.id == 1 assert span.parent_id == 2 assert span.trace_id == 15 assert span.name == 'foo' assert span.annotations == 'ann' assert span.binary_annotations == 'binary_ann' assert span.timestamp == 1485920381.2 * 1000000 assert span.duration == 2.0 * 1000000 assert span.trace_id_high is None
def add_span( self, span_id, parent_span_id, trace_id, span_name, annotations, binary_annotations, timestamp_s, duration_s, endpoint, sa_endpoint, ): thrift_endpoint = thrift.create_endpoint( endpoint.port, endpoint.service_name, endpoint.ipv4, endpoint.ipv6, ) thrift_annotations = thrift.annotation_list_builder( annotations, thrift_endpoint, ) # Binary annotations can be set through debug messages or the # set_extra_binary_annotations registry setting. thrift_binary_annotations = thrift.binary_annotation_list_builder( binary_annotations, thrift_endpoint, ) # Add sa binary annotation if sa_endpoint is not None: thrift_sa_endpoint = thrift.create_endpoint( sa_endpoint.port, sa_endpoint.service_name, sa_endpoint.ipv4, sa_endpoint.ipv6, ) thrift_binary_annotations.append(thrift.create_binary_annotation( key=thrift.zipkin_core.SERVER_ADDR, value=thrift.SERVER_ADDR_VAL, annotation_type=thrift.zipkin_core.AnnotationType.BOOL, host=thrift_sa_endpoint, )) thrift_span = thrift.create_span( span_id, parent_span_id, trace_id, span_name, thrift_annotations, thrift_binary_annotations, timestamp_s, duration_s, ) encoded_span = thrift.span_to_bytes(thrift_span) # If we've already reached the max batch size or the new span doesn't # fit in max_payload_bytes, send what we've collected until now and # start a new batch. is_over_size_limit = ( self.max_payload_bytes is not None and self.current_size + len(encoded_span) > self.max_payload_bytes ) is_over_portion_limit = len(self.queue) >= self.max_portion_size if is_over_size_limit or is_over_portion_limit: self.flush() self.queue.append(encoded_span) self.current_size += len(encoded_span)
def check_v1_thrift(obj, zipkin_attrs, inner_span_id, ts): inner_span, producer_span, root_span = _decode_binary_thrift_objs(obj) endpoint = thrift.create_endpoint( port=8080, service_name="test_service_name", ipv4="10.0.0.0", ) binary_annotations = thrift.binary_annotation_list_builder( {"some_key": "some_value"}, endpoint, ) binary_annotations.append( thrift.create_binary_annotation( "sa", "\x01", zipkin_core.AnnotationType.BOOL, thrift.create_endpoint( port=8888, service_name="sa_service", ipv6="2001:0db8:85a3:0000:0000:8a2e:0370:7334", ), ) ) expected_root = thrift.create_span( span_id=zipkin_attrs.span_id, parent_span_id=zipkin_attrs.parent_span_id, trace_id=zipkin_attrs.trace_id, span_name="test_span_name", annotations=thrift.annotation_list_builder( OrderedDict([("cs", ts), ("cr", ts + 10)]), endpoint, ), binary_annotations=binary_annotations, timestamp_s=None, duration_s=None, ) # py.test diffs of thrift Spans are pretty useless and hide many things # These prints would only appear on stdout if the test fails and help comparing # the 2 spans. print(root_span) print(expected_root) assert root_span == expected_root expected_inner = thrift.create_span( span_id=inner_span_id, parent_span_id=zipkin_attrs.span_id, trace_id=zipkin_attrs.trace_id, span_name="inner_span", annotations=thrift.annotation_list_builder( OrderedDict([("ws", ts)]), endpoint, ), binary_annotations=[], timestamp_s=ts, duration_s=5, ) # py.test diffs of thrift Spans are pretty useless and hide many things # These prints would only appear on stdout if the test fails and help comparing # the 2 spans. print(inner_span) print(expected_inner) assert inner_span == expected_inner expected_producer = thrift.create_span( span_id=inner_span_id, parent_span_id=zipkin_attrs.span_id, trace_id=zipkin_attrs.trace_id, span_name="producer_span", annotations=thrift.annotation_list_builder( OrderedDict([("ms", ts)]), endpoint, ), binary_annotations=[], timestamp_s=ts, duration_s=10, ) # py.test diffs of thrift Spans are pretty useless and hide many things # These prints would only appear on stdout if the test fails and help comparing # the 2 spans. print(producer_span) print(expected_producer) assert producer_span == expected_producer
def check_v1_thrift(obj, zipkin_attrs, inner_span_id, ts): inner_span, root_span = _decode_binary_thrift_objs(obj) endpoint = thrift.create_endpoint( port=8080, service_name='test_service_name', ipv4='10.0.0.0', ) binary_annotations = thrift.binary_annotation_list_builder( {'some_key': 'some_value'}, endpoint, ) binary_annotations.append(thrift.create_binary_annotation( 'sa', '\x01', zipkin_core.AnnotationType.BOOL, thrift.create_endpoint( port=8888, service_name='sa_service', ipv6='2001:0db8:85a3:0000:0000:8a2e:0370:7334', ), )) expected_root = thrift.create_span( span_id=zipkin_attrs.span_id, parent_span_id=zipkin_attrs.parent_span_id, trace_id=zipkin_attrs.trace_id, span_name='test_span_name', annotations=thrift.annotation_list_builder( OrderedDict([('cs', ts), ('cr', ts + 10)]), endpoint, ), binary_annotations=binary_annotations, timestamp_s=None, duration_s=None, ) # py.test diffs of thrift Spans are pretty useless and hide many things # These prints would only appear on stdout if the test fails and help comparing # the 2 spans. print(root_span) print(expected_root) assert root_span == expected_root expected_inner = thrift.create_span( span_id=inner_span_id, parent_span_id=zipkin_attrs.span_id, trace_id=zipkin_attrs.trace_id, span_name='inner_span', annotations=thrift.annotation_list_builder( OrderedDict([('cs', ts), ('sr', ts), ('ss', ts + 5), ('cr', ts + 5), ('ws', ts)]), endpoint, ), binary_annotations=[], timestamp_s=ts, duration_s=5, ) # py.test diffs of thrift Spans are pretty useless and hide many things # These prints would only appear on stdout if the test fails and help comparing # the 2 spans. print(inner_span) print(expected_inner) assert inner_span == expected_inner