diff --git a/ceilometer/objectstore/swift_middleware.py b/ceilometer/objectstore/swift_middleware.py index c655ae5ae..3c5fa1260 100644 --- a/ceilometer/objectstore/swift_middleware.py +++ b/ceilometer/objectstore/swift_middleware.py @@ -43,6 +43,7 @@ from __future__ import absolute_import import logging from oslo.utils import timeutils +import six from ceilometer.openstack.common import context from ceilometer import pipeline @@ -145,8 +146,14 @@ class CeilometerMiddleware(object): def publish_sample(self, env, bytes_received, bytes_sent): path = env['PATH_INFO'] method = env['REQUEST_METHOD'] - headers = dict((header.strip('HTTP_'), env[header]) for header - in env if header.startswith('HTTP_')) + headers = {} + for header in env: + if header.startswith('HTTP_') and env[header]: + key = header.strip('HTTP_') + if isinstance(env[header], six.text_type): + headers[key] = env[header].encode('utf-8') + else: + headers[key] = str(env[header]) try: container = obj = None diff --git a/ceilometer/tests/objectstore/test_swift_middleware.py b/ceilometer/tests/objectstore/test_swift_middleware.py index 9021d937d..fb756d787 100644 --- a/ceilometer/tests/objectstore/test_swift_middleware.py +++ b/ceilometer/tests/objectstore/test_swift_middleware.py @@ -246,6 +246,27 @@ class TestSwiftMiddleware(tests_base.BaseTestCase): data.resource_metadata['http_header_x_var2']) self.assertFalse('http_header_x_var3' in data.resource_metadata) + def test_metadata_headers_unicode(self): + app = swift_middleware.CeilometerMiddleware(FakeApp(), { + 'metadata_headers': 'unicode' + }) + uni = u'\xef\xbd\xa1\xef\xbd\xa5' + req = FakeRequest('/1.0/account/container', + environ={'REQUEST_METHOD': 'GET'}, + headers={'UNICODE': uni}) + list(app(req.environ, self.start_response)) + samples = self.pipeline_manager.pipelines[0].samples + self.assertEqual(2, len(samples)) + data = samples[0] + http_headers = [k for k in data.resource_metadata.keys() + if k.startswith('http_header_')] + self.assertEqual(1, len(http_headers)) + self.assertEqual('1.0', data.resource_metadata['version']) + self.assertEqual('container', data.resource_metadata['container']) + self.assertIsNone(data.resource_metadata['object']) + self.assertEqual(uni.encode('utf-8'), + data.resource_metadata['http_header_unicode']) + def test_metadata_headers_on_not_existing_header(self): app = swift_middleware.CeilometerMiddleware(FakeApp(), { 'metadata_headers': 'x-var3'