From 1defcfb65c23ea7399573d052785d8e32a541b3b Mon Sep 17 00:00:00 2001 From: Emma Foley Date: Wed, 14 Mar 2018 14:26:29 +0000 Subject: [PATCH] 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 --- collectd_ceilometer/common/settings.py | 2 ++ collectd_ceilometer/gnocchi/sender.py | 13 +++++++++---- collectd_ceilometer/gnocchi/writer.py | 2 +- collectd_ceilometer/tests/aodh/test_plugin.py | 3 ++- collectd_ceilometer/tests/common/test_config.py | 1 + collectd_ceilometer/tests/gnocchi/test_plugin.py | 2 +- 6 files changed, 16 insertions(+), 7 deletions(-) diff --git a/collectd_ceilometer/common/settings.py b/collectd_ceilometer/common/settings.py index b0d3a6b..4388dc4 100644 --- a/collectd_ceilometer/common/settings.py +++ b/collectd_ceilometer/common/settings.py @@ -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), ] diff --git a/collectd_ceilometer/gnocchi/sender.py b/collectd_ceilometer/gnocchi/sender.py index c77a0de..b28df9d 100644 --- a/collectd_ceilometer/gnocchi/sender.py +++ b/collectd_ceilometer/gnocchi/sender.py @@ -30,13 +30,14 @@ ROOT_LOGGER = logging.getLogger(collectd_ceilometer.__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) diff --git a/collectd_ceilometer/gnocchi/writer.py b/collectd_ceilometer/gnocchi/writer.py index 25cf424..e41a91a 100644 --- a/collectd_ceilometer/gnocchi/writer.py +++ b/collectd_ceilometer/gnocchi/writer.py @@ -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): diff --git a/collectd_ceilometer/tests/aodh/test_plugin.py b/collectd_ceilometer/tests/aodh/test_plugin.py index eb74f6a..458f85c 100644 --- a/collectd_ceilometer/tests/aodh/test_plugin.py +++ b/collectd_ceilometer/tests/aodh/test_plugin.py @@ -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) diff --git a/collectd_ceilometer/tests/common/test_config.py b/collectd_ceilometer/tests/common/test_config.py index 8d9343b..94322a8 100644 --- a/collectd_ceilometer/tests/common/test_config.py +++ b/collectd_ceilometer/tests/common/test_config.py @@ -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) diff --git a/collectd_ceilometer/tests/gnocchi/test_plugin.py b/collectd_ceilometer/tests/gnocchi/test_plugin.py index 9a3aec9..d9aad3d 100644 --- a/collectd_ceilometer/tests/gnocchi/test_plugin.py +++ b/collectd_ceilometer/tests/gnocchi/test_plugin.py @@ -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):