"Fossies" - the Fresh Open Source Software Archive

Member "monasca-api-4.0.0/monasca_api/v2/reference/metrics.py" (13 May 2020, 16077 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 "metrics.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 # (C) Copyright 2014-2017 Hewlett Packard Enterprise Development LP
    2 # Copyright 2018 OP5 AB
    3 #
    4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
    5 # not use this file except in compliance with the License. You may obtain
    6 # a copy of the License at
    7 #
    8 # http://www.apache.org/licenses/LICENSE-2.0
    9 #
   10 # Unless required by applicable law or agreed to in writing, software
   11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
   12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   13 # License for the specific language governing permissions and limitations
   14 # under the License.
   15 
   16 import falcon
   17 from monasca_common.simport import simport
   18 from monasca_common.validation import metrics as metric_validation
   19 from oslo_config import cfg
   20 from oslo_log import log
   21 
   22 from monasca_api.api import metrics_api_v2
   23 from monasca_api.common.messaging import (
   24     exceptions as message_queue_exceptions)
   25 from monasca_api.common.messaging.message_formats import (
   26     metrics as metrics_message)
   27 from monasca_api.v2.common.exceptions import HTTPUnprocessableEntityError
   28 from monasca_api.v2.reference import helpers
   29 from monasca_api.v2.reference import resource
   30 
   31 LOG = log.getLogger(__name__)
   32 
   33 
   34 def get_merge_metrics_flag(req):
   35     '''Return the value of the optional metrics_flag
   36 
   37     Returns False if merge_metrics parameter is not supplied or is not a
   38     string that evaluates to True, otherwise True
   39     '''
   40 
   41     merge_metrics_flag = helpers.get_query_param(req,
   42                                                  'merge_metrics',
   43                                                  False,
   44                                                  False)
   45     if merge_metrics_flag is not False:
   46         return helpers.str_2_bool(merge_metrics_flag)
   47     else:
   48         return False
   49 
   50 
   51 class Metrics(metrics_api_v2.MetricsV2API):
   52     def __init__(self):
   53         try:
   54             super(Metrics, self).__init__()
   55             self._region = cfg.CONF.region
   56             self._message_queue = simport.load(cfg.CONF.messaging.driver)(
   57                 'metrics')
   58             self._metrics_repo = simport.load(
   59                 cfg.CONF.repositories.metrics_driver)()
   60             self._batch_size = cfg.CONF.kafka.queue_buffering_max_messages
   61 
   62         except Exception as ex:
   63             LOG.exception(ex)
   64             raise falcon.HTTPInternalServerError('Service unavailable',
   65                                                  str(ex))
   66 
   67     def _send_metrics(self, metrics):
   68         try:
   69             for i in range(0, len(metrics), self._batch_size):
   70                 batch = metrics[i:i + self._batch_size]
   71                 self._message_queue.send_message(batch)
   72         except message_queue_exceptions.MessageQueueException as ex:
   73             LOG.exception(ex)
   74             raise falcon.HTTPServiceUnavailable('Service unavailable',
   75                                                 str(ex), 60)
   76 
   77     def _list_metrics(self, tenant_id, name, dimensions, req_uri, offset,
   78                       limit, start_timestamp, end_timestamp):
   79 
   80         result = self._metrics_repo.list_metrics(tenant_id,
   81                                                  self._region,
   82                                                  name,
   83                                                  dimensions,
   84                                                  offset, limit,
   85                                                  start_timestamp,
   86                                                  end_timestamp)
   87 
   88         return helpers.paginate(result, req_uri, limit)
   89 
   90     @resource.resource_try_catch_block
   91     def on_post(self, req, res):
   92         helpers.validate_json_content_type(req)
   93         helpers.validate_authorization(req, ['api:metrics:post'])
   94         metrics = helpers.from_json(req)
   95         try:
   96             metric_validation.validate(metrics)
   97         except Exception as ex:
   98             LOG.exception(ex)
   99             raise HTTPUnprocessableEntityError("Unprocessable Entity", str(ex))
  100 
  101         tenant_id = helpers.get_x_tenant_or_tenant_id(req, ['api:delegate'])
  102         transformed_metrics = metrics_message.transform(
  103             metrics, tenant_id, self._region)
  104         self._send_metrics(transformed_metrics)
  105         res.status = falcon.HTTP_204
  106 
  107     @resource.resource_try_catch_block
  108     def on_get(self, req, res):
  109         helpers.validate_authorization(req, ['api:metrics:get'])
  110         tenant_id = helpers.get_x_tenant_or_tenant_id(req, ['api:delegate'])
  111         name = helpers.get_query_name(req)
  112         helpers.validate_query_name(name)
  113         dimensions = helpers.get_query_dimensions(req)
  114         helpers.validate_query_dimensions(dimensions)
  115         offset = helpers.get_query_param(req, 'offset')
  116         start_timestamp = helpers.get_query_starttime_timestamp(req, False)
  117         end_timestamp = helpers.get_query_endtime_timestamp(req, False)
  118         helpers.validate_start_end_timestamps(start_timestamp, end_timestamp)
  119         result = self._list_metrics(tenant_id, name,
  120                                     dimensions, req.uri,
  121                                     offset, req.limit,
  122                                     start_timestamp, end_timestamp)
  123         res.body = helpers.to_json(result)
  124         res.status = falcon.HTTP_200
  125 
  126 
  127 class MetricsMeasurements(metrics_api_v2.MetricsMeasurementsV2API):
  128     def __init__(self):
  129         try:
  130             super(MetricsMeasurements, self).__init__()
  131             self._region = cfg.CONF.region
  132             self._metrics_repo = simport.load(
  133                 cfg.CONF.repositories.metrics_driver)()
  134 
  135         except Exception as ex:
  136             LOG.exception(ex)
  137             raise falcon.HTTPInternalServerError('Service unavailable',
  138                                                  str(ex))
  139 
  140     @resource.resource_try_catch_block
  141     def on_get(self, req, res):
  142         helpers.validate_authorization(req, ['api:metrics:get'])
  143         tenant_id = helpers.get_x_tenant_or_tenant_id(req, ['api:delegate'])
  144         name = helpers.get_query_name(req, True)
  145         helpers.validate_query_name(name)
  146         dimensions = helpers.get_query_dimensions(req)
  147         helpers.validate_query_dimensions(dimensions)
  148         start_timestamp = helpers.get_query_starttime_timestamp(req)
  149         end_timestamp = helpers.get_query_endtime_timestamp(req, False)
  150         helpers.validate_start_end_timestamps(start_timestamp, end_timestamp)
  151         offset = helpers.get_query_param(req, 'offset')
  152         merge_metrics_flag = get_merge_metrics_flag(req)
  153         group_by = helpers.get_query_group_by(req)
  154 
  155         result = self._measurement_list(tenant_id, name, dimensions,
  156                                         start_timestamp, end_timestamp,
  157                                         req.uri, offset,
  158                                         req.limit, merge_metrics_flag,
  159                                         group_by)
  160 
  161         res.body = helpers.to_json(result)
  162         res.status = falcon.HTTP_200
  163 
  164     def _measurement_list(self, tenant_id, name, dimensions, start_timestamp,
  165                           end_timestamp, req_uri, offset,
  166                           limit, merge_metrics_flag, group_by):
  167 
  168         result = self._metrics_repo.measurement_list(tenant_id,
  169                                                      self._region,
  170                                                      name,
  171                                                      dimensions,
  172                                                      start_timestamp,
  173                                                      end_timestamp,
  174                                                      offset,
  175                                                      limit,
  176                                                      merge_metrics_flag,
  177                                                      group_by)
  178 
  179         return helpers.paginate_measurements(result, req_uri, limit)
  180 
  181 
  182 class MetricsStatistics(metrics_api_v2.MetricsStatisticsV2API):
  183     def __init__(self):
  184         try:
  185             super(MetricsStatistics, self).__init__()
  186             self._region = cfg.CONF.region
  187             self._metrics_repo = simport.load(
  188                 cfg.CONF.repositories.metrics_driver)()
  189 
  190         except Exception as ex:
  191             LOG.exception(ex)
  192             raise falcon.HTTPInternalServerError('Service unavailable',
  193                                                  str(ex))
  194 
  195     @resource.resource_try_catch_block
  196     def on_get(self, req, res):
  197         helpers.validate_authorization(req, ['api:metrics:get'])
  198         tenant_id = helpers.get_x_tenant_or_tenant_id(req, ['api:delegate'])
  199         name = helpers.get_query_name(req, True)
  200         helpers.validate_query_name(name)
  201         dimensions = helpers.get_query_dimensions(req)
  202         helpers.validate_query_dimensions(dimensions)
  203         start_timestamp = helpers.get_query_starttime_timestamp(req)
  204         end_timestamp = helpers.get_query_endtime_timestamp(req, False)
  205         helpers.validate_start_end_timestamps(start_timestamp, end_timestamp)
  206         statistics = helpers.get_query_statistics(req)
  207         period = helpers.get_query_period(req)
  208         offset = helpers.get_query_param(req, 'offset')
  209         merge_metrics_flag = get_merge_metrics_flag(req)
  210         group_by = helpers.get_query_group_by(req)
  211 
  212         result = self._metric_statistics(tenant_id, name, dimensions,
  213                                          start_timestamp, end_timestamp,
  214                                          statistics, period, req.uri,
  215                                          offset, req.limit, merge_metrics_flag,
  216                                          group_by)
  217 
  218         res.body = helpers.to_json(result)
  219         res.status = falcon.HTTP_200
  220 
  221     def _metric_statistics(self, tenant_id, name, dimensions, start_timestamp,
  222                            end_timestamp, statistics, period, req_uri,
  223                            offset, limit, merge_metrics_flag, group_by):
  224 
  225         result = self._metrics_repo.metrics_statistics(tenant_id,
  226                                                        self._region,
  227                                                        name,
  228                                                        dimensions,
  229                                                        start_timestamp,
  230                                                        end_timestamp,
  231                                                        statistics, period,
  232                                                        offset,
  233                                                        limit,
  234                                                        merge_metrics_flag,
  235                                                        group_by)
  236 
  237         return helpers.paginate_statistics(result, req_uri, limit)
  238 
  239 
  240 class MetricsNames(metrics_api_v2.MetricsNamesV2API):
  241     def __init__(self):
  242         try:
  243             super(MetricsNames, self).__init__()
  244             self._region = cfg.CONF.region
  245             self._metrics_repo = simport.load(
  246                 cfg.CONF.repositories.metrics_driver)()
  247 
  248         except Exception as ex:
  249             LOG.exception(ex)
  250             raise falcon.HTTPInternalServerError('Service unavailable',
  251                                                  str(ex))
  252 
  253     @resource.resource_try_catch_block
  254     def on_get(self, req, res):
  255         helpers.validate_authorization(req, ['api:metrics:get'])
  256         tenant_id = helpers.get_x_tenant_or_tenant_id(req, ['api:delegate'])
  257         dimensions = helpers.get_query_dimensions(req)
  258         helpers.validate_query_dimensions(dimensions)
  259         offset = helpers.get_query_param(req, 'offset')
  260         result = self._list_metric_names(tenant_id, dimensions,
  261                                          req.uri, offset, req.limit)
  262         res.body = helpers.to_json(result)
  263         res.status = falcon.HTTP_200
  264 
  265     def _list_metric_names(self, tenant_id, dimensions, req_uri, offset,
  266                            limit):
  267 
  268         result = self._metrics_repo.list_metric_names(tenant_id,
  269                                                       self._region,
  270                                                       dimensions)
  271 
  272         return helpers.paginate_with_no_id(result, req_uri, offset, limit)
  273 
  274 
  275 class DimensionValues(metrics_api_v2.DimensionValuesV2API):
  276     def __init__(self):
  277         try:
  278             super(DimensionValues, self).__init__()
  279             self._region = cfg.CONF.region
  280             self._metrics_repo = simport.load(
  281                 cfg.CONF.repositories.metrics_driver)()
  282 
  283         except Exception as ex:
  284             LOG.exception(ex)
  285             raise falcon.HTTPInternalServerError('Service unavailable', str(ex))
  286 
  287     @resource.resource_try_catch_block
  288     def on_get(self, req, res):
  289         helpers.validate_authorization(req, ['api:metrics:dimension:values'])
  290         tenant_id = helpers.get_x_tenant_or_tenant_id(req, ['api:delegate'])
  291         metric_name = helpers.get_query_param(req, 'metric_name')
  292         dimension_name = helpers.get_query_param(req, 'dimension_name',
  293                                                  required=True)
  294         offset = helpers.get_query_param(req, 'offset')
  295         start_timestamp = helpers.get_query_starttime_timestamp(req, False)
  296         end_timestamp = helpers.get_query_endtime_timestamp(req, False)
  297         result = self._dimension_values(tenant_id, req.uri, metric_name,
  298                                         dimension_name, offset, req.limit,
  299                                         start_timestamp, end_timestamp)
  300         res.body = helpers.to_json(result)
  301         res.status = falcon.HTTP_200
  302 
  303     def _dimension_values(self, tenant_id, req_uri, metric_name,
  304                           dimension_name, offset, limit, start_timestamp,
  305                           end_timestamp):
  306 
  307         result = self._metrics_repo.list_dimension_values(tenant_id,
  308                                                           self._region,
  309                                                           metric_name,
  310                                                           dimension_name,
  311                                                           start_timestamp,
  312                                                           end_timestamp)
  313 
  314         return helpers.paginate_with_no_id(result, req_uri, offset, limit)
  315 
  316 
  317 class DimensionNames(metrics_api_v2.DimensionNamesV2API):
  318     def __init__(self):
  319         try:
  320             super(DimensionNames, self).__init__()
  321             self._region = cfg.CONF.region
  322             self._metrics_repo = simport.load(
  323                 cfg.CONF.repositories.metrics_driver)()
  324 
  325         except Exception as ex:
  326             LOG.exception(ex)
  327             raise falcon.HTTPInternalServerError('Service unavailable',
  328                                                  str(ex))
  329 
  330     @resource.resource_try_catch_block
  331     def on_get(self, req, res):
  332         helpers.validate_authorization(req, ['api:metrics:dimension:names'])
  333         tenant_id = helpers.get_x_tenant_or_tenant_id(req, ['api:delegate'])
  334         metric_name = helpers.get_query_param(req, 'metric_name')
  335         offset = helpers.get_query_param(req, 'offset')
  336         start_timestamp = helpers.get_query_starttime_timestamp(req, False)
  337         end_timestamp = helpers.get_query_endtime_timestamp(req, False)
  338         result = self._dimension_names(tenant_id, req.uri, metric_name,
  339                                        offset, req.limit,
  340                                        start_timestamp, end_timestamp)
  341         res.body = helpers.to_json(result)
  342         res.status = falcon.HTTP_200
  343 
  344     def _dimension_names(self, tenant_id, req_uri, metric_name, offset, limit,
  345                          start_timestamp, end_timestamp):
  346 
  347         result = self._metrics_repo.list_dimension_names(tenant_id,
  348                                                          self._region,
  349                                                          metric_name,
  350                                                          start_timestamp,
  351                                                          end_timestamp)
  352 
  353         return helpers.paginate_with_no_id(result, req_uri, offset, limit)