示例#1
0
def create_span(
    span_id,
    parent_span_id,
    trace_id,
    span_name,
    annotations,
    binary_annotations,
    timestamp_s,
    duration_s,
):
    """Takes a bunch of span attributes and returns a thriftpy representation
    of the span. Timestamps passed in are in seconds, they're converted to
    microseconds before thrift encoding.
    """
    span_dict = {
        'trace_id': unsigned_hex_to_signed_int(trace_id),
        'name': span_name,
        'id': unsigned_hex_to_signed_int(span_id),
        'annotations': annotations,
        'binary_annotations': binary_annotations,
        'timestamp': int(timestamp_s * 1000000) if timestamp_s else None,
        'duration': int(duration_s * 1000000) if duration_s else None,
    }
    if parent_span_id:
        span_dict['parent_id'] = unsigned_hex_to_signed_int(parent_span_id)
    return zipkin_core.Span(**span_dict)
示例#2
0
 def validate(span_objs):
     assert len(span_objs) == 1
     span = span_objs[0]
     assert span.trace_id == unsigned_hex_to_signed_int(trace_hex)
     assert span.id == unsigned_hex_to_signed_int(span_hex)
     assert span.parent_id == unsigned_hex_to_signed_int(parent_hex)
     # Since Zipkin headers are passed in, the span in assumed to be the
     # server part of a span started by an upstream client, so doesn't have
     # to log timestamp/duration.
     assert span.timestamp is None
     assert span.duration is None
示例#3
0
 def validate_span(span_obj):
     result_span = test_helper.massage_result_span(span_obj)
     timestamps = test_helper.get_timestamps(result_span)
     get_span['trace_id'] = unsigned_hex_to_signed_int(
         default_trace_id_generator(span_obj), )
     assert get_span == result_span
     assert old_time <= timestamps['sr']
     assert timestamps['sr'] <= timestamps['ss']
示例#4
0
 def validate_span(span_obj):
     result_span = test_helper.massage_result_span(span_obj)
     timestamps = test_helper.get_timestamps(result_span)
     get_span['trace_id'] = unsigned_hex_to_signed_int(
         default_trace_id_generator(span_obj),
     )
     assert get_span == result_span
     assert old_time <= timestamps['sr']
     assert timestamps['sr'] <= timestamps['ss']
示例#5
0
def create_span(
    span_id,
    parent_span_id,
    trace_id,
    span_name,
    annotations,
    binary_annotations,
):
    """Takes a bunch of span attributes and returns a thriftpy representation
    of the span.
    """
    span_dict = {
        "trace_id": unsigned_hex_to_signed_int(trace_id),
        "name": span_name,
        "id": unsigned_hex_to_signed_int(span_id),
        "annotations": annotations,
        "binary_annotations": binary_annotations,
    }
    if parent_span_id:
        span_dict["parent_id"] = unsigned_hex_to_signed_int(parent_span_id)
    return zipkin_core.Span(**span_dict)
示例#6
0
def create_span(
    span_id,
    parent_span_id,
    trace_id,
    span_name,
    annotations,
    binary_annotations,
):
    """Takes a bunch of span attributes and returns a thriftpy representation
    of the span.
    """
    span_dict = {
        "trace_id": unsigned_hex_to_signed_int(trace_id),
        "name": span_name,
        "id": unsigned_hex_to_signed_int(span_id),
        "annotations": annotations,
        "binary_annotations": binary_annotations,
    }
    if parent_span_id:
        span_dict["parent_id"] = unsigned_hex_to_signed_int(parent_span_id)
    print("span_dict>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
    #print(json.dumps(span_dict, indent=4))
    if len(span_dict) > 5:
        print(
            "NOTICE: there exists key which not in (trace_id, id, name, annotations, binary_annotations)!!!"
        )
    print("trace_id[int]           :{0}".format(trace_id))
    print("parent_span_id[int]     :{0}".format(parent_span_id))
    print("id[int]                 :{0}".format(span_id))
    print("name[str]               :{0}".format(span_name))
    print("annotations[list]       :{0}".format(len(annotations)))
    for an in annotations:
        print("--an:{0}".format(an))
    print("binary_annotations[list]:{0}".format(len(binary_annotations)))
    for an in binary_annotations:
        print("--ban:{0}".format(an))
    print("span_dict<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<")
    return zipkin_core.Span(**span_dict)
示例#7
0
def create_span(
    span_id,
    parent_span_id,
    trace_id,
    span_name,
    annotations,
    binary_annotations,
    timestamp_s,
    duration_s,
):
    """Takes a bunch of span attributes and returns a thriftpy2 representation
    of the span. Timestamps passed in are in seconds, they're converted to
    microseconds before thrift encoding.
    """
    # Check if trace_id is 128-bit. If so, record trace_id_high separately.
    trace_id_length = len(trace_id)
    trace_id_high = None
    if trace_id_length > 16:
        assert trace_id_length == 32
        trace_id, trace_id_high = trace_id[16:], trace_id[:16]

    if trace_id_high:
        trace_id_high = unsigned_hex_to_signed_int(trace_id_high)

    span_dict = {
        'trace_id': unsigned_hex_to_signed_int(trace_id),
        'name': span_name,
        'id': unsigned_hex_to_signed_int(span_id),
        'annotations': annotations,
        'binary_annotations': binary_annotations,
        'timestamp': int(timestamp_s * 1000000) if timestamp_s else None,
        'duration': int(duration_s * 1000000) if duration_s else None,
        'trace_id_high': trace_id_high,
    }
    if parent_span_id:
        span_dict['parent_id'] = unsigned_hex_to_signed_int(parent_span_id)
    return zipkin_core.Span(**span_dict)
示例#8
0
 def validate_span(span_objs):
     assert len(span_objs) == 1
     span_obj = span_objs[0]
     result_span = test_helper.massage_result_span(span_obj)
     timestamps = test_helper.get_timestamps(result_span)
     get_span['trace_id'] = unsigned_hex_to_signed_int(
         default_trace_id_generator(span_obj), )
     # The request to this service had no incoming Zipkin headers, so it's
     # assumed to be the root span of a trace, which means it logs
     # timestamp and duration.
     assert result_span.pop('timestamp') > old_time
     assert result_span.pop('duration') > 0
     assert get_span == result_span
     assert old_time <= timestamps['sr']
     assert timestamps['sr'] <= timestamps['ss']
示例#9
0
def _hex_to_bytes(hex_id):
    """Encodes to hexadecimal ids to big-endian binary.

    :param hex_id: hexadecimal id to encode.
    :type hex_id: str
    :return: binary representation.
    :type: bytes
    """
    if len(hex_id) <= 16:
        int_id = unsigned_hex_to_signed_int(hex_id)
        return struct.pack(">q", int_id)
    else:
        # There's no 16-bytes encoding in Python's struct. So we convert the
        # id as 2 64 bit ids and then concatenate the result.

        # NOTE: we count 16 chars from the right (:-16) rather than the left so
        # that ids with less than 32 chars will be correctly pre-padded with 0s.
        high_id = unsigned_hex_to_signed_int(hex_id[:-16])
        high_bin = struct.pack(">q", high_id)

        low_id = unsigned_hex_to_signed_int(hex_id[-16:])
        low_bin = struct.pack(">q", low_id)

        return high_bin + low_bin
示例#10
0
def test_unsigned_hex_to_signed_int():
    assert util.unsigned_hex_to_signed_int('17133d482ba4f605') == \
        1662740067609015813
    assert util.unsigned_hex_to_signed_int('b6dbb1c2b362bf51') == \
        -5270423489115668655
示例#11
0
def test_unsigned_hex_to_signed_int():
    assert util.unsigned_hex_to_signed_int("17133d482ba4f605") == 1662740067609015813
    assert util.unsigned_hex_to_signed_int("b6dbb1c2b362bf51") == -5270423489115668655