Ejemplo n.º 1
0
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)
Ejemplo n.º 2
0
def test_generate_random_128bit_string(rand):
    rand.return_value = b'17133d482ba4f60517133d482ba4f605'
    random_string = util.generate_random_128bit_string()
    assert random_string == '17133d482ba4f60517133d482ba4f605'
    # This acts as a contract test of sorts. This should return a str
    # in both py2 and py3. IOW, no unicode objects.
    assert isinstance(random_string, str)
Ejemplo n.º 3
0
def test_generate_random_128bit_string(rand, mock_time):
    rand.return_value = 0x2ba4f60517133d482ba4f605
    mock_time.return_value = float(0x17133d48)
    random_string = util.generate_random_128bit_string()
    assert random_string == '17133d482ba4f60517133d482ba4f605'
    rand.assert_called_once_with(96)  # 96 bits
    # This acts as a contract test of sorts. This should return a str
    # in both py2 and py3. IOW, no unicode objects.
    assert isinstance(random_string, str)
Ejemplo n.º 4
0
    def __get_span_attrs(self, use_128bit_trace_id=False):
        parent_span_id = self.__get_parent_span_id()
        trace_id = self.__get_trace_id()

        if trace_id is None:
            if use_128bit_trace_id:
                trace_id = generate_random_128bit_string()
            else:
                trace_id = generate_random_64bit_string()

        is_sampled = self.__is_sampled()
        span_id = generate_random_64bit_string()

        return ZipkinAttrs(
                trace_id=trace_id,
                span_id=span_id,
                parent_span_id=parent_span_id,
                flags=self.__get_flags(),
                is_sampled=is_sampled,
        )
Ejemplo n.º 5
0
def create_attrs_for_span(
    sample_rate=100.0,
    trace_id=None,
    span_id=None,
    use_128bit_trace_id=False,
):
    """Creates a set of zipkin attributes for a span.

    :param sample_rate: Float between 0.0 and 100.0 to determine sampling rate
    :type sample_rate: float
    :param trace_id: Optional 16-character hex string representing a trace_id.
                    If this is None, a random trace_id will be generated.
    :type trace_id: str
    :param span_id: Optional 16-character hex string representing a span_id.
                    If this is None, a random span_id will be generated.
    :type span_id: str
    :param use_128bit_trace_id: If true, generate 128-bit trace_ids
    :type use_128bit_trace_id: boolean
    """
    # Calculate if this trace is sampled based on the sample rate
    if trace_id is None:
        if use_128bit_trace_id:
            trace_id = generate_random_128bit_string()
        else:
            trace_id = generate_random_64bit_string()
    if span_id is None:
        span_id = generate_random_64bit_string()
    if sample_rate == 0.0:
        is_sampled = False
    else:
        is_sampled = (random.random() * 100) < sample_rate

    return ZipkinAttrs(
        trace_id=trace_id,
        span_id=span_id,
        parent_span_id=None,
        flags='0',
        is_sampled=is_sampled,
    )
Ejemplo n.º 6
0
 def generate_new_trace(self):
     if self._use_128bit_trace_id == True:
         return util.generate_random_128bit_string()
     return util.generate_random_64bit_string()