Jump to: navigation, search

Difference between revisions of "Trove/trove-notifications-v2"

(Task Manager Events)
Line 14: Line 14:
 
         This adds the ability to send usage events to an Instance object.
 
         This adds the ability to send usage events to an Instance object.
 
         """
 
         """
        def send_usage_event(self, event_type, **kwargs):
+
  def send_stack_usage_even(self, event_type, **kwargs):
            event_type = 'trove.instance.%s' % event_type
+
        az = kwargs.pop('az')
            publisher_id = CONF.host
+
        name = kwargs.pop('name')
            # Grab the instance size from the kwargs or from the nova client
+
        event_type = 'trove.instance.%s' % event_type
            instance_size = kwargs.pop('instance_size', None)
+
        publisher_id = CONF.host
            if instance_size is None:
+
        created_time = timeutils.isotime(self.db_info.created)
                flavor = self.nova_client.flavors.get(self.flavor_id)
+
        compute_instance_id = kwargs.pop('compute_instance_id')
                instance_size = flavor.ram
+
        payload = {'availability_zone': az,
            # Default payload
+
                          'created_at': created_time,
            created_time = timeutils.isotime(self.db_info.created)
+
                          'name': name,
            payload = {
+
                          'instance_id': self.id,
                'instance_size': instance_size,
+
                          'instance_name': name,
                'tenant_id': self.tenant_id,
+
                          'flavor_name': self.new_flavor['name'],
                'instance_id': self.id,
+
                          'flavor_id': self.new_flavor['id'],
                'instance_name': self.name,
+
                          'launched_at': created_time,
                'created_at': created_time,
+
                          'compute_instance_id': compute_instance_id,
                'launched_at': created_time,
+
                          'region': CONF.region,
                'nova_instance_id': self.server_id,
+
                          'state_description': self.status.description,
            }
+
                          'state': self.status,
            if CONF.trove_volume_support:
+
                          'tenant_id': self.tenant_id,
                payload.update({
+
                          'user_id': self.context.user,
                    'volume_size': self.volume_size,
+
                          'service_id': self._get_service_id(self.datastore_version.manager, CONF.notification_service_id)}
                    'nova_volume_id': self.volume_id
+
 
                })
+
        if CONF.trove_volume_support:
            # Update payload with all other kwargs
+
                  payload.update({
            payload.update(kwargs)
+
                          'volume_size': self.db_info.volume_size,
            notifier.notify(self.context, publisher_id, event_type, 'INFO',
+
                          'volume_id': self.db_info.volume_id
                                payload)
+
                  })
 +
 +
        # Update payload with all other kwargs
 +
        payload.update(kwargs)
 +
        LOG.debug(_('Sending event: %(event_type)s, %(payload)s') %
 +
        {'event_type': event_type, 'payload': payload})
 +
        notifier.notify(self.context, publisher_id, event_type, 'INFO', payload)
 
</pre>
 
</pre>
  

Revision as of 10:16, 23 May 2014

Trove Notifications

Trove will emit events for resources as they are manipulated. These events can be used to meter the service and possibly used to calculate bills.

Task Manager Events

Trove taskmanager is responsible for dispatching tasks and sending notification events. Here is a typical example:

    from trove.openstack.common.notifier import api as notifier
    from trove.openstack.common import timeutils
    class NotifyMixin(object):
        """Notification Mixin
        This adds the ability to send usage events to an Instance object.
         """
  def send_stack_usage_even(self, event_type, **kwargs):
         az = kwargs.pop('az')
         name = kwargs.pop('name')
         event_type = 'trove.instance.%s' % event_type
         publisher_id = CONF.host
         created_time = timeutils.isotime(self.db_info.created)
         compute_instance_id = kwargs.pop('compute_instance_id')
         payload = {'availability_zone': az,
                           'created_at': created_time,
                           'name': name,
                           'instance_id': self.id,
                           'instance_name': name,
                           'flavor_name': self.new_flavor['name'],
                           'flavor_id': self.new_flavor['id'],
                           'launched_at': created_time,
                           'compute_instance_id': compute_instance_id,
                           'region': CONF.region,
                           'state_description': self.status.description,
                           'state': self.status,
                           'tenant_id': self.tenant_id,
                           'user_id': self.context.user,
                           'service_id': self._get_service_id(self.datastore_version.manager, CONF.notification_service_id)}

         if CONF.trove_volume_support:
                  payload.update({
                           'volume_size': self.db_info.volume_size,
                           'volume_id': self.db_info.volume_id
                  })
 
         # Update payload with all other kwargs
         payload.update(kwargs)
         LOG.debug(_('Sending event: %(event_type)s, %(payload)s') %
         {'event_type': event_type, 'payload': payload})
         notifier.notify(self.context, publisher_id, event_type, 'INFO', payload)


Create Event

Create Events are emitted when a resource is created:

self.send_usage_event('create', instance_size=flavor_ram)

Sample Message:

{
      'event_type': 'trove.instance.create',
      'instance_size': 512,
      'tenant_id': <UUID>,
      'instance_id': <UUID>,
      'instance_name': 'MyDB,
      'created_at': UTCDATE,
      'launched_at': UTCDATE,
      'nova_instance_id': <UUID>,
      'volume_size': 1,
      'nova_volume_id': <UUID>
}

Modify Volume Size

self.send_usage_event('modify_volume', launched_at=timeutils.isotime(), modify_at=timeutils.isotime(), volume_size=new_size)

Sample::

{
      'event_type': 'trove.instance.modify_volume',
      'instance_size': 512,
      'tenant_id': <UUID>,
      'instance_id': <UUID>,
      'instance_name': 'MyDB,
      'created_at': UTCDATE,
      'launched_at': UTCDATE,
      'modify_at': UTCDATE,
      'nova_instance_id': <UUID>,
      'volume_size': 2,
      'nova_volume_id': <UUID>
}

Modify Size (Resize)

self.instance.send_usage_event('modify_flavor', instance_size=self.new_flavor['ram'], launched_at=self.db_info.created_at, modify_at=self.db_info.modified_at)

Sample::

{
      'event_type': 'trove.instance.modify_flavor',
      'instance_ram': 1024,
      'tenant_id': <UUID>,
      'instance_id': <UUID>,
      'instance_name': 'MyDB,
      'created_at': UTCDATE,
      'launched_at': UTCDATE,
      'modify_at': UTCDATE,
      'compute_instance_id': <UUID>,
      'volume_size': 2,
      'volume_id': <UUID>
}
QESTION: Do we really need to send AZ inside each notification?


Delete Event

Delete Events are emitted when a resource is deleted:

self.send_usage_event('delete', deleted_at=self.db_info.deleted_at)

Sample:

{
      'event_type': 'trove.instance.delete',
      'instance_ram': 1024,
      'tenant_id': <UUID>,
      'instance_id': <UUID>,
      'instance_name': 'MyDB,
      'created_at': UTCDATE,
      'launched_at': UTCDATE,
      'deleted_at': UTCDATE,
      'compute_instance_id': <UUID>,
      'volume_size': 2,
      'volume_id': <UUID>
}

Exists Event

Exists Events are emitted periodically signifying that the resource exists.

Sample:

{     
      'event_type': 'trove.instance.exists',
      'instance_ram': 512,
      'tenant_id': <UUID>,
      'instance_id': <UUID>,
      'instance_name': 'MyDB',
      'created_at': UTCDATE,
      'launched_at': UTCDATE,
      'compute_instance_id': <UUID>,
      'volume_size': 1,
      'volume_id': <UUID>
}