Add DEFAULT_ARCHIVE_POLICY config option for gnocchi

Added a DEFAULT_ARCHIVE_POLICY option to Gnocchi plugin,
The user can now configure the archive policy that the metrics
should use.

Change-Id: I445c4bc98b6431a39cf805a00b6237d8090a0b98
This commit is contained in:
Emma Foley 2018-03-14 14:26:29 +00:00
parent 7f1134a1cb
commit c54afa1628
7 changed files with 21 additions and 8 deletions

View File

@ -59,6 +59,8 @@ class Config(object):
CfgParam('OS_TENANT_NAME', None, six.text_type),
CfgParam('VERBOSE', False, bool),
CfgParam('LIBVIRT_METER_ENABLED', False, bool),
# Gnocchi only
CfgParam('DEFAULT_ARCHIVE_POLICY', None, six.text_type),
CfgParam('LIBVIRT_CONN_URI', 'qemu:///system', six.text_type),
]

View File

@ -30,13 +30,14 @@ ROOT_LOGGER = logging.getLogger(collectd_openstack.__name__)
class Sender(common_sender.Sender):
"""Sends the JSON serialized data to Gnocchi"""
def __init__(self):
def __init__(self, config):
"""Create the Sender instance
The cofinguration must be initialized before the object is created.
"""
super(Sender, self).__init__()
self._meter_ids = {}
self._config = config
def _on_authenticated(self):
# get the uri of service endpoint
@ -100,9 +101,13 @@ class Sender(common_sender.Sender):
def _create_metric(self, metername, endpoint, unit):
url = "{}/v1/metric/".format(endpoint)
payload = json.dumps({"name": metername,
"unit": unit,
})
data = {"name": metername,
"unit": unit,
}
if self._config.DEFAULT_ARCHIVE_POLICY:
data["archive_policy_name"] = self._config.DEFAULT_ARCHIVE_POLICY
payload = json.dumps(data)
result = self._perform_request(url, payload, self._auth_token)
metric_id = json.loads(result.text)['id']
LOGGER.debug("metric_id=%s", metric_id)

View File

@ -44,7 +44,7 @@ class Writer(object):
def __init__(self, meters, config):
self._meters = meters
self._samples = SampleContainer()
self._sender = gnocchi_sender.Sender()
self._sender = gnocchi_sender.Sender(config)
self._config = config
def write(self, vl, data):

View File

@ -165,7 +165,8 @@ class TestPlugin(unittest.TestCase):
CEILOMETER_TIMEOUT=1000,
OS_USERNAME='tester',
OS_PASSWORD='testpasswd',
OS_TENANT_NAME='service')
OS_TENANT_NAME='service',
DEFAULT_ARCHIVE_POLICY='')
@mock.patch.object(plugin, 'Plugin', autospec=True)
@mock.patch.object(plugin, 'Config', autospec=True)

View File

@ -87,6 +87,7 @@ class TestConfig(TestCase):
OS_USERNAME='tester',
OS_PASSWORD='testpasswd',
OS_TENANT_NAME='service',
DEFAULT_ARCHIVE_POLICY='',
LIBVIRT_METER_ENABLED=False)
@mock.patch.object(settings, 'LOGGER', autospec=True)

View File

@ -253,7 +253,7 @@ class TestPlugin(unittest.TestCase):
@mock.patch.object(common_sender.Sender, '_perform_request', spec=callable)
@mock.patch.object(common_sender, 'ClientV3', autospec=True)
@mock_collectd()
@mock_config()
@mock_config(DEFAULT_ARCHIVE_POLICY='')
@mock_value()
def test_request_error(
self, data, config, collectd, ClientV3, perf_req):

View File

@ -24,11 +24,15 @@ import unittest
from collectd_openstack.gnocchi import sender as gnocchi_sender
class MockedConfig(object):
DEFAULT_ARCHIVE_POLICY = ''
class TestGnocchiSender(unittest.TestCase):
"""Test the sender class."""
def setUp(self):
self.sender = gnocchi_sender.Sender()
self.sender = gnocchi_sender.Sender(config=MockedConfig)
self.sender._url_base = \
"http://my-endpoint/v1/metric/%s/measures"