Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions datadog_checks_base/changelog.d/23644.fixed
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 datadog_checks_base/datadog_checks/base/utils/agent/common.py
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)
Comment thread
Kyle-Neale marked this conversation as resolved.
41 changes: 41 additions & 0 deletions datadog_checks_base/tests/base/utils/test_agent_common.py
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
Loading