"Fossies" - the Fresh Open Source Software Archive

Member "monasca-api-4.0.0/monasca_api/api/core/log/model.py" (13 May 2020, 3339 Bytes) of package /linux/misc/openstack/monasca-api-4.0.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "model.py" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 3.1.0_vs_4.0.0.

    1 # Copyright 2016 FUJITSU LIMITED
    2 #
    3 # Licensed under the Apache License, Version 2.0 (the "License"); you may
    4 # not use this file except in compliance with the License. You may obtain
    5 # a copy of the License at
    6 #
    7 #      http://www.apache.org/licenses/LICENSE-2.0
    8 #
    9 # Unless required by applicable law or agreed to in writing, software
   10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
   11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   12 # License for the specific language governing permissions and limitations
   13 # under the License.
   14 
   15 from oslo_utils import timeutils
   16 import six
   17 
   18 from monasca_api.common.rest import utils as rest_utils
   19 
   20 
   21 def serialize_envelope(envelope):
   22     """Returns json representation of an envelope.
   23 
   24     :return: json object of envelope
   25     :rtype: six.text_type
   26 
   27     """
   28     json = rest_utils.as_json(envelope, ensure_ascii=False)
   29 
   30     if six.PY2:
   31         raw = six.text_type(json.replace(r'\\', r'\\\\'), encoding='utf-8',
   32                             errors='replace')
   33     else:
   34         raw = json
   35 
   36     return raw
   37 
   38 
   39 class LogEnvelopeException(Exception):
   40     pass
   41 
   42 
   43 class Envelope(dict):
   44     def __init__(self, log, meta):
   45         if not log:
   46             error_msg = 'Envelope cannot be created without log'
   47             raise LogEnvelopeException(error_msg)
   48         if 'tenantId' not in meta or not meta.get('tenantId'):
   49             error_msg = 'Envelope cannot be created without tenant'
   50             raise LogEnvelopeException(error_msg)
   51 
   52         creation_time = self._get_creation_time()
   53         super(Envelope, self).__init__(
   54             log=log,
   55             creation_time=creation_time,
   56             meta=meta
   57         )
   58 
   59     @staticmethod
   60     def _get_creation_time():
   61         return timeutils.utcnow_ts()
   62 
   63     @classmethod
   64     def new_envelope(cls, log, tenant_id, region, dimensions=None):
   65         """Creates new log envelope
   66 
   67         Log envelope is combined ouf of following properties
   68 
   69         * log - dict
   70         * creation_time - timestamp
   71         * meta - meta block
   72 
   73         Example output json would like this:
   74 
   75         .. code-block:: json
   76 
   77             {
   78                 "log": {
   79                   "message": "Some message",
   80                   "dimensions": {
   81                     "hostname": "devstack"
   82                   }
   83                 },
   84                 "creation_time": 1447834886,
   85                 "meta": {
   86                   "tenantId": "e4bd29509eda473092d32aadfee3e7b1",
   87                   "region": "pl"
   88                 }
   89             }
   90 
   91         :param dict log: original log element (containing message and other
   92                          params
   93         :param str tenant_id: tenant id to be put in meta field
   94         :param str region: region to be put in meta field
   95         :param dict dimensions: additional dimensions to be appended to log
   96                                 object dimensions
   97 
   98         """
   99         if dimensions:
  100             log['dimensions'].update(dimensions)
  101 
  102         log_meta = {
  103             'region': region,
  104             'tenantId': tenant_id
  105         }
  106 
  107         return cls(log, log_meta)
  108 
  109     @property
  110     def log(self):
  111         return self.get('log', None)
  112 
  113     @property
  114     def creation_time(self):
  115         return self.get('creation_time', None)
  116 
  117     @property
  118     def meta(self):
  119         return self.get('meta', None)