Skip to content

Commit 377010f

Browse files
vitkyrkaclaude
andcommitted
process autodiscovery for krakend
Adds process-based autodiscovery for krakend, on top of the existing container-based autodiscovery, using a CEL selector to match the krakend process by name. Both flavors run as parametrized variants of the same e2e test in a single environment: the process variant scopes DD_AUTOCONFIG_EXCLUDE_FEATURES=docker to just that discovery invocation (via the new ddev --env support) so that invocation's autodiscovery run never learns about containers, letting the otherwise container-bound krakend process be matched by process-based autodiscovery instead. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
1 parent 8d60800 commit 377010f

10 files changed

Lines changed: 78 additions & 13 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add process autodiscovery E2E testing helpers.

datadog_checks_dev/datadog_checks/dev/docker.py

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -229,27 +229,45 @@ def _assert_no_log_patterns(logs: str, patterns: Sequence[str], candidate_index:
229229
)
230230

231231

232+
def _get_auto_conf_volume(check_root: str | os.PathLike[str] | None = None) -> str:
233+
check_root = os.fspath(check_root or find_check_root(depth=2))
234+
check_name = os.path.basename(check_root)
235+
check_pkg = os.path.join(check_root, 'datadog_checks', check_name)
236+
return f'{check_pkg}/data/auto_conf.yaml:/etc/datadog-agent/conf.d/{check_name}.d/auto_conf.yaml:ro'
237+
238+
232239
def get_e2e_discovery_metadata(
233240
check_root: str | os.PathLike[str] | None = None,
234-
) -> dict[str, list[str]]:
235-
"""Return Docker volume metadata for an e2e discovery run.
241+
) -> dict[str, list[str] | dict[str, str]]:
242+
"""Return metadata for an e2e discovery run.
236243
237-
Mounts the integration's ``auto_conf.yaml`` into the agent container.
244+
Mounts the integration's ``auto_conf.yaml`` into the agent container, and grants
245+
the capabilities needed for process-based autodiscovery to see processes running
246+
in sibling containers (on top of the container-based autodiscovery already
247+
enabled via the Docker socket mount).
238248
239249
Use ``dd_agent_check_discovery`` alongside this metadata so that the static
240250
per-env config is temporarily replaced with an empty-instances file, leaving
241-
``auto_conf.yaml`` as the sole AD template driving config-discovery.
251+
``auto_conf.yaml`` as the sole AD template driving config-discovery. To exercise
252+
process-based (rather than container-based) discovery specifically, pass
253+
``process=True`` to that single ``dd_agent_check_discovery`` call so only that
254+
invocation's autodiscovery run stops seeing containers.
242255
"""
243-
check_root = os.fspath(check_root or find_check_root(depth=1))
244-
check_name = os.path.basename(check_root)
245-
check_pkg = os.path.join(check_root, 'datadog_checks', check_name)
246-
auto_conf = os.path.join(check_pkg, 'data', 'auto_conf.yaml')
247-
248256
return {
249257
'docker_volumes': [
250-
f'{auto_conf}:/etc/datadog-agent/conf.d/{check_name}.d/auto_conf.yaml:ro',
258+
_get_auto_conf_volume(check_root),
251259
'/var/run/docker.sock:/var/run/docker.sock:ro',
252260
],
261+
'env_vars': {
262+
# Reduce the default service collection interval from 60s to speed
263+
# up tests.
264+
'DD_DISCOVERY_SERVICE_COLLECTION_INTERVAL': '10s',
265+
},
266+
# system-probe(-lite) needs these capabilities to read /proc entries of
267+
# other processes for service discovery. Without them it falls back to
268+
# only scanning the agent's own PID namespace, which finds no host
269+
# processes.
270+
'cap_add': ['SYS_PTRACE', 'DAC_READ_SEARCH'],
253271
}
254272

255273

datadog_checks_dev/datadog_checks/dev/plugin/pytest.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,9 @@ def run_check(config=None, **kwargs):
203203
if 'times' in kwargs:
204204
kwargs['check_times'] = kwargs.pop('times')
205205

206+
for env_key, env_value in (kwargs.pop('env_vars', None) or {}).items():
207+
check_command.extend(['--env', '{}={}'.format(env_key, env_value)])
208+
206209
for key, value in kwargs.items():
207210
if value is not False:
208211
check_command.append('--{}'.format(key.replace('_', '-')))
@@ -244,11 +247,30 @@ def dd_agent_check_discovery(dd_agent_check):
244247
Passes the empty-instances config required to let ``auto_conf.yaml`` drive
245248
autodiscovery, and sets sensible defaults for ``discovery_min_instances`` and
246249
``discovery_timeout`` — all of which can be overridden per call.
250+
251+
Pass ``process=True`` to exercise process-based (rather than container-based)
252+
autodiscovery for a process that is otherwise container-bound: this excludes
253+
the ``docker`` feature for this invocation only (so this run's autodiscovery
254+
never learns about any containers) and, unless overridden, extends the
255+
default timeout to account for the minimum process age that process-based
256+
autodiscovery requires before it recognizes a process as a service.
247257
"""
248258
if not e2e_testing():
249259
pytest.skip('Not running E2E tests')
250260

251-
def run(*, discovery_min_instances=1, discovery_timeout=30, **kwargs):
261+
def run(*, discovery_min_instances=1, discovery_timeout=None, process=False, **kwargs):
262+
if discovery_timeout is None:
263+
# Process autodiscovery needs to wait for the agent to recognize the
264+
# process as a service which happens after a minimum process age of 1
265+
# minute. This time can be reduced significantly once agent-side
266+
# support for reducing the minimum age via configuration is available.
267+
discovery_timeout = 90 if process else 30
268+
269+
if process:
270+
env_vars = kwargs.pop('env_vars', None) or {}
271+
env_vars.setdefault('DD_AUTOCONFIG_EXCLUDE_FEATURES', 'docker')
272+
kwargs['env_vars'] = env_vars
273+
252274
return dd_agent_check(
253275
{'init_config': {}, 'instances': []},
254276
discovery_min_instances=discovery_min_instances,
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
- name: cel_selector
2+
description: CEL selector for autodiscovery.
3+
enabled: true
4+
example: {}

datadog_checks_dev/tests/test_docker.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ def test_get_e2e_discovery_metadata(tmp_path):
6767
f'{check_package_root}/data/auto_conf.yaml:/etc/datadog-agent/conf.d/test_check.d/auto_conf.yaml:ro',
6868
'/var/run/docker.sock:/var/run/docker.sock:ro',
6969
],
70+
'env_vars': {
71+
'DD_DISCOVERY_SERVICE_COLLECTION_INTERVAL': '10s',
72+
},
73+
'cap_add': ['SYS_PTRACE', 'DAC_READ_SEARCH'],
7074
}
7175

7276

ddev/src/ddev/e2e/agent/docker.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,9 @@ def start(self, *, agent_build: str | None, local_packages: dict[Path, str], env
277277
for key, value in sorted(env_vars.items()):
278278
command.extend(['-e', f'{key}={value}'])
279279

280+
for cap in self.metadata.get('cap_add', []):
281+
command.extend(['--cap-add', cap])
282+
280283
# The docker `--add-host` command will reliably create entries in the `/etc/hosts` file,
281284
# otherwise, edits to that file will be overwritten on container restarts
282285
for host, ip in self.metadata.get('custom_hosts', []):

krakend/assets/configuration/spec.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,9 @@ files:
8080
overrides:
8181
value.example:
8282
- krakend
83+
- template: auto_conf/cel_selector
84+
overrides:
85+
cel_selector.example:
86+
processes:
87+
- "process.name == 'krakend'"
8388
- template: auto_conf/discovery

krakend/changelog.d/24238.added

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add process autodiscovery support.

krakend/datadog_checks/krakend/data/auto_conf.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
ad_identifiers:
77
- krakend
88

9+
## CEL selector for autodiscovery.
10+
#
11+
cel_selector:
12+
processes:
13+
- process.name == 'krakend'
14+
915
## Enables configuration discovery
1016
#
1117
discovery: {}

krakend/tests/test_e2e.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,15 @@ def test_e2e(dd_agent_check, instance: InstanceBuilder):
2525

2626

2727
@pytest.mark.e2e
28-
def test_e2e_discovery(dd_agent_check_discovery, is_lab):
28+
@pytest.mark.parametrize('process', [False, True], ids=['container', 'process'])
29+
def test_e2e_discovery(dd_agent_check_discovery, is_lab, process):
2930
# In the lab environment we currently do not mount auto_conf.yaml into the
3031
# Agent container, so the Agent has no Autodiscovery template to trigger
3132
# config discovery.
3233
if is_lab:
3334
pytest.skip('lab does not currently support configuration discovery')
3435

35-
aggregator = dd_agent_check_discovery(check_rate=True)
36+
aggregator = dd_agent_check_discovery(check_rate=True, process=process)
3637

3738
metadata_metrics = get_metrics_from_metadata()
3839

0 commit comments

Comments
 (0)