Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
'metadata_flavor': 'm1.large', 'metadata_event': 'event-2',
'source': 'source-1'},
{'volume': 4, 'user': 'user-2', 'project': 'project-2',
'resource': 'resource-2', 'timestamp': (2013, 8, 1, 17, 28),
'metadata_flavor': 'm1.large', 'metadata_event': 'event-2',
'source': 'source-1'},
{'volume': 4, 'user': 'user-3', 'project': 'project-1',
'resource': 'resource-3', 'timestamp': (2013, 8, 1, 11, 22),
'metadata_flavor': 'm1.tiny', 'metadata_event': 'event-2',
'source': 'source-3'},
)
for test_sample in test_sample_data:
c = sample.Sample(
'instance',
sample.TYPE_CUMULATIVE,
unit='s',
volume=test_sample['volume'],
user_id=test_sample['user'],
project_id=test_sample['project'],
resource_id=test_sample['resource'],
timestamp=datetime.datetime(*test_sample['timestamp']),
resource_metadata={'flavor': test_sample['metadata_flavor'],
'event': test_sample['metadata_event'], },
source=test_sample['source'],
)
msg = rpc.meter_message_from_counter(
c,
self.CONF.publisher_rpc.metering_secret,
)
self.conn.record_metering_data(msg)
def create_and_store_sample(self, timestamp=datetime.datetime.utcnow(),
metadata={
'display_name': 'test-server',
'tag': 'self.counter'
},
name='instance',
sample_type=sample.TYPE_CUMULATIVE, unit='',
volume=1, user_id='user-id',
project_id='project-id',
resource_id='resource-id', source=None):
s = sample.Sample(
name, sample_type, unit=unit, volume=volume, user_id=user_id,
project_id=project_id, resource_id=resource_id,
timestamp=timestamp,
resource_metadata=metadata, source=source
)
msg = rpc.meter_message_from_counter(
s, self.CONF.publisher_rpc.metering_secret
)
self.conn.record_metering_data(msg)
return msg
# because the notifier module messes with the import path to force
# nova's version of oslo to be used instead of ceilometer's.
from ceilometer.compute import nova_notifier
from ceilometer import sample
from ceilometer.compute.pollsters import util
LOG = logging.getLogger(__name__)
nova_CONF = config.cfg.CONF
class TestNovaNotifier(base.BaseTestCase):
class Pollster(object):
instances = []
test_data_1 = sample.Sample(
name='test1',
type=sample.TYPE_CUMULATIVE,
unit='units-go-here',
volume=1,
user_id='test',
project_id='test',
resource_id='test_run_tasks',
timestamp=datetime.datetime.utcnow().isoformat(),
resource_metadata={'name': 'Pollster',
},
)
def get_samples(self, manager, cache, instance):
self.instances.append((manager, instance))
test_data_2 = util.make_sample_from_instance(
instance,
def setUp(self):
super(TestListResourcesBase, self).setUp()
for cnt in [
sample.Sample(
'instance',
'cumulative',
'',
1,
'user-id',
'project-id',
'resource-id',
timestamp=datetime.datetime(2012, 7, 2, 10, 40),
resource_metadata={'display_name': 'test-server',
'tag': 'self.sample'},
source='test_list_resources',
),
sample.Sample(
'instance',
'cumulative',
'',
unit_value = getattr(self, 'unit', self.resource_name)
resource = message['payload'].get(self.resource_name)
if resource:
# NOTE(liusheng): In %s.update.start notifications, the id is in
# message['payload'] instead of resource itself.
if message['event_type'].endswith('update.start'):
resource['id'] = message['payload']['id']
resources = [resource]
else:
resources = message['payload'].get(self.resource_name + 's', [])
resource_message = message.copy()
for resource in resources:
resource_message['payload'] = resource
yield sample.Sample.from_notification(
name=counter_name,
type=sample.TYPE_GAUGE,
unit=unit_value,
volume=1,
user_id=resource_message['_context_user_id'],
project_id=resource_message['_context_tenant_id'],
resource_id=resource['id'],
message=resource_message)
event_type_split = resource_message['event_type'].split('.')
if len(event_type_split) > 2:
yield sample.Sample.from_notification(
name=counter_name
+ "." + event_type_split[1],
type=sample.TYPE_DELTA,
unit=unit_value,
volume=1,
def process_notification(self, message):
meter_name = '.'.join(message['event_type'].split('.')[:-1])
yield sample.Sample.from_notification(
name=meter_name,
type=sample.TYPE_GAUGE,
unit='table',
volume=1,
resource_id=message['payload']['table_uuid'],
user_id=message['_context_user'],
project_id=message['_context_tenant'],
message=message)
def process_notification(self, message):
samples = message['payload']['samples']
for sample_dict in samples:
yield sample.Sample(
name=sample_dict['counter_name'],
type=sample_dict['counter_type'],
unit=sample_dict['counter_unit'],
volume=sample_dict['counter_volume'],
user_id=sample_dict['user_id'],
project_id=sample_dict['project_id'],
resource_id=sample_dict['resource_id'],
timestamp=sample_dict['timestamp'],
resource_metadata=sample_dict['resource_metadata'],
source=sample_dict['source'],
id=sample_dict['message_id'])
def get_samples(self, manager, cache, resources):
for tenant, bucket_info in self._iter_accounts(manager.keystone,
cache, resources):
for it in bucket_info['buckets']:
yield sample.Sample(
name='radosgw.containers.objects.size',
type=sample.TYPE_GAUGE,
volume=int(it.size * 1024),
unit='B',
user_id=None,
project_id=tenant,
resource_id=tenant + '/' + it.name,
resource_metadata=None,
)
def _convert(self, s, growth=1):
"""Transform the appropriate sample fields."""
volume = self._scale(s) * growth
return sample.Sample(
name=self._map(s, 'name'),
unit=self._map(s, 'unit'),
type=self.target.get('type', s.type),
volume=min(volume, self.max) if self.max else volume,
user_id=s.user_id,
project_id=s.project_id,
resource_id=s.resource_id,
timestamp=s.timestamp,
resource_metadata=s.resource_metadata
)
sample = {
'name': self.cfg["name"], 'type': self.cfg["type"],
'unit': self.cfg["unit"], 'volume': None, 'timestamp': None,
'user_id': self._fallback_user_id.parse(message),
'project_id': self._fallback_project_id.parse(message),
'resource_id': None, 'message': message, 'metadata': {},
}
for name, parser in self._metadata_attributes.items():
value = parser.parse(message)
if value:
sample['metadata'][name] = value
if self._user_meta:
meta = self._user_meta.parse(message)
if meta:
sample_util.add_reserved_user_metadata(
self.conf, meta, sample['metadata'])
# NOTE(sileht): We expect multiple samples in the payload
# so put each attribute into a list
if self.lookup:
for name in sample:
sample[name] = [sample[name]]
for name in self.SAMPLE_ATTRIBUTES:
parser = self._attributes.get(name)
if parser is not None:
value = parser.parse(message, bool(self.lookup))
# NOTE(sileht): If we expect multiple samples
# some attributes are overridden even we don't get any
# result. Also note in this case value is always a list
if ((not self.lookup and value is not None) or