def _translate_to_datadog(self, spans):
        datadog_spans = []

        for span in spans:
            trace_id, parent_id, span_id = _get_trace_ids(span)

            # datadog Span is initialized with a reference to the tracer which is
            # used to record the span when it is finished. We can skip ignore this
            # because we are not calling the finish method and explictly set the
            # duration.
            tracer = None

            datadog_span = DatadogSpan(
                tracer,
                _get_span_name(span),
                service=self.service,
                resource=_get_resource(span),
                span_type=_get_span_type(span),
                trace_id=trace_id,
                span_id=span_id,
                parent_id=parent_id,
            )
            datadog_span.start_ns = span.start_time
            datadog_span.duration_ns = span.end_time - span.start_time

            if span.status.canonical_code is not StatusCanonicalCode.OK:
                datadog_span.error = 1
                if span.status.description:
                    exc_type, exc_val = _get_exc_info(span)
                    # no mapping for error.stack since traceback not recorded
                    datadog_span.set_tag("error.msg", exc_val)
                    datadog_span.set_tag("error.type", exc_type)

            datadog_span.set_tags(span.attributes)

            # add configured env tag
            if self.env is not None:
                datadog_span.set_tag(ENV_KEY, self.env)

            # add configured application version tag to only root span
            if self.version is not None and parent_id == 0:
                datadog_span.set_tag(VERSION_KEY, self.version)

            # add configured global tags
            datadog_span.set_tags(self.tags)

            # add origin to root span
            origin = _get_origin(span)
            if origin and parent_id == 0:
                datadog_span.set_tag(DD_ORIGIN, origin)

            sampling_rate = _get_sampling_rate(span)
            if sampling_rate is not None:
                datadog_span.set_metric(SAMPLE_RATE_METRIC_KEY, sampling_rate)

            # span events and span links are not supported

            datadog_spans.append(datadog_span)

        return datadog_spans
Esempio n. 2
0
    def test_span_boolean_err(self):
        s = Span(tracer=None, name="foo.bar", service="s", resource="r")
        s.error = True
        s.finish()

        d = s.to_dict()
        assert d
        assert d["error"] == 1
        assert type(d["error"]) == int
Esempio n. 3
0
    def test_span_boolean_err(self):
        s = Span(tracer=None, name='foo.bar', service='s', resource='r')
        s.error = True
        s.finish()

        d = s.to_dict()
        assert d
        assert d['error'] == 1
        assert type(d['error']) == int
Esempio n. 4
0
def test_span_boolean_err():
    s = Span(tracer=None, name="foo.bar", service="s",  resource="r")
    s.error = True
    s.finish()

    d = s.to_dict()
    assert d
    eq_(d["error"], 1)
    eq_(type(d["error"]), int)
Esempio n. 5
0
def test_span_boolean_err():
    s = Span(tracer=None, name='foo.bar', service='s', resource='r')
    s.error = True
    s.finish()

    d = s.to_dict()
    assert d
    eq_(d['error'], 1)
    eq_(type(d['error']), int)
Esempio n. 6
0
def test_span_boolean_err():
    s = Span(tracer=None, name="foo.bar", service="s", resource="r")
    s.error = True
    s.finish()

    d = s.to_dict()
    assert d
    eq_(d["error"], 1)
    eq_(type(d["error"]), int)
Esempio n. 7
0
def test_span_pprint():
    root = Span(None,
                "test.span",
                service="s",
                resource="r",
                span_type=SpanTypes.WEB)
    root.set_tag("t", "v")
    root.set_metric("m", 1.0)
    root.finish()
    actual = root.pprint()
    assert "name='test.span'" in actual
    assert "service='s'" in actual
    assert "resource='r'" in actual
    assert "type='web'" in actual
    assert "error=0" in actual
    assert ("tags={'t': 'v'}" if six.PY3 else "tags={'t': u'v'}") in actual
    assert "metrics={'m': 1.0}" in actual
    assert re.search("id=[0-9]+", actual) is not None
    assert re.search("trace_id=[0-9]+", actual) is not None
    assert "parent_id=None" in actual
    assert re.search("duration=[0-9.]+", actual) is not None
    assert re.search("start=[0-9.]+", actual) is not None
    assert re.search("end=[0-9.]+", actual) is not None

    root = Span(None,
                "test.span",
                service="s",
                resource="r",
                span_type=SpanTypes.WEB)
    actual = root.pprint()
    assert "duration=None" in actual
    assert "end=None" in actual

    root = Span(None,
                "test.span",
                service="s",
                resource="r",
                span_type=SpanTypes.WEB)
    root.error = 1
    actual = root.pprint()
    assert "error=1" in actual

    root = Span(None,
                "test.span",
                service="s",
                resource="r",
                span_type=SpanTypes.WEB)
    root.set_tag(u"😌", u"😌")
    actual = root.pprint()
    assert (u"tags={'😌': '😌'}"
            if six.PY3 else "tags={u'\\U0001f60c': u'\\U0001f60c'}") in actual

    root = Span(None, "test.span", service=object())
    actual = root.pprint()
    assert "service=<object object at" in actual
Esempio n. 8
0
def test_custom_msgpack_encode_trace_size(encoding, name, service, resource, meta, metrics, error, span_type):
    encoder = MSGPACK_ENCODERS[encoding](1 << 20, 1 << 20)
    span = Span(tracer=None, name=name, service=service, resource=resource)
    span.meta = meta
    span.metrics = metrics
    span.error = error
    span.span_type = span_type
    trace = [span, span, span]

    encoder.put(trace)
    assert encoder.size == len(encoder.encode())
Esempio n. 9
0
    def _translate_to_datadog(self, spans):
        datadog_spans = []

        for span in spans:
            trace_id, parent_id, span_id = _get_trace_ids(span)

            # datadog Span is initialized with a reference to the tracer which is
            # used to record the span when it is finished. We can skip ignore this
            # because we are not calling the finish method and explictly set the
            # duration.
            tracer = None

            datadog_span = DatadogSpan(
                tracer,
                _get_span_name(span),
                service=self.service,
                resource=_get_resource(span),
                span_type=_get_span_type(span),
                trace_id=trace_id,
                span_id=span_id,
                parent_id=parent_id,
            )
            datadog_span.start_ns = span.start_time
            datadog_span.duration_ns = span.end_time - span.start_time

            if span.status.canonical_code is not StatusCanonicalCode.OK:
                datadog_span.error = 1
                if span.status.description:
                    exc_type, exc_val = _get_exc_info(span)
                    # no mapping for error.stack since traceback not recorded
                    datadog_span.set_tag("error.msg", exc_val)
                    datadog_span.set_tag("error.type", exc_type)

            datadog_span.set_tags(span.attributes)

            # span events and span links are not supported

            datadog_spans.append(datadog_span)

        return datadog_spans
    def _translate_to_datadog(self, spans):
        datadog_spans = []

        for span in spans:
            trace_id, parent_id, span_id = _get_trace_ids(span)

            # datadog Span is initialized with a reference to the tracer which is
            # used to record the span when it is finished. We can skip ignore this
            # because we are not calling the finish method and explictly set the
            # duration.
            tracer = None

            # extract resource attributes to be used as tags as well as potential service name
            [
                resource_tags,
                resource_service_name,
            ] = _extract_tags_from_resource(span.resource)

            datadog_span = DatadogSpan(
                tracer,
                _get_span_name(span),
                service=resource_service_name or self.service,
                resource=_get_resource(span),
                span_type=_get_span_type(span),
                trace_id=trace_id,
                span_id=span_id,
                parent_id=parent_id,
            )
            datadog_span.start_ns = span.start_time
            datadog_span.duration_ns = span.end_time - span.start_time

            if not span.status.is_ok:
                datadog_span.error = 1
                # loop over events and look for exception events, extract info.
                # https://github.com/open-telemetry/opentelemetry-python/blob/71e3a7a192c0fc8a7503fac967ada36a74b79e58/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py#L810-L819
                if span.events:
                    _extract_tags_from_exception_events(
                        span.events, datadog_span
                    )

            # combine resource attributes and span attributes, don't modify existing span attributes
            combined_span_tags = {}
            combined_span_tags.update(resource_tags)
            combined_span_tags.update(span.attributes)

            datadog_span.set_tags(combined_span_tags)

            # add configured env tag
            if self.env is not None:
                datadog_span.set_tag(ENV_KEY, self.env)

            # add configured application version tag to only root span
            if self.version is not None and parent_id == 0:
                datadog_span.set_tag(VERSION_KEY, self.version)

            # add configured global tags
            datadog_span.set_tags(self.tags)

            # add origin to root span
            origin = _get_origin(span)
            if origin and parent_id == 0:
                datadog_span.set_tag(DD_ORIGIN, origin)

            sampling_rate = _get_sampling_rate(span)
            if sampling_rate is not None:
                datadog_span.set_metric(SAMPLE_RATE_METRIC_KEY, sampling_rate)

            # span events and span links are not supported except for extracting exception event context

            datadog_spans.append(datadog_span)

        return datadog_spans