Don't publish samples if resource_id in missing

In swift API, the user can call uri without the resource_id.
This patch skip the samples publishing instead of raising the
exception:

Traceback (most recent call last):
File "ceilometer/objectstore/swift_middleware.py", line 109, in iter_response
    bytes_sent)
File "ceilometer/objectstore/swift_middleware.py", line 160, in publish_sample
    resource_id=account.partition('AUTH_')[2],
AttributeError: 'NoneType' object has no attribute 'partition'

It print the exception of the samples publishing failure instead of
raising a exception to never break the swift API.

Fixes bug #1223259

Change-Id: I5d90fa620c8f8ea82e86524b9703049257ca0345
This commit is contained in:
Mehdi Abaakouk 2013-09-11 11:07:30 +02:00
parent ecdb54d35d
commit e52e5070b3
2 changed files with 31 additions and 5 deletions

View File

@ -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()

View File

@ -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)