Exemple #1
0
    def _translate_links(self, sdk_span: SDKSpan) -> None:
        if sdk_span.links:
            self._collector_span_kwargs["links"] = []

            for sdk_span_link in sdk_span.links:

                collector_span_link = CollectorSpan.Link(
                    trace_id=(
                        sdk_span_link.context.trace_id.to_bytes(16, "big")
                    ),
                    span_id=(sdk_span_link.context.span_id.to_bytes(8, "big")),
                )

                for key, value in sdk_span_link.attributes.items():
                    try:
                        collector_span_link.attributes.append(
                            _translate_key_values(key, value)
                        )
                    # pylint: disable=broad-except
                    except Exception as error:
                        logger.exception(error)

                self._collector_span_kwargs["links"].append(
                    collector_span_link
                )
Exemple #2
0
def _encode_links(links: List[Link]) -> List[PB2SPan.Link]:
    pb2_links = None
    if links:
        pb2_links = []
        for link in links:
            encoded_link = PB2SPan.Link(
                trace_id=_encode_trace_id(link.context.trace_id),
                span_id=_encode_span_id(link.context.span_id),
            )
            for key, value in link.attributes.items():
                try:
                    encoded_link.attributes.append(
                        _encode_key_value(key, value)
                    )
                # pylint: disable=broad-except
                except Exception as error:
                    _logger.exception(error)
            pb2_links.append(encoded_link)
    return pb2_links
    def _translate_events(self, sdk_span: ReadableSpan) -> None:
        if sdk_span.events:
            self._collector_kwargs["events"] = []

            for sdk_span_event in sdk_span.events:

                collector_span_event = CollectorSpan.Event(
                    name=sdk_span_event.name,
                    time_unix_nano=sdk_span_event.timestamp,
                    dropped_attributes_count=sdk_span_event.attributes.dropped,
                )

                for key, value in sdk_span_event.attributes.items():
                    try:
                        collector_span_event.attributes.append(
                            _translate_key_values(key, value))
                    # pylint: disable=broad-except
                    except Exception as error:
                        logger.exception(error)

                self._collector_kwargs["events"].append(collector_span_event)
Exemple #4
0
def _encode_events(
    events: Sequence[Event],
) -> Optional[List[PB2SPan.Event]]:
    pb2_events = None
    if events:
        pb2_events = []
        for event in events:
            encoded_event = PB2SPan.Event(
                name=event.name,
                time_unix_nano=event.timestamp,
            )
            for key, value in event.attributes.items():
                try:
                    encoded_event.attributes.append(
                        _encode_key_value(key, value)
                    )
                # pylint: disable=broad-except
                except Exception as error:
                    _logger.exception(error)
            pb2_events.append(encoded_event)
    return pb2_events
    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]))
Exemple #6
0
    def get_exhaustive_test_spans(
        self,
    ) -> Tuple[List[SDKSpan], PB2ExportTraceServiceRequest]:
        otel_spans = self.get_exhaustive_otel_span_list()
        trace_id = _encode_trace_id(otel_spans[0].context.trace_id)
        span_kind = _SPAN_KIND_MAP[SDKSpanKind.INTERNAL]

        pb2_service_request = PB2ExportTraceServiceRequest(
            resource_spans=[
                PB2ResourceSpans(
                    resource=PB2Resource(),
                    instrumentation_library_spans=[
                        PB2InstrumentationLibrarySpans(
                            instrumentation_library=PB2InstrumentationLibrary(),
                            spans=[
                                PB2SPan(
                                    trace_id=trace_id,
                                    span_id=_encode_span_id(
                                        otel_spans[0].context.span_id
                                    ),
                                    trace_state=None,
                                    parent_span_id=_encode_span_id(
                                        otel_spans[0].parent.span_id
                                    ),
                                    name=otel_spans[0].name,
                                    kind=span_kind,
                                    start_time_unix_nano=otel_spans[
                                        0
                                    ].start_time,
                                    end_time_unix_nano=otel_spans[0].end_time,
                                    attributes=[
                                        PB2KeyValue(
                                            key="key_bool",
                                            value=PB2AnyValue(
                                                bool_value=False
                                            ),
                                        ),
                                        PB2KeyValue(
                                            key="key_string",
                                            value=PB2AnyValue(
                                                string_value="hello_world"
                                            ),
                                        ),
                                        PB2KeyValue(
                                            key="key_float",
                                            value=PB2AnyValue(
                                                double_value=111.22
                                            ),
                                        ),
                                    ],
                                    events=[
                                        PB2SPan.Event(
                                            name="event0",
                                            time_unix_nano=otel_spans[0]
                                            .events[0]
                                            .timestamp,
                                            attributes=[
                                                PB2KeyValue(
                                                    key="annotation_bool",
                                                    value=PB2AnyValue(
                                                        bool_value=True
                                                    ),
                                                ),
                                                PB2KeyValue(
                                                    key="annotation_string",
                                                    value=PB2AnyValue(
                                                        string_value="annotation_test"
                                                    ),
                                                ),
                                                PB2KeyValue(
                                                    key="key_float",
                                                    value=PB2AnyValue(
                                                        double_value=0.3
                                                    ),
                                                ),
                                            ],
                                        )
                                    ],
                                    links=[
                                        PB2SPan.Link(
                                            trace_id=_encode_trace_id(
                                                otel_spans[0]
                                                .links[0]
                                                .context.trace_id
                                            ),
                                            span_id=_encode_span_id(
                                                otel_spans[0]
                                                .links[0]
                                                .context.span_id
                                            ),
                                            attributes=[
                                                PB2KeyValue(
                                                    key="key_bool",
                                                    value=PB2AnyValue(
                                                        bool_value=True
                                                    ),
                                                ),
                                            ],
                                        )
                                    ],
                                    status=PB2Status(
                                        code=SDKStatusCode.ERROR.value,
                                        message="Example description",
                                    ),
                                )
                            ],
                        ),
                        PB2InstrumentationLibrarySpans(
                            instrumentation_library=PB2InstrumentationLibrary(
                                name="name",
                                version="version",
                            ),
                            spans=[
                                PB2SPan(
                                    trace_id=trace_id,
                                    span_id=_encode_span_id(
                                        otel_spans[3].context.span_id
                                    ),
                                    trace_state=None,
                                    parent_span_id=None,
                                    name=otel_spans[3].name,
                                    kind=span_kind,
                                    start_time_unix_nano=otel_spans[
                                        3
                                    ].start_time,
                                    end_time_unix_nano=otel_spans[3].end_time,
                                    attributes=None,
                                    events=None,
                                    links=None,
                                    status={},
                                )
                            ],
                        ),
                    ],
                ),
                PB2ResourceSpans(
                    resource=PB2Resource(
                        attributes=[
                            PB2KeyValue(
                                key="key_resource",
                                value=PB2AnyValue(
                                    string_value="some_resource"
                                ),
                            )
                        ]
                    ),
                    instrumentation_library_spans=[
                        PB2InstrumentationLibrarySpans(
                            instrumentation_library=PB2InstrumentationLibrary(),
                            spans=[
                                PB2SPan(
                                    trace_id=trace_id,
                                    span_id=_encode_span_id(
                                        otel_spans[1].context.span_id
                                    ),
                                    trace_state=None,
                                    parent_span_id=None,
                                    name=otel_spans[1].name,
                                    kind=span_kind,
                                    start_time_unix_nano=otel_spans[
                                        1
                                    ].start_time,
                                    end_time_unix_nano=otel_spans[1].end_time,
                                    attributes=None,
                                    events=None,
                                    links=None,
                                    status={},
                                ),
                                PB2SPan(
                                    trace_id=trace_id,
                                    span_id=_encode_span_id(
                                        otel_spans[2].context.span_id
                                    ),
                                    trace_state=None,
                                    parent_span_id=None,
                                    name=otel_spans[2].name,
                                    kind=span_kind,
                                    start_time_unix_nano=otel_spans[
                                        2
                                    ].start_time,
                                    end_time_unix_nano=otel_spans[2].end_time,
                                    attributes=[
                                        PB2KeyValue(
                                            key="key_string",
                                            value=PB2AnyValue(
                                                string_value="hello_world"
                                            ),
                                        ),
                                    ],
                                    events=None,
                                    links=None,
                                    status={},
                                ),
                            ],
                        )
                    ],
                ),
            ]
        )

        return otel_spans, pb2_service_request
Exemple #7
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]),
        )