Merge "add ability to dispatch events to http target"

This commit is contained in:
Jenkins 2015-03-18 21:14:45 +00:00 committed by Gerrit Code Review
commit 523e05378c
2 changed files with 73 additions and 2 deletions

View File

@ -18,7 +18,7 @@ from oslo_config import cfg
import requests
from ceilometer import dispatcher
from ceilometer.i18n import _
from ceilometer.i18n import _, _LE
from ceilometer.openstack.common import log
from ceilometer.publisher import utils as publisher_utils
@ -30,6 +30,10 @@ http_dispatcher_opts = [
help='The target where the http request will be sent. '
'If this is not set, no data will be posted. For '
'example: target = http://hostname:1234/path'),
cfg.StrOpt('event_target',
help='The target for event data where the http request '
'will be sent to. If this is not set, it will default '
'to same as Sample target.'),
cfg.BoolOpt('cadf_only',
default=False,
help='The flag that indicates if only cadf message should '
@ -55,6 +59,7 @@ class HttpDispatcher(dispatcher.Base):
[dispatcher_http]
target = www.example.com
event_target = www.example.com
cadf_only = true
timeout = 2
"""
@ -63,6 +68,8 @@ class HttpDispatcher(dispatcher.Base):
self.headers = {'Content-type': 'application/json'}
self.timeout = self.conf.dispatcher_http.timeout
self.target = self.conf.dispatcher_http.target
self.event_target = (self.conf.dispatcher_http.event_target or
self.target)
self.cadf_only = self.conf.dispatcher_http.cadf_only
def record_metering_data(self, data):
@ -114,4 +121,18 @@ class HttpDispatcher(dispatcher.Base):
meter)
def record_events(self, events):
pass
if not isinstance(events, list):
events = [events]
for event in events:
res = None
try:
res = requests.post(self.event_target, data=event.serialize(),
headers=self.headers, timeout=self.timeout)
res.raise_for_status()
except Exception:
error_code = res.status_code if res else 'unknown'
LOG.exception(_LE('Status Code: %{code}s. Failed to dispatch '
'event: %{event}s'),
{'code': error_code,
'event': event.serialize()})

View File

@ -13,12 +13,16 @@
# License for the specific language governing permissions and limitations
# under the License.
import datetime
import uuid
import mock
from oslo_config import fixture as fixture_config
from oslotest import base
import requests
from ceilometer.dispatcher import http
from ceilometer.event.storage import models as event_models
from ceilometer.publisher import utils
@ -118,3 +122,49 @@ class TestDispatcherHttp(base.BaseTestCase):
dispatcher.record_metering_data(self.msg)
self.assertEqual(1, post.call_count)
class TestEventDispatcherHttp(base.BaseTestCase):
def setUp(self):
super(TestEventDispatcherHttp, self).setUp()
self.CONF = self.useFixture(fixture_config.Config()).conf
def test_http_dispatcher(self):
self.CONF.dispatcher_http.event_target = 'fake'
dispatcher = http.HttpDispatcher(self.CONF)
event = event_models.Event(uuid.uuid4(), 'test',
datetime.datetime(2012, 7, 2, 13, 53, 40),
[], {})
with mock.patch.object(requests, 'post') as post:
dispatcher.record_events(event)
self.assertEqual(1, post.call_count)
def test_http_dispatcher_bad(self):
self.CONF.dispatcher_http.event_target = ''
dispatcher = http.HttpDispatcher(self.CONF)
event = event_models.Event(uuid.uuid4(), 'test',
datetime.datetime(2012, 7, 2, 13, 53, 40),
[], {})
with mock.patch('ceilometer.dispatcher.http.LOG',
mock.MagicMock()) as LOG:
dispatcher.record_events(event)
self.assertTrue(LOG.exception.called)
def test_http_dispatcher_share_target(self):
self.CONF.dispatcher_http.target = 'fake'
dispatcher = http.HttpDispatcher(self.CONF)
event = event_models.Event(uuid.uuid4(), 'test',
datetime.datetime(2012, 7, 2, 13, 53, 40),
[], {})
with mock.patch.object(requests, 'post') as post:
dispatcher.record_events(event)
self.assertEqual('fake', post.call_args[0][0])