{"id":5355,"library":"oslo-context","title":"Oslo Context Library","description":"The `oslo.context` library is a core OpenStack component providing helpers to manage and maintain useful information about a request context. This context is typically populated within a WSGI pipeline and utilized by various modules, such as `oslo.log`, for consistent data access throughout a request's lifecycle. The library is currently at version 6.3.0 and follows the OpenStack release cadence.","status":"active","version":"6.3.0","language":"en","source_language":"en","source_url":"https://github.com/openstack/oslo.context","tags":["openstack","context","wsgi","thread-local","logging"],"install":[{"cmd":"pip install oslo.context","lang":"bash","label":"Install latest version"}],"dependencies":[],"imports":[{"note":"The primary class for managing request-scoped context information.","symbol":"RequestContext","correct":"from oslo_context import context"},{"note":"Used to retrieve the current request context from the thread-local storage.","symbol":"get_current","correct":"from oslo_context import context"}],"quickstart":{"code":"from oslo_context import context\n\n# Create a basic RequestContext\nreq_context = context.RequestContext(\n    auth_token='fake_auth_token',\n    user_id='fake_user_id',\n    project_id='fake_project_id',\n    is_admin=False\n)\n\n# Store the context for the current thread\nreq_context.make_current()\n\n# Later in the application, retrieve the context\ncurrent_context = context.get_current()\n\nif current_context:\n    print(f\"Current User ID: {current_context.user_id}\")\n    print(f\"Is Admin: {current_context.is_admin}\")\n\n# Example of creating an admin context\nadmin_context = context.get_admin_context(show_deleted=True)\nadmin_context.make_current()\nprint(f\"Admin Context User ID: {context.get_current().user_id}\")\nprint(f\"Admin Context is Admin: {context.get_current().is_admin}\")\n\n# Clean up the context (important in testing or if not in a WSGI app)\ncontext.clear_current()","lang":"python","description":"This quickstart demonstrates how to create a `RequestContext` object, make it available globally within the current thread using `make_current()`, and retrieve it with `get_current()`. It also shows how to create a specialized admin context. For real-world applications, `RequestContext` is typically initialized by WSGI middleware and automatically managed."},"warnings":[{"fix":"Migrate from using `tenant` to `project_id` or `project_name` when initializing or accessing context information. Review your `RequestContext` instantiations and update arguments accordingly.","message":"The `tenant` argument of `RequestContext` has been removed in version 6.x.x (specifically, from the Zed series). It was deprecated for a long time.","severity":"breaking","affected_versions":">=6.0.0"},{"fix":"Always use keyword arguments when initializing `RequestContext` to ensure clarity and forward compatibility (e.g., `RequestContext(user_id='...', project_id='...')` instead of `RequestContext('...', '...')`).","message":"Passing positional arguments to `RequestContext.__init__` has been deprecated since version 2.7.0. While it might still work, it can lead to unexpected behavior in future versions.","severity":"deprecated","affected_versions":">=2.7.0"},{"fix":"If your application explicitly uses `get_logging_values` and expects the full `auth_token`, be aware of this change. For non-logging purposes, access `context.auth_token` directly from your `RequestContext` instance.","message":"The `get_logging_values` function (intended for logging) no longer outputs the `auth_token` directly but replaces it with `***` to prevent sensitive data leakage. If you rely on the `auth_token` for other purposes, retrieve it directly from the `RequestContext` object.","severity":"gotcha","affected_versions":">=6.0.0"},{"fix":"For multi-threaded or asynchronous environments, ensure that the `RequestContext` is explicitly passed or re-established in the new thread/task context. Libraries like `oslo.privsep` and `oslo.middleware` handle this for specific use cases [8, 18].","message":"The request context is stored in thread-local storage. If your application uses worker pools or asynchronous patterns that change the execution thread, the context might not be propagated automatically, leading to `None` being returned by `context.get_current()`.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-13T00:00:00.000Z","next_check":"2026-07-12T00:00:00.000Z"}