Merge "Remove class FileDispatcher"
This commit is contained in:
commit
e86aa05112
@ -1,86 +0,0 @@
|
||||
#
|
||||
# Copyright 2013 IBM Corp
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
# a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import logging
|
||||
import logging.handlers
|
||||
|
||||
from debtcollector import removals
|
||||
from oslo_config import cfg
|
||||
|
||||
from ceilometer import dispatcher
|
||||
|
||||
OPTS = [
|
||||
cfg.StrOpt('file_path',
|
||||
help='Name and the location of the file to record '
|
||||
'meters.'),
|
||||
cfg.IntOpt('max_bytes',
|
||||
default=0,
|
||||
help='The max size of the file.'),
|
||||
cfg.IntOpt('backup_count',
|
||||
default=0,
|
||||
help='The max number of the files to keep.'),
|
||||
]
|
||||
|
||||
|
||||
@removals.removed_class("FileDispatcher", message="Use file publisher instead",
|
||||
removal_version="9.0.0")
|
||||
class FileDispatcher(dispatcher.MeterDispatcherBase,
|
||||
dispatcher.EventDispatcherBase):
|
||||
"""Dispatcher class for recording metering data to a file.
|
||||
|
||||
The dispatcher class which logs each meter and/or event into a file
|
||||
configured in ceilometer configuration file. An example configuration may
|
||||
look like the following:
|
||||
|
||||
[dispatcher_file]
|
||||
file_path = /tmp/meters
|
||||
|
||||
To enable this dispatcher, the following section needs to be present in
|
||||
ceilometer.conf file
|
||||
|
||||
[DEFAULT]
|
||||
meter_dispatchers = file
|
||||
event_dispatchers = file
|
||||
"""
|
||||
|
||||
def __init__(self, conf):
|
||||
super(FileDispatcher, self).__init__(conf)
|
||||
self.log = None
|
||||
|
||||
# if the directory and path are configured, then log to the file
|
||||
if self.conf.dispatcher_file.file_path:
|
||||
dispatcher_logger = logging.Logger('dispatcher.file')
|
||||
dispatcher_logger.setLevel(logging.INFO)
|
||||
# create rotating file handler which logs meters
|
||||
rfh = logging.handlers.RotatingFileHandler(
|
||||
self.conf.dispatcher_file.file_path,
|
||||
maxBytes=self.conf.dispatcher_file.max_bytes,
|
||||
backupCount=self.conf.dispatcher_file.backup_count,
|
||||
encoding='utf8')
|
||||
|
||||
rfh.setLevel(logging.INFO)
|
||||
# Only wanted the meters to be saved in the file, not the
|
||||
# project root logger.
|
||||
dispatcher_logger.propagate = False
|
||||
dispatcher_logger.addHandler(rfh)
|
||||
self.log = dispatcher_logger
|
||||
|
||||
def record_metering_data(self, data):
|
||||
if self.log:
|
||||
self.log.info(data)
|
||||
|
||||
def record_events(self, events):
|
||||
if self.log:
|
||||
self.log.info(events)
|
@ -27,7 +27,6 @@ import ceilometer.compute.virt.libvirt.utils
|
||||
import ceilometer.compute.virt.vmware.inspector
|
||||
import ceilometer.compute.virt.xenapi.inspector
|
||||
import ceilometer.dispatcher
|
||||
import ceilometer.dispatcher.file
|
||||
import ceilometer.dispatcher.gnocchi_opts
|
||||
import ceilometer.dispatcher.http
|
||||
import ceilometer.event.converter
|
||||
@ -112,7 +111,6 @@ def list_opts():
|
||||
'membership has changed'),
|
||||
]),
|
||||
('database', ceilometer.storage.OPTS),
|
||||
('dispatcher_file', ceilometer.dispatcher.file.OPTS),
|
||||
('dispatcher_http', ceilometer.dispatcher.http.http_dispatcher_opts),
|
||||
('dispatcher_gnocchi',
|
||||
ceilometer.dispatcher.gnocchi_opts.dispatcher_opts),
|
||||
|
@ -31,8 +31,7 @@ class DirectPublisher(publisher.ConfigPublisherBase):
|
||||
are required.
|
||||
|
||||
By default, the database dispatcher is used to select another one we
|
||||
can use direct://?dispatcher=gnocchi, direct://?dispatcher=http,
|
||||
direct://?dispatcher=file, ...
|
||||
can use direct://?dispatcher=gnocchi, direct://?dispatcher=http, ...
|
||||
"""
|
||||
def __init__(self, conf, parsed_url):
|
||||
super(DirectPublisher, self).__init__(conf, parsed_url)
|
||||
|
@ -1,100 +0,0 @@
|
||||
#
|
||||
# Copyright 2013 IBM Corp
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
# a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
import logging.handlers
|
||||
import os
|
||||
import tempfile
|
||||
|
||||
from oslotest import base
|
||||
|
||||
from ceilometer.dispatcher import file
|
||||
from ceilometer.publisher import utils
|
||||
from ceilometer import service
|
||||
|
||||
|
||||
class TestDispatcherFile(base.BaseTestCase):
|
||||
|
||||
def setUp(self):
|
||||
super(TestDispatcherFile, self).setUp()
|
||||
self.CONF = service.prepare_service([], [])
|
||||
|
||||
def test_file_dispatcher_with_all_config(self):
|
||||
# Create a temporaryFile to get a file name
|
||||
tf = tempfile.NamedTemporaryFile('r')
|
||||
filename = tf.name
|
||||
tf.close()
|
||||
|
||||
self.CONF.dispatcher_file.file_path = filename
|
||||
self.CONF.dispatcher_file.max_bytes = 50
|
||||
self.CONF.dispatcher_file.backup_count = 5
|
||||
dispatcher = file.FileDispatcher(self.CONF)
|
||||
|
||||
# The number of the handlers should be 1
|
||||
self.assertEqual(1, len(dispatcher.log.handlers))
|
||||
# The handler should be RotatingFileHandler
|
||||
handler = dispatcher.log.handlers[0]
|
||||
self.assertIsInstance(handler,
|
||||
logging.handlers.RotatingFileHandler)
|
||||
|
||||
msg = {'counter_name': 'test',
|
||||
'resource_id': self.id(),
|
||||
'counter_volume': 1,
|
||||
}
|
||||
msg['message_signature'] = utils.compute_signature(
|
||||
msg, self.CONF.publisher.telemetry_secret,
|
||||
)
|
||||
|
||||
# The record_metering_data method should exist
|
||||
# and not produce errors.
|
||||
dispatcher.record_metering_data(msg)
|
||||
# After the method call above, the file should have been created.
|
||||
self.assertTrue(os.path.exists(handler.baseFilename))
|
||||
|
||||
def test_file_dispatcher_with_path_only(self):
|
||||
# Create a temporaryFile to get a file name
|
||||
tf = tempfile.NamedTemporaryFile('r')
|
||||
filename = tf.name
|
||||
tf.close()
|
||||
|
||||
self.CONF.dispatcher_file.file_path = filename
|
||||
self.CONF.dispatcher_file.max_bytes = 0
|
||||
self.CONF.dispatcher_file.backup_count = 0
|
||||
dispatcher = file.FileDispatcher(self.CONF)
|
||||
|
||||
# The number of the handlers should be 1
|
||||
self.assertEqual(1, len(dispatcher.log.handlers))
|
||||
# The handler should be RotatingFileHandler
|
||||
handler = dispatcher.log.handlers[0]
|
||||
self.assertIsInstance(handler,
|
||||
logging.FileHandler)
|
||||
|
||||
msg = {'counter_name': 'test',
|
||||
'resource_id': self.id(),
|
||||
'counter_volume': 1,
|
||||
}
|
||||
msg['message_signature'] = utils.compute_signature(
|
||||
msg, self.CONF.publisher.telemetry_secret,
|
||||
)
|
||||
|
||||
# The record_metering_data method should exist and not produce errors.
|
||||
dispatcher.record_metering_data(msg)
|
||||
# After the method call above, the file should have been created.
|
||||
self.assertTrue(os.path.exists(handler.baseFilename))
|
||||
|
||||
def test_file_dispatcher_with_no_path(self):
|
||||
self.CONF.dispatcher_file.file_path = None
|
||||
dispatcher = file.FileDispatcher(self.CONF)
|
||||
|
||||
# The log should be None
|
||||
self.assertIsNone(dispatcher.log)
|
@ -1,60 +0,0 @@
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
# a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
import fixtures
|
||||
import mock
|
||||
from oslo_config import fixture
|
||||
|
||||
from ceilometer import collector
|
||||
from ceilometer import dispatcher
|
||||
from ceilometer.publisher import utils
|
||||
from ceilometer import service
|
||||
from ceilometer.tests import base
|
||||
|
||||
|
||||
class FakeDispatcher(dispatcher.EventDispatcherBase):
|
||||
def __init__(self, conf):
|
||||
super(FakeDispatcher, self).__init__(conf)
|
||||
self.events = []
|
||||
|
||||
def record_events(self, events):
|
||||
super(FakeDispatcher, self).record_events(events)
|
||||
self.events.extend(events)
|
||||
|
||||
|
||||
class TestEventDispatcherVerifier(base.BaseTestCase):
|
||||
def setUp(self):
|
||||
super(TestEventDispatcherVerifier, self).setUp()
|
||||
conf = service.prepare_service([], [])
|
||||
self.conf = self.useFixture(fixture.Config(conf)).conf
|
||||
self.conf.import_opt('telemetry_secret',
|
||||
'ceilometer.publisher.utils',
|
||||
'publisher')
|
||||
self.conf.set_override("event_dispatchers", ['file'])
|
||||
self.useFixture(fixtures.MockPatch(
|
||||
'ceilometer.dispatcher.file.FileDispatcher',
|
||||
new=FakeDispatcher))
|
||||
|
||||
@mock.patch('ceilometer.publisher.utils.verify_signature')
|
||||
def test_sample_with_bad_signature(self, mocked_verify):
|
||||
def _fake_verify(ev, secret):
|
||||
return ev.get('message_signature') != 'bad_signature'
|
||||
mocked_verify.side_effect = _fake_verify
|
||||
sample = {"payload": [{"message_signature": "bad_signature"}]}
|
||||
manager = dispatcher.load_dispatcher_manager(self.conf)[1]
|
||||
v = collector.EventEndpoint("secret", manager)
|
||||
v.sample([sample])
|
||||
self.assertEqual([], manager['file'].obj.events)
|
||||
del sample['payload'][0]['message_signature']
|
||||
sample['payload'][0]['message_signature'] = utils.compute_signature(
|
||||
sample['payload'][0], "secret")
|
||||
v.sample([sample])
|
||||
self.assertEqual(sample['payload'], manager['file'].obj.events)
|
@ -287,12 +287,10 @@ console_scripts =
|
||||
|
||||
ceilometer.dispatcher.meter =
|
||||
database = ceilometer.dispatcher.database:MeterDatabaseDispatcher
|
||||
file = ceilometer.dispatcher.file:FileDispatcher
|
||||
http = ceilometer.dispatcher.http:HttpDispatcher
|
||||
gnocchi = ceilometer.dispatcher.gnocchi:GnocchiDispatcher
|
||||
|
||||
ceilometer.dispatcher.event =
|
||||
file = ceilometer.dispatcher.file:FileDispatcher
|
||||
http = ceilometer.dispatcher.http:HttpDispatcher
|
||||
gnocchi = ceilometer.dispatcher.gnocchi:GnocchiDispatcher
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user