Beispiel #1
0
def test_create_protobuf_span():
    span = Span(
        trace_id="1",
        name="name",
        parent_id="2",
        span_id="3",
        kind=Kind.CLIENT,
        timestamp=10,
        duration=10,
        local_endpoint=create_endpoint(service_name="service1", use_defaults=False),
        remote_endpoint=create_endpoint(service_name="service2", use_defaults=False),
        debug=True,
        shared=True,
        annotations={"foo": 1},
        tags={"key": "value"},
    )

    pb_span = protobuf.create_protobuf_span(span)

    assert pb_span == zipkin_pb2.Span(
        trace_id=b"\000\000\000\000\000\000\000\001",
        parent_id=b"\000\000\000\000\000\000\000\002",
        id=b"\000\000\000\000\000\000\000\003",
        kind=zipkin_pb2.Span.CLIENT,
        name="name",
        timestamp=10000000,
        duration=10000000,
        local_endpoint=zipkin_pb2.Endpoint(service_name="service1"),
        remote_endpoint=zipkin_pb2.Endpoint(service_name="service2"),
        debug=True,
        shared=True,
        annotations=[zipkin_pb2.Annotation(timestamp=1000000, value="foo")],
        tags={"key": "value"},
    )
Beispiel #2
0
def test_convert_endpoint():
    endpoint = create_endpoint(8888, "service1", "127.0.0.1")
    assert protobuf._convert_endpoint(endpoint) == zipkin_pb2.Endpoint(
        service_name="service1", port=8888, ipv4=b"\177\000\000\001", ipv6=None,
    )

    ipv6_endpoint = create_endpoint(0, "service1", "fe80::1ff:fe23:4567:890")
    assert protobuf._convert_endpoint(ipv6_endpoint) == zipkin_pb2.Endpoint(
        service_name="service1",
        ipv4=None,
        ipv6=b"\376\200\000\000\000\000\000\000\001\377\376#Eg\010\220",
    )
Beispiel #3
0
def test_add_sa_binary_annotation():
    zipkin_attrs = ZipkinAttrs(
        trace_id='0',
        span_id='1',
        parent_span_id=None,
        flags='0',
        is_sampled=True,
    )
    context = zipkin.zipkin_span(
        service_name='my_service',
        span_name='span_name',
        zipkin_attrs=zipkin_attrs,
        transport_handler=MockTransportHandler(),
        port=5,
        kind=Kind.CLIENT,
    )

    with context:
        assert context.logging_context.sa_endpoint is None
        context.add_sa_binary_annotation(
            port=123,
            service_name='test_service',
            host='1.2.3.4',
        )
        expected_sa_endpoint = create_endpoint(
            port=123,
            service_name='test_service',
            host='1.2.3.4',
        )
        assert context.logging_context.sa_endpoint == \
            expected_sa_endpoint

        nested_context = zipkin.zipkin_span(
            service_name='my_service',
            span_name='nested_span',
            kind=Kind.CLIENT,
        )
        with nested_context:
            nested_context.add_sa_binary_annotation(
                port=456,
                service_name='nested_service',
                host='5.6.7.8',
            )
            expected_nested_sa_endpoint = create_endpoint(
                port=456,
                service_name='nested_service',
                host='5.6.7.8',
            )
            assert nested_context.sa_endpoint == \
                expected_nested_sa_endpoint
Beispiel #4
0
def test_batch_sender_add_span_not_sampled_with_firehose(
        add_span_mock, flush_mock, time_mock):
    attr = ZipkinAttrs(
        trace_id='0000000000000001',
        span_id='0000000000000002',
        parent_span_id=None,
        flags=None,
        is_sampled=False,
    )
    span_storage = SpanStorage()
    transport_handler = mock.Mock()
    firehose_handler = mock.Mock()

    context = logging_helper.ZipkinLoggingContext(
        zipkin_attrs=attr,
        endpoint=create_endpoint(80, 'test_server', '127.0.0.1'),
        span_name='span_name',
        transport_handler=transport_handler,
        report_root_timestamp=False,
        span_storage=span_storage,
        firehose_handler=firehose_handler,
        service_name='test_server',
        encoding=Encoding.V1_JSON,
    )
    context.start_timestamp = 24
    context.response_status_code = 200

    context.binary_annotations_dict = {'k': 'v'}
    time_mock.return_value = 42

    context.emit_spans()
    assert add_span_mock.call_count == 1
    assert flush_mock.call_count == 1
Beispiel #5
0
def test_batch_sender_add_span_not_called_if_not_sampled(
        add_span_mock, flush_mock):
    attr = ZipkinAttrs(
        trace_id='0000000000000001',
        span_id='0000000000000002',
        parent_span_id=None,
        flags=None,
        is_sampled=False,
    )
    span_storage = SpanStorage()
    transport_handler = mock.Mock()

    context = logging_helper.ZipkinLoggingContext(
        zipkin_attrs=attr,
        endpoint=create_endpoint(80, 'test_server', '127.0.0.1'),
        span_name='span_name',
        transport_handler=transport_handler,
        report_root_timestamp=False,
        span_storage=span_storage,
        service_name='test_server',
        encoding=Encoding.V1_JSON,
    )
    context.emit_spans()
    assert add_span_mock.call_count == 0
    assert flush_mock.call_count == 0
Beispiel #6
0
def test_batch_sender_add_span_not_sampled_with_firehose(
    add_span_mock, flush_mock, time_mock
):
    attr = ZipkinAttrs(
        trace_id="0000000000000001",
        span_id="0000000000000002",
        parent_span_id=None,
        flags=None,
        is_sampled=False,
    )
    tracer = MockTracer()
    transport_handler = mock.Mock()
    firehose_handler = mock.Mock()

    context = logging_helper.ZipkinLoggingContext(
        zipkin_attrs=attr,
        endpoint=create_endpoint(80, "test_server", "127.0.0.1"),
        span_name="span_name",
        transport_handler=transport_handler,
        report_root_timestamp=False,
        get_tracer=lambda: tracer,
        firehose_handler=firehose_handler,
        service_name="test_server",
        encoding=Encoding.V1_JSON,
    )
    context.start_timestamp = 24
    context.response_status_code = 200

    context.tags = {"k": "v"}
    time_mock.return_value = 42

    context.emit_spans()
    assert add_span_mock.call_count == 1
    assert flush_mock.call_count == 1
Beispiel #7
0
    def test_stop_non_root(self, mock_time):
        tracer = MockTracer()
        tracer.set_transport_configured(configured=True)
        tracer.get_context().push(zipkin.create_attrs_for_span())
        context = tracer.zipkin_span(
            service_name="test_service", span_name="test_span",
        )
        context.start()

        context.stop()
        assert len(tracer.get_spans()) == 1
        endpoint = create_endpoint(service_name="test_service")
        assert tracer.get_spans()[0] == Span(
            trace_id=context.zipkin_attrs.trace_id,
            name="test_span",
            parent_id=context.zipkin_attrs.parent_span_id,
            span_id=context.zipkin_attrs.span_id,
            kind=Kind.LOCAL,
            timestamp=123,
            duration=0,
            annotations={},
            local_endpoint=endpoint,
            remote_endpoint=None,
            tags={},
        )

        assert tracer.is_transport_configured() is True
Beispiel #8
0
    def test_stop_non_root_ts_duration_overridden(self):
        tracer = MockTracer()
        tracer.set_transport_configured(configured=True)
        tracer.get_context().push(zipkin.create_attrs_for_span())
        ts = time.time()
        context = tracer.zipkin_span(
            service_name='test_service',
            span_name='test_span',
            timestamp=ts,
            duration=25,
        )
        context.start()

        context.stop()
        assert len(tracer.get_spans()) == 1
        endpoint = create_endpoint(service_name='test_service')
        assert tracer.get_spans()[0] == Span(
            trace_id=context.zipkin_attrs.trace_id,
            name='test_span',
            parent_id=context.zipkin_attrs.parent_span_id,
            span_id=context.zipkin_attrs.span_id,
            kind=Kind.LOCAL,
            timestamp=ts,
            duration=25,
            annotations={},
            local_endpoint=endpoint,
            remote_endpoint=None,
            tags={},
        )

        assert tracer.is_transport_configured() is True
Beispiel #9
0
    def test_remote_endpoint(self):
        encoder = get_encoder(Encoding.V1_THRIFT)
        remote_endpoint = create_endpoint(service_name="test_server", host="127.0.0.1")

        # For server spans, the remote endpoint is encoded as 'ca'
        binary_annotations = thrift.binary_annotation_list_builder({}, None)
        encoder.encode_remote_endpoint(
            remote_endpoint, Kind.SERVER, binary_annotations,
        )
        assert binary_annotations == [
            thrift.create_binary_annotation(
                key="ca",
                value=thrift.SERVER_ADDR_VAL,
                annotation_type=thrift.zipkin_core.AnnotationType.BOOL,
                host=thrift.create_endpoint(0, "test_server", "127.0.0.1", None),
            )
        ]

        # For client spans, the remote endpoint is encoded as 'sa'
        binary_annotations = thrift.binary_annotation_list_builder({}, None)
        encoder.encode_remote_endpoint(
            remote_endpoint, Kind.CLIENT, binary_annotations,
        )
        assert binary_annotations == [
            thrift.create_binary_annotation(
                key="sa",
                value=thrift.SERVER_ADDR_VAL,
                annotation_type=thrift.zipkin_core.AnnotationType.BOOL,
                host=thrift.create_endpoint(0, "test_server", "127.0.0.1", None),
            )
        ]
Beispiel #10
0
    def test_remote_endpoint(self):
        encoder = get_encoder(Encoding.V1_JSON)
        remote_endpoint = create_endpoint(service_name="test_server", host="127.0.0.1")

        # For server spans, the remote endpoint is encoded as 'ca'
        binary_annotations = []
        encoder.encode_remote_endpoint(
            remote_endpoint, Kind.SERVER, binary_annotations,
        )
        assert binary_annotations == [
            {
                "endpoint": {"ipv4": "127.0.0.1", "serviceName": "test_server"},
                "key": "ca",
                "value": True,
            }
        ]

        # For client spans, the remote endpoint is encoded as 'sa'
        binary_annotations = []
        encoder.encode_remote_endpoint(
            remote_endpoint, Kind.CLIENT, binary_annotations,
        )
        assert binary_annotations == [
            {
                "endpoint": {"ipv4": "127.0.0.1", "serviceName": "test_server"},
                "key": "sa",
                "value": True,
            }
        ]
Beispiel #11
0
def test_create_endpoint_correct_host_ip(gethostbyname):
    gethostbyname.return_value = "1.2.3.4"
    endpoint = create_endpoint(host="0.0.0.0")

    assert endpoint.service_name == "unknown"
    assert endpoint.port == 0
    assert endpoint.ipv4 == "0.0.0.0"
    assert endpoint.ipv6 is None
Beispiel #12
0
def test_create_endpoint_correct_host_ip(gethostbyname):
    gethostbyname.return_value = '1.2.3.4'
    endpoint = create_endpoint(host='0.0.0.0')

    assert endpoint.service_name == 'unknown'
    assert endpoint.port == 0
    assert endpoint.ipv4 == '0.0.0.0'
    assert endpoint.ipv6 is None
Beispiel #13
0
def test_create_endpoint_defaults_localhost(gethostbyname):
    gethostbyname.side_effect = socket.gaierror

    endpoint = create_endpoint(port=8080, service_name="foo")
    assert endpoint.service_name == "foo"
    assert endpoint.port == 8080
    assert endpoint.ipv4 == "127.0.0.1"
    assert endpoint.ipv6 is None
Beispiel #14
0
def test_create_endpoint_ipv6():
    endpoint = create_endpoint(
        port=8080, service_name="foo", host="2001:0db8:85a3:0000:0000:8a2e:0370:7334",
    )
    assert endpoint.service_name == "foo"
    assert endpoint.port == 8080
    assert endpoint.ipv4 is None
    assert endpoint.ipv6 == "2001:0db8:85a3:0000:0000:8a2e:0370:7334"
Beispiel #15
0
def test_create_endpoint_defaults_service_name(gethostbyname):
    gethostbyname.return_value = "0.0.0.0"
    endpoint = create_endpoint(port=8080)

    assert endpoint.service_name == "unknown"
    assert endpoint.port == 8080
    assert endpoint.ipv4 == "0.0.0.0"
    assert endpoint.ipv6 is None
Beispiel #16
0
    def start(self):
        """Enter the new span context. All annotations logged inside this
        context will be attributed to this span. All new spans generated
        inside this context will have this span as their parent.

        In the unsampled case, this context still generates new span IDs and
        pushes them onto the threadlocal stack, so downstream services calls
        made will pass the correct headers. However, the logging handler is
        never attached in the unsampled case, so the spans are never logged.
        """
        self.do_pop_attrs = False

        report_root_timestamp, self.zipkin_attrs = self._get_current_context()

        # If zipkin_attrs are not set up by now, that means this span is not
        # configured to perform logging itself, and it's not in an existing
        # Zipkin trace. That means there's nothing else to do and it can exit
        # early.
        if not self.zipkin_attrs:
            return self

        self.get_tracer().push_zipkin_attrs(self.zipkin_attrs)
        self.do_pop_attrs = True

        self.start_timestamp = time.time()

        if self._is_local_root_span:
            # Don't set up any logging if we're not sampling
            if not self.zipkin_attrs.is_sampled and not self.firehose_handler:
                return self
            # If transport is already configured don't override it. Doing so would
            # cause all previously recorded spans to never be emitted as exiting
            # the inner logging context will reset transport_configured to False.
            if self.get_tracer().is_transport_configured():
                log.info("Transport was already configured, ignoring override "
                         "from span {}".format(self.span_name))
                return self
            endpoint = create_endpoint(self.port, self.service_name, self.host)
            self.logging_context = ZipkinLoggingContext(
                self.zipkin_attrs,
                endpoint,
                self.span_name,
                self.transport_handler,
                report_root_timestamp or self.report_root_timestamp_override,
                self.get_tracer,
                self.service_name,
                binary_annotations=self.binary_annotations,
                add_logging_annotation=self.add_logging_annotation,
                client_context=self.kind == Kind.CLIENT,
                max_span_batch_size=self.max_span_batch_size,
                firehose_handler=self.firehose_handler,
                encoding=self.encoding,
                annotations=self.annotations,
            )
            self.logging_context.start()
            self.get_tracer().set_transport_configured(configured=True)

        return self
Beispiel #17
0
    def stop(self, _exc_type=None, _exc_value=None, _exc_traceback=None):
        """Exit the span context. Zipkin attrs are pushed onto the
        threadlocal stack regardless of sampling, so they always need to be
        popped off. The actual logging of spans depends on sampling and that
        the logging was correctly set up.
        """

        if self.do_pop_attrs:
            self.get_tracer().pop_zipkin_attrs()

        # If no transport is configured, there's no reason to create a new Span.
        # This also helps avoiding memory leaks since without a transport nothing
        # would pull spans out of get_tracer().
        if not self.get_tracer().is_transport_configured():
            return

        # Add the error annotation if an exception occurred
        if any((_exc_type, _exc_value, _exc_traceback)):
            error_msg = u'{0}: {1}'.format(_exc_type.__name__, _exc_value)
            self.update_binary_annotations({
                ERROR_KEY: error_msg,
            })

        # Logging context is only initialized for "root" spans of the local
        # process (i.e. this zipkin_span not inside of any other local
        # zipkin_spans)
        if self.logging_context:
            self.logging_context.stop()
            self.logging_context = None
            self.get_tracer().set_transport_configured(configured=False)
            return

        # If we've gotten here, that means that this span is a child span of
        # this context's root span (i.e. it's a zipkin_span inside another
        # zipkin_span).
        end_timestamp = time.time()
        # If self.duration is set, it means the user wants to override it
        if self.duration:
            duration = self.duration
        else:
            duration = end_timestamp - self.start_timestamp

        endpoint = create_endpoint(self.port, self.service_name, self.host)
        self.get_tracer().add_span(
            Span(
                trace_id=self.zipkin_attrs.trace_id,
                name=self.span_name,
                parent_id=self.zipkin_attrs.parent_span_id,
                span_id=self.zipkin_attrs.span_id,
                kind=self.kind,
                timestamp=self.timestamp
                if self.timestamp else self.start_timestamp,
                duration=duration,
                annotations=self.annotations,
                local_endpoint=endpoint,
                remote_endpoint=self.remote_endpoint,
                tags=self.binary_annotations,
            ))
    def test_create_json_endpoint(self, encoder):
        ipv4_endpoint = create_endpoint(
            port=8888,
            service_name='test_service',
            host='127.0.0.1',
        )
        assert encoder._create_json_endpoint(ipv4_endpoint, False) == {
            'serviceName': 'test_service',
            'port': 8888,
            'ipv4': '127.0.0.1',
        }

        ipv6_endpoint = create_endpoint(
            port=8888,
            service_name='test_service',
            host='2001:0db8:85a3:0000:0000:8a2e:0370:7334',
        )
        assert encoder._create_json_endpoint(ipv6_endpoint, False) == {
            'serviceName': 'test_service',
            'port': 8888,
            'ipv6': '2001:0db8:85a3:0000:0000:8a2e:0370:7334',
        }

        v1_endpoint = create_endpoint(
            port=8888,
            service_name=None,
            host='2001:0db8:85a3:0000:0000:8a2e:0370:7334',
            use_defaults=False,
        )
        assert encoder._create_json_endpoint(v1_endpoint, True) == {
            'serviceName': '',
            'port': 8888,
            'ipv6': '2001:0db8:85a3:0000:0000:8a2e:0370:7334',
        }

        v2_endpoint = create_endpoint(
            port=8888,
            service_name=None,
            host='2001:0db8:85a3:0000:0000:8a2e:0370:7334',
            use_defaults=False,
        )
        assert encoder._create_json_endpoint(v2_endpoint, False) == {
            'port': 8888,
            'ipv6': '2001:0db8:85a3:0000:0000:8a2e:0370:7334',
        }
Beispiel #19
0
def test_malformed_host():
    endpoint = create_endpoint(
        port=8080,
        service_name='foo',
        host='80',
    )
    assert endpoint.service_name == 'foo'
    assert endpoint.port == 8080
    assert endpoint.ipv4 is None
    assert endpoint.ipv6 is None
Beispiel #20
0
    def test_create_json_endpoint(self, encoder):
        ipv4_endpoint = create_endpoint(
            port=8888, service_name="test_service", host="127.0.0.1",
        )
        assert encoder._create_json_endpoint(ipv4_endpoint, False) == {
            "serviceName": "test_service",
            "port": 8888,
            "ipv4": "127.0.0.1",
        }

        ipv6_endpoint = create_endpoint(
            port=8888,
            service_name="test_service",
            host="2001:0db8:85a3:0000:0000:8a2e:0370:7334",
        )
        assert encoder._create_json_endpoint(ipv6_endpoint, False) == {
            "serviceName": "test_service",
            "port": 8888,
            "ipv6": "2001:0db8:85a3:0000:0000:8a2e:0370:7334",
        }

        v1_endpoint = create_endpoint(
            port=8888,
            service_name=None,
            host="2001:0db8:85a3:0000:0000:8a2e:0370:7334",
            use_defaults=False,
        )
        assert encoder._create_json_endpoint(v1_endpoint, True) == {
            "serviceName": "",
            "port": 8888,
            "ipv6": "2001:0db8:85a3:0000:0000:8a2e:0370:7334",
        }

        v2_endpoint = create_endpoint(
            port=8888,
            service_name=None,
            host="2001:0db8:85a3:0000:0000:8a2e:0370:7334",
            use_defaults=False,
        )
        assert encoder._create_json_endpoint(v2_endpoint, False) == {
            "port": 8888,
            "ipv6": "2001:0db8:85a3:0000:0000:8a2e:0370:7334",
        }
Beispiel #21
0
    def test_add_sa_binary_annotation_non_root(self):
        # Nothing happens if this is not a client span
        with zipkin.zipkin_client_span('test_service', 'test_span') as span:

            span.add_sa_binary_annotation(80, 'remote_service', '10.1.2.3')

            expected_endpoint = create_endpoint(80, 'remote_service', '10.1.2.3')
            assert span.remote_endpoint == expected_endpoint

            # setting it again throws an error
            with pytest.raises(ValueError):
                span.add_sa_binary_annotation(80, 'remote_service', '10.1.2.3')
Beispiel #22
0
def context():
    attr = ZipkinAttrs(None, None, None, None, False)
    return logging_helper.ZipkinLoggingContext(
        zipkin_attrs=attr,
        endpoint=create_endpoint(80, 'test_server', '127.0.0.1'),
        span_name='span_name',
        transport_handler=MockTransportHandler(),
        report_root_timestamp=False,
        span_storage=SpanStorage(),
        service_name='test_server',
        encoding=Encoding.V1_JSON,
    )
Beispiel #23
0
    def test_v1_span(self, kind, annotations):
        endpoint = create_endpoint(port=8080, service_name="foo", host="80")
        span = Span(
            trace_id=generate_random_64bit_string(),
            name="test span",
            parent_id=generate_random_64bit_string(),
            span_id=generate_random_64bit_string(),
            kind=kind,
            timestamp=26.0,
            duration=4.0,
            local_endpoint=endpoint,
        )
        v1_span = span.build_v1_span()

        assert v1_span.annotations == annotations
Beispiel #24
0
    def test_add_sa_binary_annotation_root(self):
        # Nothing happens if this is not a client span
        with zipkin.zipkin_client_span(
            service_name='test_service',
            span_name='test_span',
            transport_handler=MockTransportHandler(),
            sample_rate=100.0,
        ) as span:

            span.add_sa_binary_annotation(80, 'remote_service', '10.1.2.3')

            expected_endpoint = create_endpoint(80, 'remote_service', '10.1.2.3')
            assert span.logging_context.remote_endpoint == expected_endpoint

            # setting it again throws an error
            with pytest.raises(ValueError):
                span.add_sa_binary_annotation(80, 'remote_service', '10.1.2.3')
Beispiel #25
0
    def add_sa_binary_annotation(
        self,
        port=0,
        service_name='unknown',
        host='127.0.0.1',
    ):
        """Adds a 'sa' binary annotation to the current span.

        'sa' binary annotations are useful for situations where you need to log
        where a request is going but the destination doesn't support zipkin.

        Note that the span must have 'cs'/'cr' annotations.

        :param port: The port number of the destination
        :type port: int
        :param service_name: The name of the destination service
        :type service_name: str
        :param host: Host address of the destination
        :type host: str
        """
        if not self.zipkin_attrs:
            return

        if self.kind != Kind.CLIENT:
            # TODO: trying to set a sa binary annotation for a non-client span
            # should result in a logged error
            return

        sa_endpoint = create_endpoint(
            port=port,
            service_name=service_name,
            host=host,
        )
        if not self.logging_context:
            if self.sa_endpoint is not None:
                raise ValueError('SA annotation already set.')
            self.sa_endpoint = sa_endpoint
        else:
            if self.logging_context.sa_endpoint is not None:
                raise ValueError('SA annotation already set.')
            self.logging_context.sa_endpoint = sa_endpoint
Beispiel #26
0
def test_zipkin_logging_context(time_mock):
    # Tests the context manager aspects of the ZipkinLoggingContext
    time_mock.return_value = 42
    attr = ZipkinAttrs(None, None, None, None, False)
    tracer = MockTracer()
    context = logging_helper.ZipkinLoggingContext(
        zipkin_attrs=attr,
        endpoint=create_endpoint(80, "test_server", "127.0.0.1"),
        span_name="span_name",
        transport_handler=MockTransportHandler(),
        report_root_timestamp=False,
        get_tracer=lambda: tracer,
        service_name="test_server",
        encoding=Encoding.V1_JSON,
    )
    # Ignore the actual logging part
    with mock.patch.object(context, "emit_spans"):
        context.start()
        assert context.start_timestamp == 42
        context.stop()
        # Make sure the handler and the zipkin attrs are gone
        assert context.emit_spans.call_count == 1
    def test_remote_endpoint(self):
        encoder = get_encoder(Encoding.V1_JSON)
        remote_endpoint = create_endpoint(
            service_name='test_server',
            host='127.0.0.1',
        )

        # For server spans, the remote endpoint is encoded as 'ca'
        binary_annotations = []
        encoder.encode_remote_endpoint(
            remote_endpoint,
            Kind.SERVER,
            binary_annotations,
        )
        assert binary_annotations == [{
            'endpoint': {
                'ipv4': '127.0.0.1',
                'serviceName': 'test_server'
            },
            'key': 'ca',
            'value': True,
        }]

        # For client spans, the remote endpoint is encoded as 'sa'
        binary_annotations = []
        encoder.encode_remote_endpoint(
            remote_endpoint,
            Kind.CLIENT,
            binary_annotations,
        )
        assert binary_annotations == [{
            'endpoint': {
                'ipv4': '127.0.0.1',
                'serviceName': 'test_server'
            },
            'key': 'sa',
            'value': True,
        }]
Beispiel #28
0
def test_batch_sender_add_span_not_called_if_not_sampled(add_span_mock, flush_mock):
    attr = ZipkinAttrs(
        trace_id="0000000000000001",
        span_id="0000000000000002",
        parent_span_id=None,
        flags=None,
        is_sampled=False,
    )
    tracer = MockTracer()
    transport_handler = mock.Mock()

    context = logging_helper.ZipkinLoggingContext(
        zipkin_attrs=attr,
        endpoint=create_endpoint(80, "test_server", "127.0.0.1"),
        span_name="span_name",
        transport_handler=transport_handler,
        report_root_timestamp=False,
        get_tracer=lambda: tracer,
        service_name="test_server",
        encoding=Encoding.V1_JSON,
    )
    context.emit_spans()
    assert add_span_mock.call_count == 0
    assert flush_mock.call_count == 0
Beispiel #29
0
def test_adding_sa_binary_annotation_without_sampling():
    """Even if we're not sampling, we still want to add binary annotations
    since they're useful for firehose traces.
    """
    context = zipkin.zipkin_span(
        service_name='my_service',
        span_name='span_name',
        transport_handler=MockTransportHandler(),
        sample_rate=0.0,
        kind=Kind.CLIENT,
    )
    with context:
        context.add_sa_binary_annotation(
            port=123,
            service_name='test_service',
            host='1.2.3.4',
        )
        expected_sa_endpoint = create_endpoint(
            port=123,
            service_name='test_service',
            host='1.2.3.4',
        )

        assert context.sa_endpoint == expected_sa_endpoint
Beispiel #30
0
def test_zipkin_logging_server_context_emit_spans(
    add_span_mock, flush_mock, time_mock, fake_endpoint
):
    # This lengthy function tests that the logging context properly
    # logs both client and server spans.
    trace_id = "000000000000000f"
    parent_span_id = "0000000000000001"
    server_span_id = "0000000000000002"
    client_span_id = "0000000000000003"
    client_span_name = "breadcrumbs"
    attr = ZipkinAttrs(
        trace_id=trace_id,
        span_id=server_span_id,
        parent_span_id=parent_span_id,
        flags=None,
        is_sampled=True,
    )
    tracer = MockTracer()

    client_span = Span(
        trace_id=trace_id,
        name=client_span_name,
        parent_id=server_span_id,
        span_id=client_span_id,
        kind=Kind.CLIENT,
        timestamp=26.0,
        duration=4.0,
        local_endpoint=create_endpoint(service_name="test_server"),
        annotations={"ann2": 2, "cs": 26, "cr": 30},
        tags={"bann2": "yiss"},
    )
    tracer.get_spans().append(client_span)

    transport_handler = mock.Mock()

    context = logging_helper.ZipkinLoggingContext(
        zipkin_attrs=attr,
        endpoint=fake_endpoint,
        span_name="GET /foo",
        transport_handler=transport_handler,
        report_root_timestamp=True,
        get_tracer=lambda: tracer,
        service_name="test_server",
        encoding=Encoding.V1_JSON,
    )

    context.start_timestamp = 24
    context.response_status_code = 200

    context.tags = {"k": "v"}
    time_mock.return_value = 42

    context.emit_spans()
    client_log_call, server_log_call = add_span_mock.call_args_list
    assert (
        server_log_call[0][1].build_v1_span()
        == Span(
            trace_id=trace_id,
            name="GET /foo",
            parent_id=parent_span_id,
            span_id=server_span_id,
            kind=Kind.SERVER,
            timestamp=24.0,
            duration=18.0,
            local_endpoint=fake_endpoint,
            annotations={"sr": 24, "ss": 42},
            tags={"k": "v"},
        ).build_v1_span()
    )
    assert client_log_call[0][1] == client_span
    assert flush_mock.call_count == 1