-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add get_agent_embedded_path helper to base check #23644
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
Kyle-Neale
wants to merge
6
commits into
master
Choose a base branch
from
kyle.neale/agent-embedded-path
base: master
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.
+81
−0
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
bebd7a4
feat(base): add get_agent_embedded_path helper
Kyle-Neale 88c4214
Merge branch 'master' into kyle.neale/agent-embedded-path
Kyle-Neale 9aaabaa
fix(base): rename changelog fragment to PR number; parametrize helper…
Kyle-Neale acd8314
fix(base): make get_agent_embedded_path tests platform-aware
Kyle-Neale 603a539
fix(base): handle Windows in get_agent_embedded_path
Kyle-Neale 61c3a29
fix(base): trim get_agent_embedded_path docstring
Kyle-Neale 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 @@ | ||
| Add `get_agent_embedded_path` helper to resolve paths under the agent's embedded directory regardless of install location. |
20 changes: 20 additions & 0 deletions
20
datadog_checks_base/datadog_checks/base/utils/agent/common.py
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,5 +1,25 @@ | ||
| # (C) Datadog, Inc. 2019-present | ||
| # All rights reserved | ||
| # Licensed under a 3-clause BSD style license (see LICENSE) | ||
| import os | ||
| import sys | ||
|
|
||
| try: | ||
| import datadog_agent | ||
| except ImportError: | ||
| from datadog_checks.base.stubs import datadog_agent | ||
|
|
||
| METRIC_NAMESPACE_METRICS = 'datadog.agent.metrics' | ||
| METRIC_NAMESPACE_PROFILE = 'datadog.agent.profile' | ||
|
|
||
|
|
||
| def get_agent_embedded_path(*parts: str) -> str | None: | ||
| """Resolve a path under the agent's `embedded` directory, or ``None`` if unavailable.""" | ||
| if os.name == 'nt': | ||
| install_path = sys.executable.split('embedded')[0].rstrip(os.sep) | ||
| return os.path.join(install_path, 'embedded3', *parts) | ||
| run_path = datadog_agent.get_config('run_path') | ||
| if not run_path: | ||
| return None | ||
| install_path = run_path[:-4] if run_path.endswith('/run') else run_path | ||
| return os.path.join(install_path, 'embedded', *parts) | ||
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,60 @@ | ||
| # (C) Datadog, Inc. 2026-present | ||
| # All rights reserved | ||
| # Licensed under a 3-clause BSD style license (see LICENSE) | ||
| import os | ||
|
|
||
| import mock | ||
| import pytest | ||
|
|
||
| from datadog_checks.base.utils.agent.common import get_agent_embedded_path | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| 'run_path, parts, expected_install', | ||
| [ | ||
| pytest.param('/opt/datadog-agent/run', ('sbin', 'gstatus'), '/opt/datadog-agent', id='standard_install'), | ||
| pytest.param( | ||
| '/opt/datadog-packages/datadog-agent/7.79.0/run', | ||
| ('sbin', 'gstatus'), | ||
| '/opt/datadog-packages/datadog-agent/7.79.0', | ||
| id='remote_management_install', | ||
| ), | ||
| pytest.param( | ||
| '/custom/agent/dir', ('ssl', 'certs', 'cacert.pem'), '/custom/agent/dir', id='run_path_without_trailing_run' | ||
| ), | ||
| pytest.param('/opt/datadog-agent/run', (), '/opt/datadog-agent', id='no_parts'), | ||
| ], | ||
| ) | ||
| def test_get_agent_embedded_path_posix(run_path, parts, expected_install): | ||
| with ( | ||
| mock.patch('datadog_checks.base.utils.agent.common.os.name', 'posix'), | ||
| mock.patch( | ||
| 'datadog_checks.base.utils.agent.common.datadog_agent.get_config', | ||
| return_value=run_path, | ||
| ), | ||
| ): | ||
| assert get_agent_embedded_path(*parts) == os.path.join(expected_install, 'embedded', *parts) | ||
|
|
||
|
|
||
| def test_get_agent_embedded_path_missing_run_path_returns_none(): | ||
| with ( | ||
| mock.patch('datadog_checks.base.utils.agent.common.os.name', 'posix'), | ||
| mock.patch( | ||
| 'datadog_checks.base.utils.agent.common.datadog_agent.get_config', | ||
| return_value='', | ||
| ), | ||
| ): | ||
| assert get_agent_embedded_path('sbin', 'gstatus') is None | ||
|
|
||
|
|
||
| def test_get_agent_embedded_path_windows_uses_sys_executable(): | ||
| """On Windows, derive from sys.executable and use the embedded3 directory.""" | ||
| install_dir = r'C:\Program Files\Datadog\Datadog Agent' | ||
| sys_executable = os.path.join(install_dir, 'embedded3', 'python.exe') | ||
| with ( | ||
| mock.patch('datadog_checks.base.utils.agent.common.os.name', 'nt'), | ||
| mock.patch('datadog_checks.base.utils.agent.common.sys.executable', sys_executable), | ||
| ): | ||
| assert get_agent_embedded_path('ssl', 'certs', 'cacert.pem') == os.path.join( | ||
| install_dir, 'embedded3', 'ssl', 'certs', 'cacert.pem' | ||
| ) |
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 this helper is used from a Windows Agent,
run_pathis the writable cache directory (%ProgramData%\\Datadog\\run, as documented indatadog_checks_base/datadog_checks/base/checks/base.py), not the install directory, and the embedded runtime lives underC:\\Program Files\\Datadog\\Datadog Agent\\embedded3(the existing FIPS helper switches toembedded3foros.name == 'nt'). Since this line only strips a literal/runand then appendsembedded, it would return a path like%ProgramData%\\Datadog\\run\\embedded\\..., so Windows checks that adopt the new helper will fail to find their bundled certs or binaries.Useful? React with 👍 / 👎.