You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
FEAT: Add session audit context setter/getter API (set_audit_context / get_audit_context) for compliance tracing #621
Is your feature request related to a problem? Please describe.
There is no built-in way to attach session-level auditing or tracing metadata to a Connection in mssql-python. Applications that need to propagate end-user identity, module names, or action context to SQL Server for auditing, or Extended Events tracing must manually execute raw sp_set_session_context calls, handle input validation themselves, and track which keys have been set. This is error-prone, verbose, and inconsistent across codebases.
Describe the solution you'd like
A setter/getter pair on the connection class
set_audit_context()
Accepts well-known keys (application, module, action, user_id) as named parameters and arbitrary extra keys via **kwargs.
Validates key/value lengths against SQL Server limits (128-char keys, 8000-char values) before executing.
Uses parameterized queries to call sp_set_session_context, preventing SQL injection.
Supports a read_only flag to lock keys for the remainder of the session.
Raises appropriate ProgrammingError / InterfaceError exceptions on invalid input or closed connections.
get_audit_context()
Returns the current session audit context as a dictionary.
The getter reads from the local cache without making a server round-trip
Describe alternatives you've considered
Raw cursor execution — Users call cursor.execute("EXEC sp_set_session_context Mitsukuni Sato (@key)=?, value (@value)=?", ...) directly. This works but requires boilerplate, duplicates validation logic, and lacks discoverability.
Connection string properties — Encoding audit metadata into the connection string (e.g., Application Name). This only covers a single field, is immutable after connect, and doesn't flow to SESSION_CONTEXT().
Additional context
Applications that need to comply with regulations (GDPR, PCI-DSS etc) must propagate end-user identity, module context, and action tracking to SQL Server for audit trail generation and compliance reporting. Without a dedicated setter API, developers are forced to manually execute raw sp_set_session_context stored procedure calls, implement their own input validation, and maintain their own getter/cache logic
Kaps (@kapilsamant) can you open a discussion item with an API proposal. I see that the design and intention is scattered across the issue and the PR, allowing little chance to ask questions about the API.
Activity
github-actions commentedon Jun 3, 2026
Hi Kaps (@kapilsamant), thank you for opening this issue!
Our team will review it shortly. We aim to triage all new issues within 24-48 hours and get back to you.
If you have additional information to share, please feel free to update the issue.
Thank you for your patience!
jahnvi480 commentedon Jun 4, 2026
Thanks Kaps (@kapilsamant) for the PR! We'll review this based on our current priority list and get back to you. Appreciate the contribution!
saurabh500 commentedon Jun 4, 2026
Kaps (@kapilsamant) can you open a discussion item with an API proposal. I see that the design and intention is scattered across the issue and the PR, allowing little chance to ask questions about the API.
saurabh500 commentedon Jun 4, 2026
An example discussion where we built a non-standard API with community is #414
kapilsamant commentedon Jun 4, 2026
Done #624