Exemplo n.º 1
0
def transform(metrics, tenant_id, region):
    transformed_metric = {'metric': {},
                          'meta': {'tenantId': tenant_id, 'region': region},
                          'creation_time': timeutils.utcnow_ts()}

    if isinstance(metrics, list):
        transformed_metrics = []
        for metric in metrics:
            transformed_metric['metric'] = metric
            transformed_metrics.append(rest_utils.as_json(transformed_metric))
        return transformed_metrics
    else:
        transformed_metric['metric'] = metrics
        return [rest_utils.as_json(transformed_metric)]
Exemplo n.º 2
0
def to_json(data):
    """Converts data to JSON string.

    :param dict data: data to be transformed to JSON
    :return: JSON string
    :rtype: str
    :raises: Exception
    """
    try:
        # NOTE(trebskit) ensure_ascii => UTF-8
        return rest_utils.as_json(data, ensure_ascii=False)
    except Exception as ex:
        LOG.exception(ex)
        raise
Exemplo n.º 3
0
def serialize_envelope(envelope):
    """Returns json representation of an envelope.

    :return: json object of envelope
    :rtype: six.text_type

    """
    json = rest_utils.as_json(envelope, ensure_ascii=False)

    if six.PY2:
        raw = six.text_type(json.replace(r'\\', r'\\\\'),
                            encoding='utf-8',
                            errors='replace')
    else:
        raw = json

    return raw
Exemplo n.º 4
0
    def _truncate(self, envelope):
        """Truncates the message if needed.

        Each message send to kafka is verified.
        Method checks if message serialized to json
        exceeds maximum allowed size that can be posted to kafka
        queue. If so, method truncates message property of the log
        by difference between message and allowed size.

        :param Envelope envelope: original envelope
        :return: serialized message
        :rtype: str
        """

        msg_str = model.serialize_envelope(envelope)
        envelope_size = ((len(bytearray(msg_str, 'utf-8', 'replace')) +
                          _TIMESTAMP_KEY_SIZE +
                          _KAFKA_META_DATA_SIZE)
                         if msg_str is not None else -1)

        diff_size = ((envelope_size - self.max_message_size) +
                     _TRUNCATION_SAFE_OFFSET)

        if diff_size > 1:
            truncated_by = diff_size + _TRUNCATED_PROPERTY_SIZE

            LOG.warning(('Detected message that exceeds %d bytes,'
                         'message will be truncated by %d bytes'),
                        self.max_message_size,
                        truncated_by)

            log_msg = envelope['log']['message']
            truncated_log_msg = log_msg[:-truncated_by]

            envelope['log']['truncated'] = True
            envelope['log']['message'] = truncated_log_msg

            msg_str = rest_utils.as_json(envelope)

        return msg_str
Exemplo n.º 5
0
    def test_as_json_success(self):
        data = mock.Mock()

        dumped_json = utils.as_json(data)

        self.assertEqual(dumped_json, self.mock_json.dumps.return_value)
Exemplo n.º 6
0
 def test_from_json(self):
     body_json = {'test_body': 'test'}
     req = request.Request(
         testing.create_environ(body=rest_utils.as_json(body_json), ))
     response = helpers.from_json(req)
     self.assertEqual(body_json, response)