-
Notifications
You must be signed in to change notification settings - Fork 1
feat: Multi user context #196
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
monoxgas
wants to merge
1
commit into
main
Choose a base branch
from
feat/multi-user-context
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -423,17 +423,7 @@ def get_run_context(self) -> RunContext: | |
| """ | ||
| if (run := current_run_span.get()) is None: | ||
| raise RuntimeError("get_run_context() must be called within a run") | ||
|
|
||
| # Capture OpenTelemetry trace context | ||
| trace_context: dict[str, str] = {} | ||
| propagate.inject(trace_context) | ||
|
|
||
| return { | ||
| "run_id": run.run_id, | ||
| "run_name": run.name, | ||
| "project": run.project, | ||
| "trace_context": trace_context, | ||
| } | ||
| return run.get_context() | ||
| ``` | ||
|
|
||
|
|
||
|
|
@@ -501,30 +491,8 @@ def initialize(self) -> None: | |
| f"Failed to connect to the Dreadnode server: {e}", | ||
| ) from e | ||
|
|
||
| headers = {"User-Agent": f"dreadnode/{VERSION}", "X-Api-Key": self.token} | ||
| span_processors.append( | ||
| BatchSpanProcessor( | ||
| RemovePendingSpansExporter( # This will tell Logfire to emit pending spans to us as well | ||
| OTLPSpanExporter( | ||
| endpoint=urljoin(self.server, "/api/otel/traces"), | ||
| headers=headers, | ||
| compression=Compression.Gzip, | ||
| ), | ||
| ), | ||
| ), | ||
| ) | ||
| # TODO(nick): Metrics | ||
| # https://linear.app/dreadnode/issue/ENG-1310/sdk-add-metrics-exports | ||
| # metric_readers.append( | ||
| # PeriodicExportingMetricReader( | ||
| # OTLPMetricExporter( | ||
| # endpoint=urljoin(self.server, "/v1/metrics"), | ||
| # headers=headers, | ||
| # compression=Compression.Gzip, | ||
| # # preferred_temporality | ||
| # ) | ||
| # ) | ||
| # ) | ||
| span_processors.append(RoutingSpanProcessor(self.server, self.token)) | ||
|
|
||
| if self._api is not None: | ||
| api = self._api | ||
| self._credential_manager = CredentialManager( | ||
|
|
@@ -1750,6 +1718,7 @@ run( | |
| project: str | None = None, | ||
| autolog: bool = True, | ||
| name_prefix: str | None = None, | ||
| api_key: str | None = None, | ||
| attributes: AnyDict | None = None, | ||
| ) -> RunSpan | ||
| ``` | ||
|
|
@@ -1799,6 +1768,16 @@ with dreadnode.run("my_run"): | |
| `True` | ||
| ) | ||
| –Automatically log task inputs, outputs, and execution metrics if otherwise unspecified. | ||
| * **`name_prefix`** | ||
| (`str | None`, default: | ||
| `None` | ||
| ) | ||
| –A prefix to use when generating a random name for the run. | ||
| * **`api_key`** | ||
| (`str | None`, default: | ||
| `None` | ||
| ) | ||
| –An optional API key to use for tracing this run instead of the configured one. | ||
| * **`attributes`** | ||
| (`AnyDict | None`, default: | ||
| `None` | ||
|
|
@@ -1823,6 +1802,7 @@ def run( | |
| project: str | None = None, | ||
| autolog: bool = True, | ||
| name_prefix: str | None = None, | ||
| api_key: str | None = None, | ||
| attributes: AnyDict | None = None, | ||
| ) -> RunSpan: | ||
| """ | ||
|
|
@@ -1849,6 +1829,8 @@ def run( | |
| the project passed to `configure()` will be used, or the | ||
| run will be associated with a default project. | ||
| autolog: Automatically log task inputs, outputs, and execution metrics if otherwise unspecified. | ||
| name_prefix: A prefix to use when generating a random name for the run. | ||
| api_key: An optional API key to use for tracing this run instead of the configured one. | ||
| attributes: Additional attributes to attach to the run span. | ||
|
|
||
| Returns: | ||
|
|
@@ -1870,6 +1852,7 @@ def run( | |
| tags=tags, | ||
| credential_manager=self._credential_manager, # type: ignore[arg-type] | ||
| autolog=autolog, | ||
| export_auth_token=api_key, | ||
| ) | ||
| ``` | ||
|
|
||
|
|
@@ -2649,6 +2632,63 @@ def task_span( | |
| ``` | ||
|
|
||
|
|
||
| </Accordion> | ||
|
|
||
| ### using\_api\_key | ||
|
|
||
| ```python | ||
| using_api_key(api_key: str) -> t.Iterator[None] | ||
| ``` | ||
|
|
||
| Context manager to temporarily override the API key used for exporting spans. | ||
|
|
||
| This is useful for multi-user scenarios where you want to log data | ||
| on behalf of another user. | ||
|
|
||
| Example | ||
|
|
||
| ```python | ||
| with dreadnode.with_api_key("other_user_api_key"): | ||
| with dreadnode.run("my_run"): | ||
| # do some work here | ||
| pass | ||
| ``` | ||
|
|
||
| **Parameters:** | ||
|
|
||
| * **`api_key`** | ||
| (`str`) | ||
| –The API key to use for exporting spans within the context. | ||
|
|
||
| <Accordion title="Source code in dreadnode/main.py" icon="code"> | ||
| ```python | ||
| @contextlib.contextmanager | ||
| def using_api_key(self, api_key: str) -> t.Iterator[None]: | ||
| """ | ||
| Context manager to temporarily override the API key used for exporting spans. | ||
|
|
||
| This is useful for multi-user scenarios where you want to log data | ||
| on behalf of another user. | ||
|
|
||
| Example: | ||
| ~~~ | ||
| with dreadnode.with_api_key("other_user_api_key"): | ||
|
||
| with dreadnode.run("my_run"): | ||
| # do some work here | ||
| pass | ||
| ~~~ | ||
|
|
||
| Args: | ||
| api_key: The API key to use for exporting spans within the context. | ||
| """ | ||
| token_token = current_export_auth_token_context.set(api_key) | ||
| try: | ||
| yield | ||
| finally: | ||
| current_export_auth_token_context.reset(token_token) | ||
| ``` | ||
|
|
||
|
|
||
| </Accordion> | ||
|
|
||
| DreadnodeConfigWarning | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The example code uses
dreadnode.with_api_key()but the actual function name isusing_api_key(). This inconsistency will cause the example to fail.