FEAT: Add session audit context API (set_audit_context / get_audit_context)#622
FEAT: Add session audit context API (set_audit_context / get_audit_context)#622kapilsamant wants to merge 3 commits into
Conversation
|
@kapilsamant please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.
Contributor License AgreementContribution License AgreementThis Contribution License Agreement (“Agreement”) is agreed to by the party signing below (“You”),
|
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Adds a session metadata/auditing API on the SQL Server connection using sp_set_session_context, plus a test suite to validate local caching, server round-trips, and error conditions.
Changes:
- Implement
Connection.set_audit_context()to set/merge/clear session context keys and maintain a local cache. - Implement
Connection.get_audit_context()to expose the cached context safely (copy). - Add pytest coverage for server round-trip behavior, merging, clearing, read-only keys, closed-connection errors, and size/type validation.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
| tests/test_023_audit_context.py | New tests covering the new audit context API behaviors and error cases. |
| mssql_python/connection.py | Adds set_audit_context/get_audit_context APIs backed by sp_set_session_context with validation and caching. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Work Item / Issue Reference
Summary
Add session-level auditing/tracing API to the Connection class via two new methods:
set_audit_context() -> Sets session metadata (application, module, action, user_id, and arbitrary extra keys) on the current connection using sp_set_session_context. Supports read_only mode, input validation with length limits, parameterized query execution, and local caching of set values.
get_audit_context()-> Returns a copy of the locally cached session context dictionary.
Values set through this API are visible to SESSION_CONTEXT() in T-SQL, Extended Events, sys.dm_exec_sessions, and audit specifications.