Skip to content

Commit

Permalink
Store context in local thread store for logging.
Browse files Browse the repository at this point in the history
This also fixes the logging of request ids for every request.

Fixes bug 1031596

Change-Id: Ifd0217c99402316214efaf1fe8533c60c2277257
  • Loading branch information
ameade committed Aug 1, 2012
1 parent eeedad3 commit 5cf0c9b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
25 changes: 25 additions & 0 deletions glance/context.py
Expand Up @@ -16,6 +16,7 @@
# under the License.

import glance.common.utils
from glance.openstack.common import local


class RequestContext(object):
Expand All @@ -38,6 +39,30 @@ def __init__(self, auth_tok=None, user=None, tenant=None, roles=None,
self.request_id = glance.common.utils.generate_uuid()
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
def owner(self):
"""Return the owner to correlate with an image."""
Expand Down
8 changes: 8 additions & 0 deletions glance/tests/unit/test_context.py
Expand Up @@ -16,6 +16,7 @@
# under the License.

from glance import context
from glance.openstack.common import local
from glance.tests.unit import utils as unit_utils
from glance.tests import utils

Expand Down Expand Up @@ -242,3 +243,10 @@ def test_request_id(self):
def test_service_catalog(self):
ctx = context.RequestContext(service_catalog=['foo'])
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)

0 comments on commit 5cf0c9b

Please sign in to comment.