def _filter_attribute_values(attributes: types.Attributes): if attributes: for attr_key, attr_value in list(attributes.items()): if _is_valid_attribute_value(attr_value): if isinstance(attr_value, MutableSequence): attributes[attr_key] = tuple(attr_value) else: attributes.pop(attr_key)
def _extract_attributes( attrs: types.Attributes, num_attrs_limit: int, add_agent_attr: bool = False, ) -> ProtoSpan.Attributes: """Convert span.attributes to dict.""" attributes_dict = BoundedDict( num_attrs_limit) # type: BoundedDict[str, AttributeValue] invalid_value_dropped_count = 0 for key, value in attrs.items() if attrs else []: key = _truncate_str(key, 128)[0] if key in LABELS_MAPPING: # pylint: disable=consider-using-get key = LABELS_MAPPING[key] value = _format_attribute_value(value) if value: attributes_dict[key] = value else: invalid_value_dropped_count += 1 if add_agent_attr: attributes_dict["g.co/agent"] = _format_attribute_value( "opentelemetry-python {}; google-cloud-trace-exporter {}".format( _strip_characters( pkg_resources.get_distribution( "opentelemetry-sdk").version), _strip_characters(google_ext_version), )) return ProtoSpan.Attributes( attribute_map=attributes_dict, dropped_attributes_count=attributes_dict. dropped # type: ignore[attr-defined] + invalid_value_dropped_count, )
def _extract_attributes( attrs: types.Attributes, num_attrs_limit: int, add_agent_attr: bool = False, ) -> ProtoSpan.Attributes: """Convert span.attributes to dict.""" attributes_dict = BoundedDict(num_attrs_limit) invalid_value_dropped_count = 0 for key, value in attrs.items(): key = _truncate_str(key, 128)[0] value = _format_attribute_value(value) if value: attributes_dict[key] = value else: invalid_value_dropped_count += 1 if add_agent_attr: attributes_dict["g.co/agent"] = _format_attribute_value( "opentelemetry-python {}; google-cloud-trace-exporter {}".format( _strip_characters( pkg_resources.get_distribution( "opentelemetry-sdk").version), _strip_characters(google_ext_version), )) return ProtoSpan.Attributes( attribute_map=attributes_dict, dropped_attributes_count=attributes_dict.dropped + invalid_value_dropped_count, )
def auto_event_attrs(self, addl_links: types.Attributes) -> types.Attributes: """Get the event attributes for auto-eventing a span.""" return { "spanned_reason": self._autoevent_reason_value, "span_behavior": str(self.behavior), "added_attributes": list(addl_links.keys()) if addl_links else [], }
def _encode_attributes( attributes: Attributes, ) -> Optional[List[PB2KeyValue]]: if attributes: pb2_attributes = [] for key, value in attributes.items(): try: pb2_attributes.append(_encode_key_value(key, value)) except Exception as error: # pylint: disable=broad-except _logger.exception(error) else: pb2_attributes = None return pb2_attributes
def _extract_attributes(attrs: types.Attributes, num_attrs_limit: int) -> ProtoSpan.Attributes: """Convert span.attributes to dict.""" attributes_dict = BoundedDict(num_attrs_limit) for key, value in attrs.items(): key = _truncate_str(key, 128)[0] value = _format_attribute_value(value) if value is not None: attributes_dict[key] = value return ProtoSpan.Attributes( attribute_map=attributes_dict, dropped_attributes_count=len(attrs) - len(attributes_dict), )