-
Notifications
You must be signed in to change notification settings - Fork 20
bugsnag: propagate cloud_provider into contextual logging for queued executions #268
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,7 @@ | |
| - execution_id: From ExecutionNode.id - tracks individual execution nodes | ||
| - container_execution_id: From ContainerExecution.id - tracks running containers | ||
| - user_id: User who initiated the operation | ||
| - cloud_provider: From task_spec annotations, set by execution_logging_context for orchestrated runs | ||
| - Any other metadata you want to track in logs | ||
|
|
||
| Usage: | ||
|
|
@@ -23,7 +24,12 @@ | |
|
|
||
| import contextvars | ||
| from contextlib import contextmanager | ||
| from typing import Any, Optional | ||
| from typing import TYPE_CHECKING, Any, Optional | ||
|
|
||
| if TYPE_CHECKING: | ||
| from .. import backend_types_sql as bts | ||
|
|
||
| _CLOUD_PROVIDER_ANNOTATION_KEY = "cloud-pipelines.net/orchestration/cloud_provider" | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nebius_launchers.py has this same exact constant. Would it make sense to create a shared config/model/etc file that both of them reference against? Or some way to not duplicate the variable?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤖 Good eye, but the duplication is intentional for now. The tech lead asked us not to commit |
||
|
|
||
| # Single context variable to store all metadata as a dictionary | ||
| _context_metadata: contextvars.ContextVar[dict[str, Any]] = contextvars.ContextVar( | ||
|
|
@@ -125,3 +131,19 @@ def logging_context(**metadata: Any): | |
| finally: | ||
| # Restore previous metadata | ||
| _context_metadata.set(prev_metadata) | ||
|
|
||
|
|
||
| def execution_logging_context(execution: "bts.ExecutionNode"): | ||
| """Return a logging context populated with metadata for *execution*. | ||
|
|
||
| Always sets ``execution_id``. Also sets ``cloud_provider`` when the | ||
| ``cloud-pipelines.net/orchestration/cloud_provider`` annotation is present | ||
| on the task spec. | ||
| """ | ||
| ctx: dict[str, str] = {"execution_id": execution.id} | ||
| cloud_provider = ((execution.task_spec or {}).get("annotations") or {}).get( | ||
| _CLOUD_PROVIDER_ANNOTATION_KEY | ||
| ) | ||
| if cloud_provider is not None: | ||
| ctx["cloud_provider"] = cloud_provider | ||
| return logging_context(**ctx) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| """Tests for contextual_logging.execution_logging_context.""" | ||
|
|
||
| from cloud_pipelines_backend import backend_types_sql as bts | ||
| from cloud_pipelines_backend.instrumentation import contextual_logging | ||
|
|
||
| _CLOUD_PROVIDER_KEY = "cloud-pipelines.net/orchestration/cloud_provider" | ||
|
|
||
|
|
||
| def _make_execution(*, task_spec: dict | None = None) -> bts.ExecutionNode: | ||
| node = bts.ExecutionNode(task_spec=task_spec or {}) | ||
| node.id = "test-execution-id" | ||
| node.extra_data = {} | ||
| return node | ||
|
|
||
|
|
||
| class TestExecutionLoggingContext: | ||
| def test_always_sets_execution_id(self): | ||
| execution = _make_execution() | ||
| with contextual_logging.execution_logging_context(execution): | ||
| assert ( | ||
| contextual_logging.get_context_metadata("execution_id") | ||
| == "test-execution-id" | ||
| ) | ||
|
|
||
| def test_no_cloud_provider_when_annotation_absent(self): | ||
| execution = _make_execution(task_spec={}) | ||
| with contextual_logging.execution_logging_context(execution): | ||
| assert contextual_logging.get_context_metadata("cloud_provider") is None | ||
|
|
||
| def test_sets_cloud_provider_from_annotation(self): | ||
| execution = _make_execution( | ||
| task_spec={"annotations": {_CLOUD_PROVIDER_KEY: "nebius"}} | ||
| ) | ||
| with contextual_logging.execution_logging_context(execution): | ||
| assert contextual_logging.get_context_metadata("cloud_provider") == "nebius" | ||
|
|
||
| def test_no_cloud_provider_when_task_spec_is_none(self): | ||
| execution = _make_execution(task_spec=None) | ||
| with contextual_logging.execution_logging_context(execution): | ||
| assert contextual_logging.get_context_metadata("cloud_provider") is None | ||
|
|
||
| def test_no_cloud_provider_when_annotations_is_none(self): | ||
| execution = _make_execution(task_spec={"annotations": None}) | ||
| with contextual_logging.execution_logging_context(execution): | ||
| assert contextual_logging.get_context_metadata("cloud_provider") is None | ||
|
|
||
| def test_context_is_cleared_after_block(self): | ||
| execution = _make_execution( | ||
| task_spec={"annotations": {_CLOUD_PROVIDER_KEY: "gcp"}} | ||
| ) | ||
| with contextual_logging.execution_logging_context(execution): | ||
| pass | ||
| assert contextual_logging.get_context_metadata("execution_id") is None | ||
| assert contextual_logging.get_context_metadata("cloud_provider") is None |
Uh oh!
There was an error while loading. Please reload this page.