Ejemplo n.º 1
0
    def _translate_data(
        self, data: Sequence[SDKSpan]
    ) -> ExportTraceServiceRequest:
        # pylint: disable=attribute-defined-outside-init

        sdk_resource_instrumentation_library_spans = {}

        for sdk_span in data:

            if sdk_span.resource not in (
                sdk_resource_instrumentation_library_spans.keys()
            ):
                if sdk_span.instrumentation_info is not None:
                    instrumentation_library_spans = InstrumentationLibrarySpans(
                        instrumentation_library=InstrumentationLibrary(
                            name=sdk_span.instrumentation_info.name,
                            version=sdk_span.instrumentation_info.version,
                        )
                    )

                else:
                    instrumentation_library_spans = (
                        InstrumentationLibrarySpans()
                    )

                sdk_resource_instrumentation_library_spans[
                    sdk_span.resource
                ] = instrumentation_library_spans

            self._collector_span_kwargs = {}

            self._translate_name(sdk_span)
            self._translate_start_time(sdk_span)
            self._translate_end_time(sdk_span)
            self._translate_span_id(sdk_span)
            self._translate_trace_id(sdk_span)
            self._translate_parent(sdk_span)
            self._translate_context_trace_state(sdk_span)
            self._translate_attributes(sdk_span)
            self._translate_events(sdk_span)
            self._translate_links(sdk_span)
            self._translate_status(sdk_span)

            self._collector_span_kwargs["kind"] = getattr(
                CollectorSpan.SpanKind,
                "SPAN_KIND_{}".format(sdk_span.kind.name),
            )

            sdk_resource_instrumentation_library_spans[
                sdk_span.resource
            ].spans.append(CollectorSpan(**self._collector_span_kwargs))

        return ExportTraceServiceRequest(
            resource_spans=_get_resource_data(
                sdk_resource_instrumentation_library_spans,
                ResourceSpans,
                "spans",
            )
        )
Ejemplo n.º 2
0
    def _translate_data(self, data) -> ExportTraceServiceRequest:

        sdk_resource_instrumentation_library_spans = {}

        for sdk_span in data:

            if sdk_span.resource not in (
                    sdk_resource_instrumentation_library_spans.keys()):
                sdk_resource_instrumentation_library_spans[
                    sdk_span.resource] = InstrumentationLibrarySpans()

            self._collector_span_kwargs = {}

            self._translate_name(sdk_span)
            self._translate_start_time(sdk_span)
            self._translate_end_time(sdk_span)
            self._translate_span_id(sdk_span)
            self._translate_trace_id(sdk_span)
            self._translate_parent(sdk_span)
            self._translate_context_trace_state(sdk_span)
            self._translate_attributes(sdk_span)
            self._translate_events(sdk_span)
            self._translate_links(sdk_span)
            self._translate_status(sdk_span)

            self._collector_span_kwargs["kind"] = getattr(
                CollectorSpan.SpanKind, sdk_span.kind.name)

            sdk_resource_instrumentation_library_spans[
                sdk_span.resource].spans.append(
                    CollectorSpan(**self._collector_span_kwargs))

        return ExportTraceServiceRequest(resource_spans=_get_resource_data(
            sdk_resource_instrumentation_library_spans,
            ResourceSpans,
            "spans",
        ))
    def test_translate_spans(self):

        expected = ExportTraceServiceRequest(
            resource_spans=[
                ResourceSpans(
                    resource=CollectorResource(
                        attributes=[
                            AttributeKeyValue(key="a", int_value=1),
                            AttributeKeyValue(key="b", bool_value=False),
                        ]
                    ),
                    instrumentation_library_spans=[
                        InstrumentationLibrarySpans(
                            spans=[
                                CollectorSpan(
                                    # pylint: disable=no-member
                                    name="a",
                                    start_time_unix_nano=self.span.start_time,
                                    end_time_unix_nano=self.span.end_time,
                                    trace_state="a=b,c=d",
                                    span_id=int.to_bytes(
                                        10217189687419569865, 8, "big"
                                    ),
                                    trace_id=int.to_bytes(
                                        67545097771067222548457157018666467027,
                                        16,
                                        "big",
                                    ),
                                    parent_span_id=(
                                        b"\000\000\000\000\000\00009"
                                    ),
                                    kind=CollectorSpan.SpanKind.INTERNAL,
                                    attributes=[
                                        AttributeKeyValue(
                                            key="a", int_value=1
                                        ),
                                        AttributeKeyValue(
                                            key="b", bool_value=True
                                        ),
                                    ],
                                    events=[
                                        CollectorSpan.Event(
                                            name="a",
                                            time_unix_nano=1591240820506462784,
                                            attributes=[
                                                AttributeKeyValue(
                                                    key="a", int_value=1
                                                ),
                                                AttributeKeyValue(
                                                    key="b", int_value=False
                                                ),
                                            ],
                                        )
                                    ],
                                    status=Status(code=0, message=""),
                                    links=[
                                        CollectorSpan.Link(
                                            trace_id=int.to_bytes(
                                                1, 16, "big"
                                            ),
                                            span_id=int.to_bytes(2, 8, "big"),
                                            attributes=[
                                                AttributeKeyValue(
                                                    key="a", int_value=1
                                                ),
                                                AttributeKeyValue(
                                                    key="b", bool_value=False
                                                ),
                                            ],
                                        )
                                    ],
                                )
                            ]
                        )
                    ],
                ),
            ]
        )

        # pylint: disable=protected-access
        self.assertEqual(expected, self.exporter._translate_spans([self.span]))
Ejemplo n.º 4
0
    def _translate_data(
            self, data: Sequence[ReadableSpan]) -> ExportTraceServiceRequest:
        # pylint: disable=attribute-defined-outside-init

        sdk_resource_instrumentation_library_spans = {}

        for sdk_span in data:
            instrumentation_library_spans_map = (
                sdk_resource_instrumentation_library_spans.get(
                    sdk_span.resource, {}))
            # If we haven't seen the Resource yet, add it to the map
            if not instrumentation_library_spans_map:
                sdk_resource_instrumentation_library_spans[
                    sdk_span.resource] = instrumentation_library_spans_map
            instrumentation_library_spans = (
                instrumentation_library_spans_map.get(
                    sdk_span.instrumentation_info))
            # If we haven't seen the InstrumentationInfo for this Resource yet, add it to the map
            if not instrumentation_library_spans:
                if sdk_span.instrumentation_info is not None:
                    instrumentation_library_spans_map[
                        sdk_span.
                        instrumentation_info] = InstrumentationLibrarySpans(
                            instrumentation_library=InstrumentationLibrary(
                                name=sdk_span.instrumentation_info.name,
                                version=sdk_span.instrumentation_info.version,
                            ))
                else:
                    # If no InstrumentationInfo, store in None key
                    instrumentation_library_spans_map[
                        sdk_span.
                        instrumentation_info] = InstrumentationLibrarySpans()
            instrumentation_library_spans = (
                instrumentation_library_spans_map.get(
                    sdk_span.instrumentation_info))
            self._collector_kwargs = {}

            self._translate_name(sdk_span)
            self._translate_start_time(sdk_span)
            self._translate_end_time(sdk_span)
            self._translate_span_id(sdk_span)
            self._translate_trace_id(sdk_span)
            self._translate_parent(sdk_span)
            self._translate_context_trace_state(sdk_span)
            self._collector_kwargs["attributes"] = self._translate_attributes(
                sdk_span.attributes)
            self._translate_events(sdk_span)
            self._translate_links(sdk_span)
            self._translate_status(sdk_span)
            if sdk_span.dropped_attributes:
                self._collector_kwargs[
                    "dropped_attributes_count"] = sdk_span.dropped_attributes
            if sdk_span.dropped_events:
                self._collector_kwargs[
                    "dropped_events_count"] = sdk_span.dropped_events
            if sdk_span.dropped_links:
                self._collector_kwargs[
                    "dropped_links_count"] = sdk_span.dropped_links

            self._collector_kwargs["kind"] = getattr(
                CollectorSpan.SpanKind,
                f"SPAN_KIND_{sdk_span.kind.name}",
            )

            instrumentation_library_spans.spans.append(
                CollectorSpan(**self._collector_kwargs))

        return ExportTraceServiceRequest(resource_spans=get_resource_data(
            sdk_resource_instrumentation_library_spans,
            ResourceSpans,
            "spans",
        ))
Ejemplo n.º 5
0
    def _translate_spans(
        self,
        sdk_spans: Sequence[SDKSpan],
    ) -> ExportTraceServiceRequest:

        sdk_resource_instrumentation_library_spans = {}

        for sdk_span in sdk_spans:

            if sdk_span.resource not in (
                    sdk_resource_instrumentation_library_spans.keys()):
                sdk_resource_instrumentation_library_spans[
                    sdk_span.resource] = InstrumentationLibrarySpans()

            self._collector_span_kwargs = {}

            self._translate_name(sdk_span)
            self._translate_start_time(sdk_span)
            self._translate_end_time(sdk_span)
            self._translate_span_id(sdk_span)
            self._translate_trace_id(sdk_span)
            self._translate_parent(sdk_span)
            self._translate_context_trace_state(sdk_span)
            self._translate_attributes(sdk_span)
            self._translate_events(sdk_span)
            self._translate_links(sdk_span)
            self._translate_status(sdk_span)

            self._collector_span_kwargs["kind"] = getattr(
                CollectorSpan.SpanKind, sdk_span.kind.name)

            sdk_resource_instrumentation_library_spans[
                sdk_span.resource].spans.append(
                    CollectorSpan(**self._collector_span_kwargs))

        resource_spans = []

        for (
                sdk_resource,
                instrumentation_library_spans,
        ) in sdk_resource_instrumentation_library_spans.items():

            collector_resource = Resource()

            for key, value in sdk_resource.labels.items():

                try:
                    collector_resource.attributes.append(
                        AttributeKeyValue(**_translate_key_values(key, value)))
                except Exception as error:  # pylint: disable=broad-except
                    logger.exception(error)

            resource_spans.append(
                ResourceSpans(
                    resource=collector_resource,
                    instrumentation_library_spans=[
                        instrumentation_library_spans
                    ],
                ))

        return ExportTraceServiceRequest(resource_spans=resource_spans)
Ejemplo n.º 6
0
    def test_translate_spans_multi(self):
        expected = ExportTraceServiceRequest(
            resource_spans=[
                ResourceSpans(
                    resource=OTLPResource(
                        attributes=[
                            KeyValue(key="a", value=AnyValue(int_value=1)),
                            KeyValue(
                                key="b", value=AnyValue(bool_value=False)
                            ),
                        ]
                    ),
                    instrumentation_library_spans=[
                        InstrumentationLibrarySpans(
                            instrumentation_library=InstrumentationLibrary(
                                name="name", version="version"
                            ),
                            spans=[
                                OTLPSpan(
                                    # pylint: disable=no-member
                                    name="a",
                                    start_time_unix_nano=self.span.start_time,
                                    end_time_unix_nano=self.span.end_time,
                                    trace_state="a=b,c=d",
                                    span_id=int.to_bytes(
                                        10217189687419569865, 8, "big"
                                    ),
                                    trace_id=int.to_bytes(
                                        67545097771067222548457157018666467027,
                                        16,
                                        "big",
                                    ),
                                    parent_span_id=(
                                        b"\000\000\000\000\000\00009"
                                    ),
                                    kind=(
                                        OTLPSpan.SpanKind.SPAN_KIND_INTERNAL
                                    ),
                                    attributes=[
                                        KeyValue(
                                            key="a",
                                            value=AnyValue(int_value=1),
                                        ),
                                        KeyValue(
                                            key="b",
                                            value=AnyValue(bool_value=True),
                                        ),
                                    ],
                                    events=[
                                        OTLPSpan.Event(
                                            name="a",
                                            time_unix_nano=1591240820506462784,
                                            attributes=[
                                                KeyValue(
                                                    key="a",
                                                    value=AnyValue(
                                                        int_value=1
                                                    ),
                                                ),
                                                KeyValue(
                                                    key="b",
                                                    value=AnyValue(
                                                        bool_value=False
                                                    ),
                                                ),
                                            ],
                                        )
                                    ],
                                    status=Status(code=0, message=""),
                                    links=[
                                        OTLPSpan.Link(
                                            trace_id=int.to_bytes(
                                                1, 16, "big"
                                            ),
                                            span_id=int.to_bytes(2, 8, "big"),
                                            attributes=[
                                                KeyValue(
                                                    key="a",
                                                    value=AnyValue(
                                                        int_value=1
                                                    ),
                                                ),
                                                KeyValue(
                                                    key="b",
                                                    value=AnyValue(
                                                        bool_value=False
                                                    ),
                                                ),
                                            ],
                                        )
                                    ],
                                )
                            ],
                        ),
                        InstrumentationLibrarySpans(
                            instrumentation_library=InstrumentationLibrary(
                                name="name2", version="version2"
                            ),
                            spans=[
                                OTLPSpan(
                                    # pylint: disable=no-member
                                    name="c",
                                    start_time_unix_nano=self.span3.start_time,
                                    end_time_unix_nano=self.span3.end_time,
                                    trace_state="a=b,c=d",
                                    span_id=int.to_bytes(
                                        10217189687419569865, 8, "big"
                                    ),
                                    trace_id=int.to_bytes(
                                        67545097771067222548457157018666467027,
                                        16,
                                        "big",
                                    ),
                                    parent_span_id=(
                                        b"\000\000\000\000\000\00009"
                                    ),
                                    kind=(
                                        OTLPSpan.SpanKind.SPAN_KIND_INTERNAL
                                    ),
                                    status=Status(code=0, message=""),
                                )
                            ],
                        ),
                    ],
                ),
                ResourceSpans(
                    resource=OTLPResource(
                        attributes=[
                            KeyValue(key="a", value=AnyValue(int_value=2)),
                            KeyValue(
                                key="b", value=AnyValue(bool_value=False)
                            ),
                        ]
                    ),
                    instrumentation_library_spans=[
                        InstrumentationLibrarySpans(
                            instrumentation_library=InstrumentationLibrary(
                                name="name", version="version"
                            ),
                            spans=[
                                OTLPSpan(
                                    # pylint: disable=no-member
                                    name="b",
                                    start_time_unix_nano=self.span2.start_time,
                                    end_time_unix_nano=self.span2.end_time,
                                    trace_state="a=b,c=d",
                                    span_id=int.to_bytes(
                                        10217189687419569865, 8, "big"
                                    ),
                                    trace_id=int.to_bytes(
                                        67545097771067222548457157018666467027,
                                        16,
                                        "big",
                                    ),
                                    parent_span_id=(
                                        b"\000\000\000\000\000\00009"
                                    ),
                                    kind=(
                                        OTLPSpan.SpanKind.SPAN_KIND_INTERNAL
                                    ),
                                    status=Status(code=0, message=""),
                                )
                            ],
                        )
                    ],
                ),
            ]
        )

        # pylint: disable=protected-access
        self.assertEqual(
            expected,
            self.exporter._translate_data([self.span, self.span2, self.span3]),
        )