feat: Multi user context#196
Conversation
There was a problem hiding this comment.
Pull Request Overview
Introduces multi-user context support to enable tracking and exporting spans with API key overrides, allowing services to handle multiple users with dedicated export instances per API key.
- Added
RoutingSpanProcessorto route spans based on user-specific API keys instead of using a single BatchSpanProcessor - Introduced
using_api_key()context manager andapi_keyparameter torun()for per-user span export control - Enhanced
RunContextto include export authentication token for cross-thread/process context continuity
Reviewed Changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| dreadnode/tracing/span.py | Added export auth token context management and updated RunSpan to support API key overrides |
| dreadnode/tracing/processors.py | New RoutingSpanProcessor for multi-user span routing based on API keys |
| dreadnode/tracing/constants.py | Added constant for span resource attribute token |
| dreadnode/main.py | Replaced BatchSpanProcessor with RoutingSpanProcessor and added using_api_key context manager |
| dreadnode/init.py | Exported using_api_key function |
| docs/sdk/main.mdx | Updated documentation to reflect new API key functionality |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
| class RunContext(te.TypedDict, total=False): | ||
| """Context for transferring and continuing runs in other places.""" | ||
|
|
||
| run_id: str | ||
| run_name: str | ||
| project: str | ||
| trace_context: dict[str, str] | ||
| export_auth_token: str | None | ||
|
|
||
|
|
There was a problem hiding this comment.
Changing RunContext from a required TypedDict to total=False makes all fields optional, which could break existing code that assumes required fields like 'run_id' are always present. Consider using separate required and optional field groups or create a new TypedDict for the optional fields.
| class RunContext(te.TypedDict, total=False): | |
| """Context for transferring and continuing runs in other places.""" | |
| run_id: str | |
| run_name: str | |
| project: str | |
| trace_context: dict[str, str] | |
| export_auth_token: str | None | |
| class RunContextBase(te.TypedDict): | |
| """Required fields for RunContext.""" | |
| run_id: str | |
| run_name: str | |
| project: str | |
| trace_context: dict[str, str] | |
| class RunContext(RunContextBase, total=False): | |
| """Context for transferring and continuing runs in other places, with optional fields.""" | |
| export_auth_token: str | None |
| server_url: str, | ||
| default_token: str, | ||
| *, | ||
| token_header_name: str = "X-Api-Key", # noqa: S107 |
There was a problem hiding this comment.
The noqa comment suggests this is flagged as a security issue. Consider using a constant or configuration value instead of a hardcoded string literal for the API key header name.
| Example | ||
|
|
||
| ```python | ||
| with dreadnode.with_api_key("other_user_api_key"): |
There was a problem hiding this comment.
The example code uses dreadnode.with_api_key() but the actual function name is using_api_key(). This inconsistency will cause the example to fail.
|
|
||
| Example: | ||
| ~~~ | ||
| with dreadnode.with_api_key("other_user_api_key"): |
There was a problem hiding this comment.
The documentation example uses with_api_key() but the actual function name is using_api_key(). This inconsistency will cause the example to fail.
Support tracking and exporting spans with api key overrides to support multi-user services
Key Changes
dn.run(..., api_key=)argument andwith dn.using_api_key()to override this context on demand.get_run_contextcarries this override information so if we have a backend stack passing around user-local context, it's reflected in the exports.Example
Generated Summary:
PR Description
This pull request introduces significant modifications to the SDK's tracing mechanism, enhancing support for multi-user scenarios by allowing the use of API keys for span exports.
get_run_contextmethod to simplify the extraction of run context data.using_api_key, to facilitate temporary overriding of the API key used for exporting spans during nested operations.api_keyas an optional parameter torunandinitializemethods, enabling context-specific tracing.BatchSpanProcessorwith a newRoutingSpanProcessorthat allows spans to be routed based on user-specific tokens.RunContextstructure to includeexport_auth_tokento maintain API key information across different contexts.These changes enable more flexible tracing capabilities and improve the SDK's usability in multi-user applications, allowing for controlled export configurations based on individual API keys.
This summary was generated with ❤️ by rigging