def format_attributes_json(self):
        """Convert the Attributes object to json format."""
        attributes_json = {}

        for key, value in self.attributes.items():
            key = utils.check_str_length(key)[0]
            value = _format_attribute_value(value)

            if value is not None:
                attributes_json[key] = value

        return {'attributeMap': attributes_json}
    def test_check_str_length(self):
        limit = 5

        str_to_check = u'test测试'

        (result,
         truncated_byte_count) = utils.check_str_length(str_to_check, limit)

        expected_result = 'test'

        # Should only have 4 bytes remained, dropped off the invalid part if
        # truncated in the middle of a character.
        self.assertEqual(expected_result, result)
        self.assertEqual(truncated_byte_count, 5)
def _extract_tags_from_span(attr):
    if attr is None:
        return {}
    tags = {}
    for attribute_key, attribute_value in attr.items():
        if isinstance(attribute_value, (int, bool, float)):
            value = str(attribute_value)
        elif isinstance(attribute_value, str):
            res, _ = check_str_length(str_to_check=attribute_value)
            value = res
        else:
            logging.warning('Could not serialize tag %s', attribute_key)
            continue
        tags[attribute_key] = value
    return tags