-
Notifications
You must be signed in to change notification settings - Fork 542
refactor: move helpers out of ddtrace.internal.runtime.__init__ #19812
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
emmettbutler
wants to merge
2
commits into
main
Choose a base branch
from
emmett.butler/runtime-layering
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.
+132
−116
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| import typing as t | ||
| import uuid | ||
|
|
||
| from ddtrace.internal.settings import env | ||
|
|
||
| from . import forksafe | ||
|
|
||
|
|
||
| __all__ = [ | ||
| "get_ancestor_runtime_id", | ||
| "get_process_role", | ||
| "get_runtime_id", | ||
| "get_parent_runtime_id", | ||
| "get_runtime_propagation_envs", | ||
| ] | ||
|
|
||
|
|
||
| _ENV_ROOT_SESSION_ID = "_DD_ROOT_PY_SESSION_ID" | ||
| _ENV_PARENT_SESSION_ID = "_DD_PARENT_PY_SESSION_ID" | ||
|
|
||
|
|
||
| def _generate_runtime_id() -> str: | ||
| return uuid.uuid4().hex | ||
|
|
||
|
|
||
| _RUNTIME_ID: str = _generate_runtime_id() | ||
| # Seeded from env vars when this process was spawned (multiprocessing spawn/forkserver). | ||
| # For fork-based processes these are set by _set_runtime_id() via the forksafe hook. | ||
| _ANCESTOR_RUNTIME_ID: t.Optional[str] = env.get(_ENV_ROOT_SESSION_ID) | ||
| _PARENT_RUNTIME_ID: t.Optional[str] = env.get(_ENV_PARENT_SESSION_ID) | ||
| # IMPORTANT: Do not change t.Set to set until minimum Python version is 3.11+ | ||
| # Module-level set[...] in Python 3.10 affects import timing. See packages.py for details. | ||
| _ON_RUNTIME_ID_CHANGE: t.Set[t.Callable[[str], None]] = set() # noqa: UP006 | ||
|
|
||
|
|
||
| def on_runtime_id_change(cb: t.Callable[[str], None]) -> None: | ||
| """Register a callback to be called when the runtime ID changes. | ||
|
|
||
| This can happen after a fork. | ||
| """ | ||
| global _ON_RUNTIME_ID_CHANGE | ||
| _ON_RUNTIME_ID_CHANGE.add(cb) | ||
|
|
||
|
|
||
| @forksafe.register | ||
| def _set_runtime_id() -> None: | ||
| global _RUNTIME_ID, _ANCESTOR_RUNTIME_ID, _PARENT_RUNTIME_ID | ||
|
|
||
| # Save the runtime ID of the common ancestor of all processes. | ||
| if _ANCESTOR_RUNTIME_ID is None: | ||
| _ANCESTOR_RUNTIME_ID = _RUNTIME_ID | ||
|
|
||
| _PARENT_RUNTIME_ID = _RUNTIME_ID | ||
| _RUNTIME_ID = _generate_runtime_id() | ||
| for cb in _ON_RUNTIME_ID_CHANGE: | ||
| cb(_RUNTIME_ID) | ||
|
|
||
|
|
||
| def get_runtime_id() -> str: | ||
| """Return a unique string identifier for this runtime. | ||
|
|
||
| Do not store this identifier as it can change when, e.g., the process forks. | ||
| """ | ||
| return _RUNTIME_ID | ||
|
|
||
|
|
||
| def get_ancestor_runtime_id() -> t.Optional[str]: | ||
| """Return the runtime ID of the common ancestor of this process. | ||
|
|
||
| Once this value is set (this will happen after a fork) it will not change | ||
| for the lifetime of the process. This function returns ``None`` for the | ||
| ancestor process. | ||
| """ | ||
| return _ANCESTOR_RUNTIME_ID | ||
|
|
||
|
|
||
| def get_parent_runtime_id() -> t.Optional[str]: | ||
| """Return the runtime ID of the parent process. | ||
|
|
||
| Set after a fork or when seeded from the ``_DD_PARENT_PY_SESSION_ID`` environment | ||
| variable (multiprocessing spawn/forkserver). Returns ``None`` in the root process. | ||
| """ | ||
| return _PARENT_RUNTIME_ID | ||
|
|
||
|
|
||
| def get_process_role() -> t.Optional[str]: | ||
| """Return the role of this process in a forking framework. | ||
|
|
||
| Returns ``'worker'`` if this process was forked from a parent (or spawned | ||
| as a child via multiprocessing), ``'main'`` if this process has forked | ||
| worker children, or ``None`` for a single-process application. | ||
| """ | ||
| if _PARENT_RUNTIME_ID is not None: | ||
| return "worker" | ||
| if forksafe.has_forked(): | ||
| return "main" | ||
| return None | ||
|
|
||
|
|
||
| def get_runtime_propagation_envs() -> dict[str, str]: | ||
| """Return session lineage env vars to inject into child process environments. | ||
|
|
||
| These vars allow exec-based child processes (subprocess, multiprocessing spawn) | ||
| to reconstruct the process lineage without relying on fork inheritance. | ||
| """ | ||
| ancestor = get_ancestor_runtime_id() | ||
| current = get_runtime_id() | ||
| session_vars: dict[str, str] = {_ENV_ROOT_SESSION_ID: ancestor if ancestor is not None else current} | ||
| if current is not None: | ||
| session_vars[_ENV_PARENT_SESSION_ID] = current | ||
| return session_vars |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,9 @@ | ||
| import typing as t | ||
| import uuid | ||
|
|
||
| from ddtrace.internal.settings import env | ||
|
|
||
| from .. import forksafe | ||
| from ddtrace.internal._runtime_id import get_ancestor_runtime_id | ||
| from ddtrace.internal._runtime_id import get_parent_runtime_id | ||
| from ddtrace.internal._runtime_id import get_process_role | ||
|
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.
The profiling test suite fails, so the PR cannot complete required validation. Assertion details
Was this helpful? React 👍 or 👎 |
||
| from ddtrace.internal._runtime_id import get_runtime_id | ||
| from ddtrace.internal._runtime_id import get_runtime_propagation_envs | ||
| from ddtrace.internal._runtime_id import on_runtime_id_change | ||
|
|
||
|
|
||
| __all__ = [ | ||
|
|
@@ -12,100 +12,5 @@ | |
| "get_runtime_id", | ||
| "get_parent_runtime_id", | ||
| "get_runtime_propagation_envs", | ||
| "on_runtime_id_change", | ||
| ] | ||
|
|
||
|
|
||
| _ENV_ROOT_SESSION_ID = "_DD_ROOT_PY_SESSION_ID" | ||
| _ENV_PARENT_SESSION_ID = "_DD_PARENT_PY_SESSION_ID" | ||
|
|
||
|
|
||
| def _generate_runtime_id() -> str: | ||
| return uuid.uuid4().hex | ||
|
|
||
|
|
||
| _RUNTIME_ID: str = _generate_runtime_id() | ||
| # Seeded from env vars when this process was spawned (multiprocessing spawn/forkserver). | ||
| # For fork-based processes these are set by _set_runtime_id() via the forksafe hook. | ||
| _ANCESTOR_RUNTIME_ID: t.Optional[str] = env.get(_ENV_ROOT_SESSION_ID) | ||
| _PARENT_RUNTIME_ID: t.Optional[str] = env.get(_ENV_PARENT_SESSION_ID) | ||
| # IMPORTANT: Do not change t.Set to set until minimum Python version is 3.11+ | ||
| # Module-level set[...] in Python 3.10 affects import timing. See packages.py for details. | ||
| _ON_RUNTIME_ID_CHANGE: t.Set[t.Callable[[str], None]] = set() # noqa: UP006 | ||
|
|
||
|
|
||
| def on_runtime_id_change(cb: t.Callable[[str], None]) -> None: | ||
| """Register a callback to be called when the runtime ID changes. | ||
|
|
||
| This can happen after a fork. | ||
| """ | ||
| global _ON_RUNTIME_ID_CHANGE | ||
| _ON_RUNTIME_ID_CHANGE.add(cb) | ||
|
|
||
|
|
||
| @forksafe.register | ||
| def _set_runtime_id(): | ||
| global _RUNTIME_ID, _ANCESTOR_RUNTIME_ID, _PARENT_RUNTIME_ID | ||
|
|
||
| # Save the runtime ID of the common ancestor of all processes. | ||
| if _ANCESTOR_RUNTIME_ID is None: | ||
| _ANCESTOR_RUNTIME_ID = _RUNTIME_ID | ||
|
|
||
| _PARENT_RUNTIME_ID = _RUNTIME_ID | ||
| _RUNTIME_ID = _generate_runtime_id() | ||
| for cb in _ON_RUNTIME_ID_CHANGE: | ||
| cb(_RUNTIME_ID) | ||
|
|
||
|
|
||
| def get_runtime_id() -> str: | ||
| """Return a unique string identifier for this runtime. | ||
|
|
||
| Do not store this identifier as it can change when, e.g., the process forks. | ||
| """ | ||
| return _RUNTIME_ID | ||
|
|
||
|
|
||
| def get_ancestor_runtime_id() -> t.Optional[str]: | ||
| """Return the runtime ID of the common ancestor of this process. | ||
|
|
||
| Once this value is set (this will happen after a fork) it will not change | ||
| for the lifetime of the process. This function returns ``None`` for the | ||
| ancestor process. | ||
| """ | ||
| return _ANCESTOR_RUNTIME_ID | ||
|
|
||
|
|
||
| def get_parent_runtime_id() -> t.Optional[str]: | ||
| """Return the runtime ID of the parent process. | ||
|
|
||
| Set after a fork or when seeded from the ``_DD_PARENT_PY_SESSION_ID`` environment | ||
| variable (multiprocessing spawn/forkserver). Returns ``None`` in the root process. | ||
| """ | ||
| return _PARENT_RUNTIME_ID | ||
|
|
||
|
|
||
| def get_process_role() -> t.Optional[str]: | ||
| """Return the role of this process in a forking framework. | ||
|
|
||
| Returns ``'worker'`` if this process was forked from a parent (or spawned | ||
| as a child via multiprocessing), ``'main'`` if this process has forked | ||
| worker children, or ``None`` for a single-process application. | ||
| """ | ||
| if _PARENT_RUNTIME_ID is not None: | ||
| return "worker" | ||
| if forksafe.has_forked(): | ||
| return "main" | ||
| return None | ||
|
|
||
|
|
||
| def get_runtime_propagation_envs() -> dict[str, str]: | ||
| """Return session lineage env vars to inject into child process environments. | ||
|
|
||
| These vars allow exec-based child processes (subprocess, multiprocessing spawn) | ||
| to reconstruct the process lineage without relying on fork inheritance. | ||
| """ | ||
| ancestor = get_ancestor_runtime_id() | ||
| current = get_runtime_id() | ||
| session_vars: dict[str, str] = {_ENV_ROOT_SESSION_ID: ancestor if ancestor is not None else current} | ||
| if current is not None: | ||
| session_vars[_ENV_PARENT_SESSION_ID] = current | ||
| return session_vars | ||
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
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.
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.
When the profiling suite runs
test_uwsgi_postfork_worker_role_via_mockortest_uwsgi_main_role_via_mock,tests/profiling/test_process_role.pystill callsmonkeypatch.setattr(ddtrace.internal.runtime, "_PARENT_RUNTIME_ID", ...). This wrapper no longer defines that attribute, so both tests raiseAttributeErrorbefore reaching their assertions; update those mocks to patchddtrace.internal._runtime_id(whereget_process_rolenow reads the state), or preserve compatible forwarding.Useful? React with 👍 / 👎.