def _format_attribute_value(value: types.AttributeValue) -> AttributeValue: if isinstance(value, bool): value_type = "bool_value" elif isinstance(value, int): value_type = "int_value" elif isinstance(value, str): value_type = "string_value" value = _get_truncatable_str_object(value, 256) elif isinstance(value, float): value_type = "string_value" value = _get_truncatable_str_object("{:0.4f}".format(value), 256) elif isinstance(value, collections.Sequence): value_type = "string_value" value = _get_truncatable_str_object( ",".join(str(x) for x in value), 256 ) else: logger.warning( "ignoring attribute value %s of type %s. Values type must be one " "of bool, int, string or float, or a sequence of these", value, type(value), ) return None return AttributeValue(**{value_type: value})
def test_extract_links(self): self.assertIsNone(_extract_links([])) trace_id = "6e0c63257de34c92bf9efcd03927272e" span_id1 = "95bb5edabd45950f" span_id2 = "b6b86ad2915c9ddc" link1 = Link( context=SpanContext( trace_id=int(trace_id, 16), span_id=int(span_id1, 16), is_remote=False, ), attributes={}, ) link2 = Link( context=SpanContext( trace_id=int(trace_id, 16), span_id=int(span_id1, 16), is_remote=False, ), attributes=self.attributes_variety_pack, ) link3 = Link( context=SpanContext( trace_id=int(trace_id, 16), span_id=int(span_id2, 16), is_remote=False, ), attributes={"illegal_attr_value": dict(), "int_attr_value": 123}, ) self.assertEqual( _extract_links([link1, link2, link3]), ProtoSpan.Links( link=[ { "trace_id": trace_id, "span_id": span_id1, "type": "TYPE_UNSPECIFIED", "attributes": ProtoSpan.Attributes(attribute_map={}), }, { "trace_id": trace_id, "span_id": span_id1, "type": "TYPE_UNSPECIFIED", "attributes": self.extracted_attributes_variety_pack, }, { "trace_id": trace_id, "span_id": span_id2, "type": "TYPE_UNSPECIFIED", "attributes": { "attribute_map": { "int_attr_value": AttributeValue(int_value=123) }, "dropped_attributes_count": 1, }, }, ] ), )
def test_attribute_value_truncation(self): self.assertEqual( _format_attribute_value(self.str_300), AttributeValue( string_value=TruncatableString( value=self.str_256, truncated_byte_count=300 - 256 ) ), )
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)) }), )
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_list_attribute_value(self): self.assertEqual( _format_attribute_value(("one", "two")), AttributeValue(string_value=TruncatableString( value="one,two", truncated_byte_count=0)), ) self.assertEqual( _format_attribute_value([True]), AttributeValue(string_value=TruncatableString( value="True", truncated_byte_count=0)), ) self.assertEqual( _format_attribute_value((2, 5)), AttributeValue(string_value=TruncatableString( value="2,5", truncated_byte_count=0)), ) self.assertEqual( _format_attribute_value([2.0, 0.5, 4.55]), AttributeValue(string_value=TruncatableString( value="2.0,0.5,4.55", truncated_byte_count=0)), )
def setUpClass(cls): cls.project_id = "PROJECT" cls.attributes_variety_pack = { "str_key": "str_value", "bool_key": False, "double_key": 1.421, "int_key": 123, } cls.extracted_attributes_variety_pack = ProtoSpan.Attributes( attribute_map={ "str_key": AttributeValue( string_value=TruncatableString( value="str_value", truncated_byte_count=0 ) ), "bool_key": AttributeValue(bool_value=False), "double_key": AttributeValue( string_value=TruncatableString( value="1.4210", truncated_byte_count=0 ) ), "int_key": AttributeValue(int_value=123), } ) cls.agent_code = _format_attribute_value( "opentelemetry-python {}; google-cloud-trace-exporter {}".format( _strip_characters( pkg_resources.get_distribution("opentelemetry-sdk").version ), _strip_characters(google_ext_version), ) ) cls.example_trace_id = "6e0c63257de34c92bf9efcd03927272e" cls.example_span_id = "95bb5edabd45950f" cls.example_time_in_ns = 1589919268850900051 cls.example_time_stamp = _get_time_from_ns(cls.example_time_in_ns) cls.str_300 = "a" * 300 cls.str_256 = "a" * 256 cls.str_128 = "a" * 128
def setUp(self): self.client_patcher = mock.patch( "opentelemetry.exporter.cloud_trace.TraceServiceClient") self.client_patcher.start() self.project_id = "PROJECT" self.attributes_variety_pack = { "str_key": "str_value", "bool_key": False, "double_key": 1.421, "int_key": 123, } self.extracted_attributes_variety_pack = ProtoSpan.Attributes( attribute_map={ "str_key": AttributeValue(string_value=TruncatableString( value="str_value", truncated_byte_count=0)), "bool_key": AttributeValue(bool_value=False), "double_key": AttributeValue(string_value=TruncatableString( value="1.4210", truncated_byte_count=0)), "int_key": AttributeValue(int_value=123), })
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, )