Skip to content

Commit 12fbb16

Browse files
vitkyrkaclaude
andauthored
flink: add container-based config discovery support (#24485)
* flink: add container-based config discovery support Adds a discovery.strategies block using discovery/openmetrics_from_ports (port 9249), an auto_conf.yaml ad_identifiers entry for the flink image, E2E discovery metadata wiring, and test_e2e_discovery/ test_e2e_discovery_all_candidates tests covering both the jobmanager and taskmanager roles, which share the same container image. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> * Add changelog entry for #24485 Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> * fixup --------- Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
1 parent 8147f85 commit 12fbb16

8 files changed

Lines changed: 146 additions & 4 deletions

File tree

flink/assets/configuration/spec.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ name: flink
22
fleet_configurable: true
33
files:
44
- name: flink.yaml
5+
discovery:
6+
strategies:
7+
- template: discovery/openmetrics_from_ports
8+
overrides:
9+
port_hints:
10+
- 9249
511
options:
612
- template: init_config
713
options:
@@ -23,3 +29,10 @@ files:
2329
path: /var/log/flink.log
2430
source: flink
2531
service: <SERVICE>
32+
- name: auto_conf.yaml
33+
options:
34+
- template: ad_identifiers
35+
overrides:
36+
value.example:
37+
- flink
38+
- template: auto_conf/discovery

flink/changelog.d/24485.added

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add container-based config discovery support.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# (C) Datadog, Inc. 2026-present
2+
# All rights reserved
3+
# Licensed under a 3-clause BSD style license (see LICENSE)
4+
5+
# This file is autogenerated.
6+
# To change this file you should edit assets/configuration/spec.yaml and then run the following commands:
7+
# ddev -x validate config -s <INTEGRATION_NAME>
8+
# ddev -x validate models -s <INTEGRATION_NAME>
9+
10+
from __future__ import annotations
11+
12+
from collections.abc import Iterator
13+
from typing import Any
14+
15+
from datadog_checks.base.utils.discovery import Service, candidate_ports
16+
from datadog_checks.flink.config_models import discovery_overrides
17+
from datadog_checks.flink.config_models.instance import InstanceConfig
18+
from datadog_checks.flink.config_models.shared import SharedConfig
19+
20+
21+
def _generated_candidates(service: Service) -> Iterator[dict[str, Any]]:
22+
shared = SharedConfig.model_validate({}, context={'configured_fields': frozenset()}).model_dump(
23+
by_alias=True, mode='json', exclude_none=True
24+
)
25+
# discovery[0]: from_ports
26+
for port in candidate_ports(service, [9249]):
27+
ctx = {'port': port}
28+
instance_data = {
29+
'openmetrics_endpoint': 'http://{service.host}:{port.number}/metrics'.format(service=service, **ctx),
30+
}
31+
instance = InstanceConfig.model_validate(
32+
instance_data, context={'configured_fields': frozenset(instance_data)}
33+
).model_dump(by_alias=True, mode='json', exclude_none=True)
34+
yield {'init_config': shared, 'instances': [instance]}
35+
36+
37+
def candidates(service: Service) -> Iterator[dict[str, Any]]:
38+
override = getattr(discovery_overrides, 'candidates', None)
39+
if override is None:
40+
yield from _generated_candidates(service)
41+
else:
42+
yield from override(service, default=_generated_candidates)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# (C) Datadog, Inc. 2026-present
2+
# All rights reserved
3+
# Licensed under a 3-clause BSD style license (see LICENSE)
4+
5+
# Override the generated discovery candidates() for this integration.
6+
#
7+
# Define a candidates(service, default) function to wrap or replace the generated
8+
# candidate generation. `default` is the generated generator; call it to reuse
9+
# the spec-driven candidates, or ignore it to replace them entirely.
10+
#
11+
# def candidates(service, default):
12+
# yield from default(service)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# (C) Datadog, Inc. 2026-present
2+
# All rights reserved
3+
# Licensed under a 3-clause BSD style license (see LICENSE)
4+
5+
# Here you can define custom (local:) discovery strategies for this integration.
6+
#
7+
# Decorate a generator with @discovery_strategy (imported from
8+
# datadog_checks.base.utils.discovery) and reference it from the spec discovery
9+
# stanza as `strategy: local:<function_name>`. The function receives the
10+
# discovered Service plus the inputs declared in the spec and yields one context
11+
# (ctx) mapping per candidate, exposing the keys listed in `provides`.
12+
#
13+
# from datadog_checks.base.utils.discovery import discovery_strategy
14+
#
15+
# @discovery_strategy(provides=('svc',))
16+
# def from_some_config(service, config_path):
17+
# ...
18+
# yield {'svc': ...}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
## @param ad_identifiers - list of strings - required
2+
## A list of container identifiers that are used by Autodiscovery to identify
3+
## which container the check should be run against. For more information, see:
4+
## https://docs.datadoghq.com/agent/guide/ad_identifiers/
5+
#
6+
ad_identifiers:
7+
- flink
8+
9+
## Enables configuration discovery
10+
#
11+
discovery: {}
12+
13+
## Unused init configuration
14+
#
15+
init_config:
16+
17+
## Unused instance configuration
18+
#
19+
instances: []

flink/tests/conftest.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import pytest
99

10-
from datadog_checks.dev import docker_run, get_docker_hostname, get_here
10+
from datadog_checks.dev import docker_run, get_docker_hostname, get_e2e_discovery_metadata, get_here
1111
from datadog_checks.dev.conditions import CheckEndpoints
1212
from datadog_checks.flink import FlinkCheck
1313

@@ -26,9 +26,12 @@ def dd_environment():
2626
),
2727
sleep=15,
2828
):
29-
yield {
30-
"openmetrics_endpoint": f"http://{get_docker_hostname()}:{JOBMANAGER_PORT}/metrics",
31-
}
29+
yield (
30+
{
31+
"openmetrics_endpoint": f"http://{get_docker_hostname()}:{JOBMANAGER_PORT}/metrics",
32+
},
33+
get_e2e_discovery_metadata(),
34+
)
3235

3336

3437
@pytest.fixture

flink/tests/test_e2e.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,18 @@
33
# Licensed under a 3-clause BSD style license (see LICENSE)
44
import pytest
55

6+
from datadog_checks.dev.docker import CONTAINER_STABILITY_LOG_PATTERNS, assert_all_discovery_candidates_stable
67
from datadog_checks.flink import FlinkCheck
78

89
pytestmark = [pytest.mark.e2e]
910

11+
# Flink logs a harmless startup notice ("Hadoop FS is not available ...: NoClassDefFoundError")
12+
# with the vanilla `flink` image. Exclude only that known-benign substring so a real error is
13+
# still caught.
14+
DISCOVERY_STABILITY_LOG_PATTERNS = tuple(
15+
pattern if pattern != r'error' else r'(?<!NoClassDefFound)error' for pattern in CONTAINER_STABILITY_LOG_PATTERNS
16+
)
17+
1018
# Core metrics that should be present on a freshly-started JobManager,
1119
# regardless of whether any Flink job has been submitted. JVM and cluster
1220
# metrics are reported as soon as the reporter starts.
@@ -20,9 +28,35 @@
2028
"flink.jobmanager.taskSlotsTotal",
2129
]
2230

31+
# Core metrics that should be present on a freshly-started TaskManager, mirroring
32+
# EXPECTED_CORE_METRICS above but for the other role sharing the same container image.
33+
EXPECTED_TASKMANAGER_CORE_METRICS = [
34+
"flink.taskmanager.Status.JVM.CPU.Load",
35+
"flink.taskmanager.Status.JVM.Memory.Heap.Used",
36+
"flink.taskmanager.Status.JVM.Threads.Count",
37+
]
38+
2339

2440
def test_e2e_jobmanager_metrics(dd_agent_check, dd_environment):
2541
aggregator = dd_agent_check(dd_environment, rate=True)
2642
for metric in EXPECTED_CORE_METRICS:
2743
aggregator.assert_metric(metric, at_least=1)
2844
aggregator.assert_service_check('flink.openmetrics.health', FlinkCheck.OK)
45+
46+
47+
def test_e2e_discovery(dd_agent_check_discovery):
48+
# Both the jobmanager and taskmanager containers share the same `flink` image, so
49+
# Autodiscovery finds and configures one instance per container.
50+
aggregator = dd_agent_check_discovery(rate=True, discovery_min_instances=2)
51+
52+
for metric in EXPECTED_CORE_METRICS:
53+
aggregator.assert_metric(metric, at_least=1)
54+
for metric in EXPECTED_TASKMANAGER_CORE_METRICS:
55+
aggregator.assert_metric(metric, at_least=1)
56+
aggregator.assert_service_check('flink.openmetrics.health', FlinkCheck.OK)
57+
58+
59+
def test_e2e_discovery_all_candidates(dd_agent_check):
60+
assert_all_discovery_candidates_stable(
61+
dd_agent_check, FlinkCheck, compose_service='jobmanager', log_patterns=DISCOVERY_STABILITY_LOG_PATTERNS
62+
)

0 commit comments

Comments
 (0)