Merge "Expose vm's metadata to metrics"

This commit is contained in:
Jenkins 2015-01-19 10:44:10 +00:00 committed by Gerrit Code Review
commit 0978b918e7
4 changed files with 61 additions and 1 deletions

View File

@ -19,6 +19,9 @@ from oslo_config import cfg
import six
# Below config is for collecting metadata which user defined in nova or else,
# and then storing it to Sample for future use according to user's requirement.
# Such as using it as OpenTSDB tags for metrics.
OPTS = [
cfg.ListOpt('reserved_metadata_namespace',
default=['metering.'],
@ -26,6 +29,11 @@ OPTS = [
cfg.IntOpt('reserved_metadata_length',
default=256,
help='Limit on length of reserved metadata values.'),
cfg.ListOpt('reserved_metadata_keys',
default=[],
help='List of metadata keys reserved for metering use. And '
'these keys are additional to the ones included in the '
'namespace.'),
]
cfg.CONF.register_opts(OPTS)
@ -43,6 +51,17 @@ def add_reserved_user_metadata(src_metadata, dest_metadata):
k[len(prefix):].replace('.', '_') not in dest_metadata)
)
user_metadata.update(md)
for metadata_key in cfg.CONF.reserved_metadata_keys:
md = dict(
(k.replace('.', '_'),
v[:limit] if isinstance(v, six.string_types) else v)
for k, v in src_metadata.items()
if (k == metadata_key and
k.replace('.', '_') not in dest_metadata)
)
user_metadata.update(md)
if user_metadata:
dest_metadata['user_metadata'] = user_metadata

View File

@ -37,6 +37,10 @@ class TestPollsterBase(base.BaseTestCase):
self.instance.flavor = {'name': 'm1.small', 'id': 2, 'vcpus': 1,
'ram': 512, 'disk': 20, 'ephemeral': 0}
self.instance.status = 'active'
self.instance.metadata = {
'fqdn': 'vm_fqdn',
'metering.stack': '2cadc4b4-8789-123c-b4eg-edd2f0a9c128',
'project_cos': 'dev'}
patch_virt = mockpatch.Patch(
'ceilometer.compute.virt.inspector.get_hypervisor_inspector',

View File

@ -18,6 +18,7 @@
# under the License.
import mock
from oslo.config import fixture as fixture_config
from ceilometer.agent import manager
from ceilometer.compute.pollsters import instance as pollsters_instance
@ -50,3 +51,30 @@ class TestInstancePollster(base.TestPollsterBase):
samples = list(pollster.get_samples(mgr, {}, [self.instance]))
self.assertEqual(1, len(samples))
self.assertEqual('instance:m1.small', samples[0].name)
@mock.patch('ceilometer.pipeline.setup_pipeline', mock.MagicMock())
def test_get_reserved_metadata_with_keys(self):
self.CONF = self.useFixture(fixture_config.Config()).conf
self.CONF.set_override('reserved_metadata_keys', ['fqdn'])
mgr = manager.AgentManager()
pollster = pollsters_instance.InstancePollster()
samples = list(pollster.get_samples(mgr, {}, [self.instance]))
self.assertEqual({'fqdn': 'vm_fqdn',
'stack': '2cadc4b4-8789-123c-b4eg-edd2f0a9c128'},
samples[0].resource_metadata['user_metadata'])
@mock.patch('ceilometer.pipeline.setup_pipeline', mock.MagicMock())
def test_get_reserved_metadata_with_namespace(self):
mgr = manager.AgentManager()
pollster = pollsters_instance.InstancePollster()
samples = list(pollster.get_samples(mgr, {}, [self.instance]))
self.assertEqual({'stack': '2cadc4b4-8789-123c-b4eg-edd2f0a9c128'},
samples[0].resource_metadata['user_metadata'])
self.CONF = self.useFixture(fixture_config.Config()).conf
self.CONF.set_override('reserved_metadata_namespace', [])
mgr = manager.AgentManager()
pollster = pollsters_instance.InstancePollster()
samples = list(pollster.get_samples(mgr, {}, [self.instance]))
self.assertNotIn('user_metadata', samples[0].resource_metadata)

View File

@ -528,12 +528,21 @@ User-defined sample metadata for Nova
Users are allowed to add additional metadata to samples of nova meter.
These additional metadata are stored in 'resource_metadata.user_metadata.*' of the sample.
To do so, users should add nova user metadata prefixed with 'metering.':
To do so, users can add nova user metadata prefixed with 'metering.':
::
$ nova boot --meta metering.custom_metadata=a_value my_vm
Or users can define metadata keys they cared without any prefix in ceilometer.conf.
For example,if users need to add "fqdn" of metadata to samples,
they can add or modify as below into ceilometer.conf in [DEFAULT] group:
::
reserved_metadata_keys=fqdn
Note: The name of the metadata shouldn't exceed 256 characters otherwise it will be cut off.
Also, if it has '.', this will be replaced by a '_' in ceilometer.