Store context in local thread store for logging.

This also fixes the logging of request ids for every request.

Fixes bug 1031596

Change-Id: Ifd0217c99402316214efaf1fe8533c60c2277257
This commit is contained in:
Alex Meade 2012-08-01 00:32:30 -04:00
parent eeedad3333
commit 5cf0c9b4eb
2 changed files with 33 additions and 0 deletions

View File

@ -16,6 +16,7 @@
# under the License. # under the License.
import glance.common.utils import glance.common.utils
from glance.openstack.common import local
class RequestContext(object): class RequestContext(object):
@ -38,6 +39,30 @@ class RequestContext(object):
self.request_id = glance.common.utils.generate_uuid() self.request_id = glance.common.utils.generate_uuid()
self.service_catalog = service_catalog self.service_catalog = service_catalog
if not hasattr(local.store, 'context'):
self.update_store()
def to_dict(self):
# NOTE(ameade): These keys are named to correspond with the default
# format string for logging the context in openstack common
return {'request_id': self.request_id,
'user_id': self.user,
'tenant_id': self.tenant,
'is_admin': self.is_admin,
'project_id': self.tenant,
'read_deleted': self.show_deleted,
'roles': self.roles,
'auth_token': self.auth_tok,
'service_catalog': self.service_catalog,
}
@classmethod
def from_dict(cls, values):
return cls(**values)
def update_store(self):
local.store.context = self
@property @property
def owner(self): def owner(self):
"""Return the owner to correlate with an image.""" """Return the owner to correlate with an image."""

View File

@ -16,6 +16,7 @@
# under the License. # under the License.
from glance import context from glance import context
from glance.openstack.common import local
from glance.tests.unit import utils as unit_utils from glance.tests.unit import utils as unit_utils
from glance.tests import utils from glance.tests import utils
@ -242,3 +243,10 @@ class TestContext(utils.BaseTestCase):
def test_service_catalog(self): def test_service_catalog(self):
ctx = context.RequestContext(service_catalog=['foo']) ctx = context.RequestContext(service_catalog=['foo'])
self.assertEqual(['foo'], ctx.service_catalog) self.assertEqual(['foo'], ctx.service_catalog)
def test_context_local_store(self):
if hasattr(local.store, 'context'):
del local.store.context
ctx = context.RequestContext()
self.assertTrue(hasattr(local.store, 'context'))
self.assertEqual(ctx, local.store.context)