diff --git a/ceilometer/objectstore/swift_middleware.py b/ceilometer/objectstore/swift_middleware.py index 1f3891e06..145c383f8 100644 --- a/ceilometer/objectstore/swift_middleware.py +++ b/ceilometer/objectstore/swift_middleware.py @@ -37,7 +37,7 @@ metadata_headers = X-TEST from __future__ import absolute_import -from swift.common.utils import split_path +from swift.common.utils import split_path, get_logger import webob REQUEST = webob @@ -68,6 +68,7 @@ class CeilometerMiddleware(object): def __init__(self, app, conf): self.app = app + self.logger = get_logger(conf, log_route='ceilometer') self.metadata_headers = [h.strip().replace('-', '_').lower() for h in conf.get( @@ -100,9 +101,12 @@ class CeilometerMiddleware(object): bytes_sent += len(chunk) yield chunk finally: - self.publish_sample(env, - input_proxy.bytes_received, - bytes_sent) + try: + self.publish_sample(env, + input_proxy.bytes_received, + bytes_sent) + except Exception: + self.logger.exception('Failed to publish samples') try: iterable = self.app(env, my_start_response) @@ -115,7 +119,7 @@ class CeilometerMiddleware(object): def publish_sample(self, env, bytes_received, bytes_sent): req = REQUEST.Request(env) try: - version, account, container, obj = split_path(req.path, 1, 4, True) + version, account, container, obj = split_path(req.path, 2, 4, True) except ValueError: return now = timeutils.utcnow().isoformat() diff --git a/tests/objectstore/test_swift_middleware.py b/tests/objectstore/test_swift_middleware.py index 6717900b6..ffce02e2d 100644 --- a/tests/objectstore/test_swift_middleware.py +++ b/tests/objectstore/test_swift_middleware.py @@ -18,6 +18,7 @@ # under the License. import cStringIO as StringIO +import mock from oslo.config import cfg from webob import Request @@ -248,3 +249,24 @@ class TestSwiftMiddleware(base.TestCase): list(app(req.environ, self.start_response)) samples = self.pipeline_manager.pipelines[0].samples self.assertEqual(len(samples), 0) + + def test_missing_resource_id(self): + app = swift_middleware.CeilometerMiddleware(FakeApp(), {}) + req = Request.blank('/5.0/', + environ={'REQUEST_METHOD': 'GET'}) + list(app(req.environ, self.start_response)) + samples = self.pipeline_manager.pipelines[0].samples + self.assertEqual(len(samples), 0) + + @mock.patch.object(swift_middleware.CeilometerMiddleware, + 'publish_sample') + def test_publish_sample_fail(self, mocked_publish_sample): + mocked_publish_sample.side_effect = Exception("a exception") + app = swift_middleware.CeilometerMiddleware(FakeApp(body=["test"]), {}) + req = Request.blank('/1.0/account/container', + environ={'REQUEST_METHOD': 'GET'}) + resp = list(app(req.environ, self.start_response)) + samples = self.pipeline_manager.pipelines[0].samples + self.assertEqual(len(samples), 0) + self.assertEqual(resp, ["test"]) + mocked_publish_sample.assert_called_once_with(mock.ANY, 0, 4)