-
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.
Open
Changes from 4 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. |
22 changes: 22 additions & 0 deletions
22
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,27 @@ | ||
| # (C) Datadog, Inc. 2019-present | ||
| # All rights reserved | ||
| # Licensed under a 3-clause BSD style license (see LICENSE) | ||
| import os | ||
|
|
||
| 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 from the agent's `run_path` config. | ||
|
|
||
| Returns ``None`` when ``run_path`` is unset so callers can decide whether the | ||
| miss is fatal or merely skips a fallback. Works for both the standard install | ||
| (``/opt/datadog-agent/run``) and Remote-Management installs | ||
| (``/opt/datadog-packages/datadog-agent/<version>/run``). | ||
| """ | ||
| 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,41 @@ | ||
| # (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(run_path, parts, expected_install): | ||
| with 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.datadog_agent.get_config', | ||
| return_value='', | ||
| ): | ||
| assert get_agent_embedded_path('sbin', 'gstatus') is None |
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.
Uh oh!
There was an error while loading. Please reload this page.