Files
monasca-api/monasca_api/common/messaging/message_formats/metrics.py
Joe Keen 08c200b6a8 Wrapping single metrics in a list on post
For efficiency we're using the Kafka clients send_messages function which takes
a variable number of messages to send and batches them so that they can be read
one at a time.  To do this we use the * operator to expand the list of metrics
as we pass them to the send_messages function.  If the input data is not a list
this doesn't work so even single metrics need to be bundled in a list.

Change-Id: I2f196b37db0db95a5163696f14258a865fa9a615
2015-06-19 17:04:13 -06:00

34 lines
1.2 KiB
Python

# Copyright 2014 Hewlett-Packard
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import json
from oslo_utils import timeutils
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(json.dumps(transformed_metric))
return transformed_metrics
else:
transformed_metric['metric'] = metrics
return [json.dumps(transformed_metric)]