Exemplo n.º 1
0
 def test_should_fail_pass_for_non_message_in_log_property(self):
     log_object = {
         'massage': 'some messages',
         'application_type': 'monasca-log-api',
         'dimensions': {
             'hostname': 'devstack'
         }
     }
     validation.validate_log_message(log_object)
Exemplo n.º 2
0
    def new_log(self,
                application_type,
                dimensions,
                payload,
                content_type='application/json',
                validate=True):
        """Creates new log object.

        :param str application_type: origin of the log
        :param dict dimensions: dictionary of dimensions (any data sent to api)
        :param stream payload: stream to read log entry from
        :param str content_type: actual content type used to send data to
                                 server
        :param bool validate: by default True, marks if log should be validated
        :return: log object
        :rtype: dict

        :keyword: log_object
        """

        payload = rest_utils.read_body(payload, content_type)
        if not payload:
            return None

        # normalize_yet_again
        application_type = parse_application_type(application_type)
        dimensions = parse_dimensions(dimensions)

        if validate:
            self._log.debug('Validation enabled, proceeding with validation')
            validation.validate_application_type(application_type)
            validation.validate_dimensions(dimensions)

        self._log.debug(
            'application_type=%s,dimensions=%s' % (
                application_type, dimensions)
        )

        log_object = {}
        if content_type == 'application/json':
            log_object.update(payload)
        else:
            log_object.update({'message': payload})

        validation.validate_log_message(log_object)

        dimensions['component'] = application_type
        log_object.update({'dimensions': dimensions})

        return log_object
Exemplo n.º 3
0
    def on_post(self, req, res):
        validation.validate_payload_size(req)
        validation.validate_content_type(req, Logs.SUPPORTED_CONTENT_TYPES)

        cross_tenant_id = req.get_param('tenant_id')
        tenant_id = req.get_header(*headers.X_TENANT_ID)
        validation.validate_cross_tenant(
            tenant_id=tenant_id,
            cross_tenant_id=cross_tenant_id,
            roles=req.get_header(*headers.X_ROLES)
        )

        request_body = helpers.read_json_msg_body(req)
        log_list = self._get_logs(request_body)
        global_dimensions = self._get_global_dimensions(request_body)

        envelopes = []
        for log_element in log_list:
            try:
                LOG.trace('Processing log %s', log_element)

                validation.validate_log_message(log_element)

                dimensions = self._get_dimensions(log_element,
                                                  global_dimensions)
                envelope = self._create_log_envelope(tenant_id,
                                                     cross_tenant_id,
                                                     dimensions,
                                                     log_element)
                envelopes.append(envelope)

                LOG.trace('Log %s processed into envelope %s',
                          log_element,
                          envelope)
            except Exception as ex:
                LOG.error('Failed to process log %s', log_element)
                LOG.exception(ex)
                res.status = getattr(ex, 'status', falcon.HTTP_500)
                return

        self._send_logs(envelopes)
        res.status = falcon.HTTP_204
Exemplo n.º 4
0
    def _transform_message(self, log_element, *args):
        try:
            validation.validate_log_message(log_element)

            log_envelope = model.Envelope.new_envelope(
                log=log_element,
                tenant_id=args[1],
                region=self.service_region,
                dimensions=self._get_dimensions(log_element,
                                                global_dims=args[0])
            )

            msg_payload = (super(BulkProcessor, self)
                           ._transform_message(log_envelope))

            return msg_payload
        except Exception as ex:
            LOG.error('Log transformation failed, rejecting log')
            LOG.exception(ex)

            return None