Example #1
0
 def test_extract_variety_of_attributes(self):
     self.assertEqual(
         _extract_attributes(
             self.attributes_variety_pack, num_attrs_limit=4
         ),
         self.extracted_attributes_variety_pack,
     )
 def test_add_agent_attribute(self):
     self.assertEqual(
         _extract_attributes({}, num_attrs_limit=4, add_agent_attr=True),
         ProtoSpan.Attributes(
             attribute_map={"g.co/agent": self.agent_code},
             dropped_attributes_count=0,
         ),
     )
 def test_extract_label_mapping_attributes(self):
     attributes_labels_mapping = {
         "http.scheme":
         "http",
         "http.host":
         "172.19.0.4:8000",
         "http.method":
         "POST",
         "http.request_content_length":
         321,
         "http.response_content_length":
         123,
         "http.route":
         "/fuzzy/search",
         "http.status_code":
         200,
         "http.url":
         "http://172.19.0.4:8000/fuzzy/search",
         "http.user_agent":
         "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36",
     }
     extracted_attributes_labels_mapping = ProtoSpan.Attributes(
         attribute_map={
             "/http/client_protocol":
             AttributeValue(string_value=TruncatableString(
                 value="http", truncated_byte_count=0)),
             "/http/host":
             AttributeValue(string_value=TruncatableString(
                 value="172.19.0.4:8000", truncated_byte_count=0)),
             "/http/method":
             AttributeValue(string_value=TruncatableString(
                 value="POST", truncated_byte_count=0)),
             "/http/request/size":
             AttributeValue(int_value=321),
             "/http/response/size":
             AttributeValue(int_value=123),
             "/http/route":
             AttributeValue(string_value=TruncatableString(
                 value="/fuzzy/search", truncated_byte_count=0)),
             "/http/status_code":
             AttributeValue(int_value=200),
             "/http/url":
             AttributeValue(string_value=TruncatableString(
                 value="http://172.19.0.4:8000/fuzzy/search",
                 truncated_byte_count=0,
             )),
             "/http/user_agent":
             AttributeValue(string_value=TruncatableString(
                 value=
                 "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36",
                 truncated_byte_count=0,
             )),
         })
     self.assertEqual(
         _extract_attributes(attributes_labels_mapping, num_attrs_limit=9),
         extracted_attributes_labels_mapping,
     )
Example #4
0
    def test_extract_attributes(self):
        self.assertEqual(_extract_attributes({}, 4),
                         ProtoSpan.Attributes(attribute_map={}))
        self.assertEqual(
            _extract_attributes(self.attributes_variety_pack, 4),
            self.extracted_attributes_variety_pack,
        )
        # Test ignoring attributes with illegal value type
        self.assertEqual(
            _extract_attributes({"illegal_attribute_value": dict()}, 4),
            ProtoSpan.Attributes(attribute_map={}, dropped_attributes_count=1),
        )

        too_many_attrs = {}
        for attr_key in range(5):
            too_many_attrs[str(attr_key)] = 0
        proto_attrs = _extract_attributes(too_many_attrs, 4)
        self.assertEqual(proto_attrs.dropped_attributes_count, 1)
 def test_attribute_key_truncation(self):
     self.assertEqual(
         _extract_attributes({self.str_300: "attr_value"},
                             num_attrs_limit=4),
         ProtoSpan.Attributes(
             attribute_map={
                 self.str_128:
                 AttributeValue(string_value=TruncatableString(
                     value="attr_value", truncated_byte_count=0))
             }),
     )
Example #6
0
 def test_ignore_invalid_attributes(self):
     self.assertEqual(
         _extract_attributes(
             {"illegal_attribute_value": {}, "legal_attribute": 3},
             num_attrs_limit=4,
         ),
         ProtoSpan.Attributes(
             attribute_map={"legal_attribute": AttributeValue(int_value=3)},
             dropped_attributes_count=1,
         ),
     )
 def test_agent_attribute_priority(self):
     # Drop existing attributes in favor of the agent attribute
     self.assertEqual(
         _extract_attributes(
             {"attribute_key": "attr_value"},
             num_attrs_limit=1,
             add_agent_attr=True,
         ),
         ProtoSpan.Attributes(
             attribute_map={"g.co/agent": self.agent_code},
             dropped_attributes_count=1,
         ),
     )
    def test_truncate(self):
        """Cloud Trace API imposes limits on the length of many things,
        e.g. strings, number of events, number of attributes. We truncate
        these things before sending it to the API as an optimization.
        """
        str_300 = "a" * 300
        str_256 = "a" * 256
        str_128 = "a" * 128
        self.assertEqual(_truncate_str("aaaa", 1), ("a", 3))
        self.assertEqual(_truncate_str("aaaa", 5), ("aaaa", 0))
        self.assertEqual(_truncate_str("aaaa", 4), ("aaaa", 0))
        self.assertEqual(_truncate_str("中文翻译", 4), ("中", 9))

        self.assertEqual(
            _format_attribute_value(str_300),
            AttributeValue(
                string_value=TruncatableString(
                    value=str_256, truncated_byte_count=300 - 256
                )
            ),
        )

        self.assertEqual(
            _extract_attributes({str_300: str_300}, 4),
            ProtoSpan.Attributes(
                attribute_map={
                    str_128: AttributeValue(
                        string_value=TruncatableString(
                            value=str_256, truncated_byte_count=300 - 256
                        )
                    )
                }
            ),
        )

        time_in_ns1 = 1589919268850900051
        time_in_ms_and_ns1 = {"seconds": 1589919268, "nanos": 850899968}
        event1 = Event(name=str_300, attributes={}, timestamp=time_in_ns1)
        self.assertEqual(
            _extract_events([event1]),
            ProtoSpan.TimeEvents(
                time_event=[
                    {
                        "time": time_in_ms_and_ns1,
                        "annotation": {
                            "description": TruncatableString(
                                value=str_256, truncated_byte_count=300 - 256
                            ),
                            "attributes": {},
                        },
                    },
                ]
            ),
        )

        trace_id = "6e0c63257de34c92bf9efcd03927272e"
        span_id = "95bb5edabd45950f"
        link = Link(
            context=SpanContext(
                trace_id=int(trace_id, 16),
                span_id=int(span_id, 16),
                is_remote=False,
            ),
            attributes={},
        )
        too_many_links = [link] * (MAX_NUM_LINKS + 1)
        self.assertEqual(
            _extract_links(too_many_links),
            ProtoSpan.Links(
                link=[
                    {
                        "trace_id": trace_id,
                        "span_id": span_id,
                        "type": "TYPE_UNSPECIFIED",
                        "attributes": {},
                    }
                ]
                * MAX_NUM_LINKS,
                dropped_links_count=len(too_many_links) - MAX_NUM_LINKS,
            ),
        )

        link_attrs = {}
        for attr_key in range(MAX_LINK_ATTRS + 1):
            link_attrs[str(attr_key)] = 0
        attr_link = Link(
            context=SpanContext(
                trace_id=int(trace_id, 16),
                span_id=int(span_id, 16),
                is_remote=False,
            ),
            attributes=link_attrs,
        )

        proto_link = _extract_links([attr_link])
        self.assertEqual(
            len(proto_link.link[0].attributes.attribute_map), MAX_LINK_ATTRS
        )

        too_many_events = [event1] * (MAX_NUM_EVENTS + 1)
        self.assertEqual(
            _extract_events(too_many_events),
            ProtoSpan.TimeEvents(
                time_event=[
                    {
                        "time": time_in_ms_and_ns1,
                        "annotation": {
                            "description": TruncatableString(
                                value=str_256, truncated_byte_count=300 - 256
                            ),
                            "attributes": {},
                        },
                    },
                ]
                * MAX_NUM_EVENTS,
                dropped_annotations_count=len(too_many_events)
                - MAX_NUM_EVENTS,
            ),
        )

        time_in_ns1 = 1589919268850900051
        event_attrs = {}
        for attr_key in range(MAX_EVENT_ATTRS + 1):
            event_attrs[str(attr_key)] = 0
        proto_events = _extract_events(
            [Event(name="a", attributes=event_attrs, timestamp=time_in_ns1)]
        )
        self.assertEqual(
            len(
                proto_events.time_event[0].annotation.attributes.attribute_map
            ),
            MAX_EVENT_ATTRS,
        )
 def test_too_many_attributes(self):
     too_many_attrs = {}
     for attr_key in range(5):
         too_many_attrs[str(attr_key)] = 0
     proto_attrs = _extract_attributes(too_many_attrs, num_attrs_limit=4)
     self.assertEqual(proto_attrs.dropped_attributes_count, 1)
 def test_extract_empty_attributes(self):
     self.assertEqual(
         _extract_attributes({}, num_attrs_limit=4),
         ProtoSpan.Attributes(attribute_map={}),
     )