Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
15 changes: 15 additions & 0 deletions haproxy/assets/configuration/spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ name: HAProxy
fleet_configurable: true
files:
- name: haproxy.yaml
discovery:
strategies:
- strategy: from_ports
port_hints:
- 8404
candidates:
- use_openmetrics: "true"
openmetrics_endpoint: "http://{service.host}:{port.number}/metrics"
options:
- template: init_config
options:
Expand Down Expand Up @@ -237,3 +245,10 @@ files:
port: 514
service: haproxy
source: haproxy
- name: auto_conf.yaml
options:
- template: ad_identifiers
overrides:
value.example:
- haproxy
- template: auto_conf/discovery
1 change: 1 addition & 0 deletions haproxy/changelog.d/24572.added
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add container-based config discovery support.
43 changes: 43 additions & 0 deletions haproxy/datadog_checks/haproxy/config_models/discovery.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# (C) Datadog, Inc. 2026-present
# All rights reserved
# Licensed under a 3-clause BSD style license (see LICENSE)

# This file is autogenerated.
# To change this file you should edit assets/configuration/spec.yaml and then run the following commands:
# ddev -x validate config -s <INTEGRATION_NAME>
# ddev -x validate models -s <INTEGRATION_NAME>

from __future__ import annotations

from collections.abc import Iterator
from typing import Any

from datadog_checks.base.utils.discovery import Service, candidate_ports
from datadog_checks.haproxy.config_models import discovery_overrides
from datadog_checks.haproxy.config_models.instance import InstanceConfig
from datadog_checks.haproxy.config_models.shared import SharedConfig


def _generated_candidates(service: Service) -> Iterator[dict[str, Any]]:
shared = SharedConfig.model_validate({}, context={'configured_fields': frozenset()}).model_dump(
by_alias=True, mode='json', exclude_none=True
)
# discovery[0]: from_ports
for port in candidate_ports(service, [8404]):
ctx = {'port': port}
instance_data = {
'use_openmetrics': 'true',
'openmetrics_endpoint': 'http://{service.host}:{port.number}/metrics'.format(service=service, **ctx),
}
instance = InstanceConfig.model_validate(
instance_data, context={'configured_fields': frozenset(instance_data)}
).model_dump(by_alias=True, mode='json', exclude_none=True)
yield {'init_config': shared, 'instances': [instance]}


def candidates(service: Service) -> Iterator[dict[str, Any]]:
override = getattr(discovery_overrides, 'candidates', None)
if override is None:
yield from _generated_candidates(service)
else:
yield from override(service, default=_generated_candidates)
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# (C) Datadog, Inc. 2026-present
# All rights reserved
# Licensed under a 3-clause BSD style license (see LICENSE)

# Override the generated discovery candidates() for this integration.
#
# Define a candidates(service, default) function to wrap or replace the generated
# candidate generation. `default` is the generated generator; call it to reuse
# the spec-driven candidates, or ignore it to replace them entirely.
#
# def candidates(service, default):
# yield from default(service)
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# (C) Datadog, Inc. 2026-present
# All rights reserved
# Licensed under a 3-clause BSD style license (see LICENSE)

# Here you can define custom (local:) discovery strategies for this integration.
#
# Decorate a generator with @discovery_strategy (imported from
# datadog_checks.base.utils.discovery) and reference it from the spec discovery
# stanza as `strategy: local:<function_name>`. The function receives the
# discovered Service plus the inputs declared in the spec and yields one context
# (ctx) mapping per candidate, exposing the keys listed in `provides`.
#
# from datadog_checks.base.utils.discovery import discovery_strategy
#
# @discovery_strategy(provides=('svc',))
# def from_some_config(service, config_path):
# ...
# yield {'svc': ...}
19 changes: 19 additions & 0 deletions haproxy/datadog_checks/haproxy/data/auto_conf.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
## @param ad_identifiers - list of strings - required
## A list of container identifiers that are used by Autodiscovery to identify
## which container the check should be run against. For more information, see:
## https://docs.datadoghq.com/agent/guide/ad_identifiers/
#
ad_identifiers:
- haproxy

## Enables configuration discovery
#
discovery: {}

## Unused init configuration
#
init_config:

## Unused instance configuration
#
instances: []
2 changes: 1 addition & 1 deletion haproxy/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ classifiers = [
"Private :: Do Not Upload",
]
dependencies = [
"datadog-checks-base>=37.33.0",
"datadog-checks-base>=37.41.0",
]
dynamic = [
"version",
Expand Down
4 changes: 2 additions & 2 deletions haproxy/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import requests
from packaging import version

from datadog_checks.dev import TempDir, WaitFor, docker_run
from datadog_checks.dev import TempDir, WaitFor, docker_run, get_e2e_discovery_metadata
from datadog_checks.haproxy import HAProxyCheck
from datadog_checks.haproxy.metrics import METRIC_MAP

Expand Down Expand Up @@ -49,7 +49,7 @@ def dd_environment():
yield e
else:
with docker_run(compose_file=os.path.join(HERE, 'docker', 'haproxy.yaml'), endpoints=[ENDPOINT_PROMETHEUS]):
yield INSTANCE
yield INSTANCE, get_e2e_discovery_metadata()


@pytest.fixture(scope='session')
Expand Down
18 changes: 18 additions & 0 deletions haproxy/tests/test_e2e.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import pytest

from datadog_checks.base import is_affirmative
from datadog_checks.dev.docker import assert_all_discovery_candidates_stable
from datadog_checks.dev.utils import get_metadata_metrics
from datadog_checks.haproxy import HAProxyCheck

from .common import ENDPOINT_PROMETHEUS, HAPROXY_LEGACY, requires_new_environment

Expand Down Expand Up @@ -40,3 +42,19 @@ def test_checkv2(dd_agent_check, instancev2, prometheus_metricsv2):

aggregator.assert_all_metrics_covered()
aggregator.assert_metrics_using_metadata(get_metadata_metrics())


def test_e2e_discovery(dd_agent_check_discovery, prometheus_metricsv2):
aggregator = dd_agent_check_discovery(rate=True)

# discovery resolves the container's internal network address, which differs from
# ENDPOINT_PROMETHEUS's host-mapped one, so the endpoint tag isn't asserted here.
for metric in prometheus_metricsv2:
aggregator.assert_metric('haproxy.{}'.format(metric))

aggregator.assert_all_metrics_covered()
aggregator.assert_metrics_using_metadata(get_metadata_metrics())


def test_e2e_discovery_all_candidates(dd_agent_check):
assert_all_discovery_candidates_stable(dd_agent_check, HAProxyCheck)
Loading