def test_encode_id_zero_padding(self):
        trace_id = 0x0E0C63257DE34C926F9EFCD03927272E
        span_id = 0x04BF92DEEFC58C92
        parent_id = 0x0AAAAAAAAAAAAAAA
        start_time = 683647322 * 10**9  # in ns
        duration = 50 * 10**6
        end_time = start_time + duration

        otel_span = trace._Span(
            name=TEST_SERVICE_NAME,
            context=trace_api.SpanContext(
                trace_id,
                span_id,
                is_remote=False,
                trace_flags=TraceFlags(TraceFlags.SAMPLED),
            ),
            parent=trace_api.SpanContext(trace_id, parent_id, is_remote=False),
            resource=trace.Resource({}),
        )
        otel_span.start(start_time=start_time)
        otel_span.end(end_time=end_time)

        expected_output = [{
            "traceId":
            format_trace_id(trace_id),
            "id":
            format_span_id(span_id),
            "name":
            TEST_SERVICE_NAME,
            "timestamp":
            JsonV1Encoder._nsec_to_usec_round(start_time),
            "duration":
            JsonV1Encoder._nsec_to_usec_round(duration),
            "debug":
            True,
            "parentId":
            format_span_id(parent_id),
        }]

        self.assertEqual(
            json.dumps(expected_output),
            JsonV1Encoder().serialize([otel_span], NodeEndpoint()),
        )
Example #2
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()
            ),
        )