Example #1
0
    def _test_encode_max_tag_length(self, max_tag_value_length: int):
        otel_span, expected_tag_output = self.get_data_for_max_tag_length_test(
            max_tag_value_length
        )
        service_name = otel_span.name

        binary_annotations = []
        for tag_key, tag_expected_value in expected_tag_output.items():
            binary_annotations.append(
                {
                    "key": tag_key,
                    "value": tag_expected_value,
                    "endpoint": {"serviceName": service_name},
                }
            )

        expected_output = [
            {
                "traceId": JsonV1Encoder._encode_trace_id(
                    otel_span.context.trace_id
                ),
                "id": JsonV1Encoder._encode_span_id(otel_span.context.span_id),
                "name": service_name,
                "timestamp": JsonV1Encoder._nsec_to_usec_round(
                    otel_span.start_time
                ),
                "duration": JsonV1Encoder._nsec_to_usec_round(
                    otel_span.end_time - otel_span.start_time
                ),
                "binaryAnnotations": binary_annotations,
                "debug": True,
            }
        ]

        self.assert_equal_encoded_spans(
            json.dumps(expected_output),
            JsonV1Encoder(max_tag_value_length).serialize(
                [otel_span], NodeEndpoint()
            ),
        )
Example #2
0
    def test_encode(self):

        local_endpoint = {"serviceName": TEST_SERVICE_NAME}

        otel_spans = self.get_exhaustive_otel_span_list()
        trace_id = JsonV1Encoder._encode_trace_id(
            otel_spans[0].context.trace_id
        )

        expected_output = [
            {
                "traceId": trace_id,
                "id": JsonV1Encoder._encode_span_id(
                    otel_spans[0].context.span_id
                ),
                "name": otel_spans[0].name,
                "timestamp": otel_spans[0].start_time // 10 ** 3,
                "duration": (otel_spans[0].end_time // 10 ** 3)
                - (otel_spans[0].start_time // 10 ** 3),
                "annotations": [
                    {
                        "timestamp": otel_spans[0].events[0].timestamp
                        // 10 ** 3,
                        "value": json.dumps(
                            {
                                "event0": {
                                    "annotation_bool": True,
                                    "annotation_string": "annotation_test",
                                    "key_float": 0.3,
                                }
                            },
                            sort_keys=True,
                        ),
                        "endpoint": local_endpoint,
                    }
                ],
                "binaryAnnotations": [
                    {
                        "key": "key_bool",
                        "value": "false",
                        "endpoint": local_endpoint,
                    },
                    {
                        "key": "key_string",
                        "value": "hello_world",
                        "endpoint": local_endpoint,
                    },
                    {
                        "key": "key_float",
                        "value": "111.22",
                        "endpoint": local_endpoint,
                    },
                    {
                        "key": "otel.status_code",
                        "value": "OK",
                        "endpoint": local_endpoint,
                    },
                ],
                "debug": True,
                "parentId": JsonV1Encoder._encode_span_id(
                    otel_spans[0].parent.span_id
                ),
            },
            {
                "traceId": trace_id,
                "id": JsonV1Encoder._encode_span_id(
                    otel_spans[1].context.span_id
                ),
                "name": otel_spans[1].name,
                "timestamp": otel_spans[1].start_time // 10 ** 3,
                "duration": (otel_spans[1].end_time // 10 ** 3)
                - (otel_spans[1].start_time // 10 ** 3),
                "binaryAnnotations": [
                    {
                        "key": "key_resource",
                        "value": "some_resource",
                        "endpoint": local_endpoint,
                    },
                    {
                        "key": "otel.status_code",
                        "value": "ERROR",
                        "endpoint": local_endpoint,
                    },
                    {
                        "key": "error",
                        "value": "Example description",
                        "endpoint": local_endpoint,
                    },
                ],
            },
            {
                "traceId": trace_id,
                "id": JsonV1Encoder._encode_span_id(
                    otel_spans[2].context.span_id
                ),
                "name": otel_spans[2].name,
                "timestamp": otel_spans[2].start_time // 10 ** 3,
                "duration": (otel_spans[2].end_time // 10 ** 3)
                - (otel_spans[2].start_time // 10 ** 3),
                "binaryAnnotations": [
                    {
                        "key": "key_string",
                        "value": "hello_world",
                        "endpoint": local_endpoint,
                    },
                    {
                        "key": "key_resource",
                        "value": "some_resource",
                        "endpoint": local_endpoint,
                    },
                ],
            },
            {
                "traceId": trace_id,
                "id": JsonV1Encoder._encode_span_id(
                    otel_spans[3].context.span_id
                ),
                "name": otel_spans[3].name,
                "timestamp": otel_spans[3].start_time // 10 ** 3,
                "duration": (otel_spans[3].end_time // 10 ** 3)
                - (otel_spans[3].start_time // 10 ** 3),
                "binaryAnnotations": [
                    {
                        "key": NAME_KEY,
                        "value": "name",
                        "endpoint": local_endpoint,
                    },
                    {
                        "key": VERSION_KEY,
                        "value": "version",
                        "endpoint": local_endpoint,
                    },
                ],
            },
        ]

        self.assert_equal_encoded_spans(
            json.dumps(expected_output),
            JsonV1Encoder().serialize(otel_spans, NodeEndpoint()),
        )