Esempio n. 1
0
def test_zipkin_server_span():
    context = zipkin.zipkin_server_span('test_service', 'test_span')

    assert context.kind == Kind.SERVER

    with pytest.raises(ValueError):
        zipkin.zipkin_server_span('test_service', 'test_span', kind=Kind.LOCAL)
Esempio n. 2
0
    def _start_call_span(self, app, sender, task_id):
        # type: (celery.app.Celery, str, str) -> py_zipkin.zipkin.server_span

        zipkin_attrs = zipkin.get_zipkin_attrs(
        )  # type: py_zipkin.zipkin.ZipkinAttrs
        """:type: py_zipkin.zipkin.ZipkinAttrs"""

        if zipkin_attrs:
            zipkin_attrs = zipkin.ZipkinAttrs(
                trace_id=zipkin_attrs.trace_id,
                span_id=zipkin.generate_random_64bit_string(),
                parent_span_id=zipkin_attrs.span_id,
                flags=zipkin_attrs.flags,
                is_sampled=zipkin_attrs.is_sampled,
            )

        else:
            zipkin_attrs = zipkin.create_attrs_for_span(self.sample_rate)

        # Get task definition to see if task ignores result
        task = get_task_from_app(app, sender)

        # Is Call Complete at this point.
        if task.ignore_result or not self._has_result_event:
            call_type = 'async'
            backend = None

        else:
            call_type = 'call'
            backend = get_task_backend_name(task, is_eager)

        span = zipkin.zipkin_server_span(
            service_name='celery.{}'.format(sender.split('.', 1)[0]),
            span_name='task.{}:{}'.format(call_type, sender),
            transport_handler=self.transport_handler,
            sample_rate=self.sample_rate,
            zipkin_attrs=zipkin_attrs,
        )

        print span.service_name
        print span.span_name
        print span.zipkin_attrs

        span.start()
        span.update_binary_annotations({
            'celery.task.name': sender,
            'celery.task.type': call_type,
            'celery.task.backend': backend,
            'celery.machine': self._hostname,
            'celery.platform': self._platform,
        })

        return span
Esempio n. 3
0
 def get_dependency(self, worker_ctx):
     config = self.container.config[ZIPKIN_CONFIG_SECTION]
     zipkin_attrs = _read_zipkin_attrs(worker_ctx)
     logger.debug('get_dependency zipkin attrs: {}'.format(zipkin_attrs))
     span = zipkin.zipkin_server_span(worker_ctx.service_name,
                                      worker_ctx.entrypoint.method_name,
                                      zipkin_attrs=zipkin_attrs,
                                      transport_handler=self.transport.handle,
                                      sample_rate=config.get('SAMPLE_RATE', 10.0))
     logger.debug('tracing {}.{}'.format(worker_ctx.service_name, worker_ctx.entrypoint.method_name))
     self.spans[worker_ctx.call_id] = span
     return span
Esempio n. 4
0
    def get_dependency(self, worker_ctx):
        zipkin_attrs = _read_zipkin_attrs(worker_ctx)

        # if there are no zipking attrs in context data, request isn't traced
        # TODO: support trace initialization in such condition
        if not zipkin_attrs:
            return None
        span = zipkin.zipkin_server_span(
            worker_ctx.service_name,
            worker_ctx.entrypoint.method_name,
            zipkin_attrs=zipkin_attrs,
            transport_handler=self.transport.handle)
        self.spans[worker_ctx.call_id] = span
        return span
Esempio n. 5
0
def test_server_span(encoding):
    mock_transport_handler, mock_logs = mock_logger()
    with zipkin.zipkin_server_span(
            service_name="test_service_name",
            span_name="test_span_name",
            transport_handler=mock_transport_handler,
            sample_rate=100.0,
            encoding=encoding,
    ):
        with zipkin.zipkin_client_span(
                service_name="nested_service",
                span_name="nested_span",
        ):
            pass

        pass

    assert len(mock_logs) == 1
    spans = json.loads(mock_logs[0])
    client_span = spans[0]
    server_span = spans[1]

    assert server_span["name"] == "test_span_name"
    # Local nested spans report timestamp and duration
    assert "timestamp" in server_span
    assert "duration" in server_span
    if encoding == Encoding.V1_JSON:
        assert len(server_span["annotations"]) == 2
        assert sorted([ann["value"]
                       for ann in server_span["annotations"]]) == [
                           "sr",
                           "ss",
                       ]
    elif encoding == Encoding.V2_JSON:
        assert server_span["kind"] == "SERVER"

    assert client_span["name"] == "nested_span"
    # Local nested spans report timestamp and duration
    assert "timestamp" in client_span
    assert "duration" in client_span
    if encoding == Encoding.V1_JSON:
        assert len(client_span["annotations"]) == 2
        assert sorted([ann["value"]
                       for ann in client_span["annotations"]]) == [
                           "cr",
                           "cs",
                       ]
    elif encoding == Encoding.V2_JSON:
        assert client_span["kind"] == "CLIENT"
Esempio n. 6
0
def test_server_span(encoding):
    mock_transport_handler, mock_logs = mock_logger()
    with zipkin.zipkin_server_span(
            service_name='test_service_name',
            span_name='test_span_name',
            transport_handler=mock_transport_handler,
            sample_rate=100.0,
            encoding=encoding,
    ):
        with zipkin.zipkin_client_span(
                service_name='nested_service',
                span_name='nested_span',
        ):
            pass

        pass

    assert len(mock_logs) == 1
    spans = json.loads(mock_logs[0])
    client_span = spans[0]
    server_span = spans[1]

    assert server_span['name'] == 'test_span_name'
    # Local nested spans report timestamp and duration
    assert 'timestamp' in server_span
    assert 'duration' in server_span
    if encoding == Encoding.V1_JSON:
        assert len(server_span['annotations']) == 2
        assert sorted([ann['value']
                       for ann in server_span['annotations']]) == ['sr', 'ss']
    elif encoding == Encoding.V2_JSON:
        assert server_span['kind'] == 'SERVER'

    assert client_span['name'] == 'nested_span'
    # Local nested spans report timestamp and duration
    assert 'timestamp' in client_span
    assert 'duration' in client_span
    if encoding == Encoding.V1_JSON:
        assert len(client_span['annotations']) == 2
        assert sorted([ann['value']
                       for ann in client_span['annotations']]) == ['cr', 'cs']
    elif encoding == Encoding.V2_JSON:
        assert client_span['kind'] == 'CLIENT'
Esempio n. 7
0
        def wrapper(*args, **kwargs):
            try:
                service_name = args[0].name
            except AttributeError:
                service_name = "unknown"

            if not debug:
                return func(*args, **kwargs)
            if hasattr(args[0], "zipkin_nameko") and not args[0].zipkin_nameko:
                handler = HttpTransport(url).handle
                monkey_patch(handler)
                with zipkin.zipkin_server_span(
                        service_name,
                        span_name,
                        sample_rate=100.0,
                        transport_handler=handler,
                ):
                    return func(*args, **kwargs)
            else:
                return func(*args, **kwargs)
Esempio n. 8
0
    def _start_taskrun_span(self,
                            sender,
                            task,
                            span_id=None,
                            trace_id=None,
                            parent_id=None,
                            flags=None,
                            is_sampled=None):
        # type: (str, str, str, str, str, str) -> py_zipkin.zipkin.server_span

        zipkin_attrs = zipkin.get_zipkin_attrs(
        )  # type: py_zipkin.zipkin.ZipkinAttrs
        """:type: py_zipkin.zipkin.ZipkinAttrs"""

        if not zipkin_attrs:
            zipkin_attrs = zipkin.create_attrs_for_span(self.sample_rate)
        zipkin_attrs = zipkin.ZipkinAttrs(
            trace_id=trace_id or zipkin_attrs.trace_id,
            span_id=span_id or zipkin.generate_random_64bit_string(),
            parent_span_id=parent_id or zipkin_attrs.span_id,
            flags=flags or zipkin_attrs.flags,
            is_sampled=zipkin_attrs.is_sampled
            if is_sampled is None else is_sampled,
        )

        span = zipkin.zipkin_server_span(
            service_name='celery.{}'.format(sender.split('.', 1)[0]),
            span_name='task.run:{}'.format(sender),
            transport_handler=self.transport_handler,
            sample_rate=self.sample_rate,
            zipkin_attrs=zipkin_attrs,
        )

        backend = None
        context = task.request
        is_eager = context.get('is_eager', False)
        if task.ignore_result or not self._has_result_event:
            call_type = 'async'
        else:
            call_type = 'call'
            if is_eager:
                backend = 'is_eager'

            else:
                backend = get_task_backend_name(task, is_eager)

        span.start()
        span.update_binary_annotations({
            'celery.task.name':
            sender,
            'celery.task.type':
            call_type,
            'celery.task.backend':
            backend,
            'celery.machine':
            context.get('hostname', self._hostname),
            'celery.is_eager':
            context.get('is_eager', False),
            'celery.platform':
            self._platform,
        })

        return span