Files
computer-playbook/plugins/lookup/scrape_target.py
kevinveenbirkenbachandClaude Opus 5 50b9f60c43 fix(prometheus): resolve the native-metrics scrape target per deployment mode
`native_metrics_target` was a Jinja filter, so it could not read
DEPLOYMENT_MODE: filters see no Ansible variables. It returned
`<services.<key>.name>:<port>` unconditionally, which is `matrix-synapse:9000`
for matrix. Compose serves that name; swarm does not, because `docker stack
deploy` ignores `container_name` and the alias never exists. This repository
says as much itself, through `lookup('compose_only', 'container_name', ...)`.
The target was therefore scraped and refused, and `playwright.spec.js:274`
reported `web-app-matrix: 1 matching target(s) reporting up != 1`, meaning one
matching target whose `up` value is not 1.

It becomes a lookup that delegates host resolution to `scrape_target`, which
was already mode-aware, so exactly one plugin knows the difference between the
two modes. The port has to be handed over explicitly: matrix exposes metrics on
9000 while `services.synapse.ports.internal.http` is 8008, so the existing
internal-port lookup cannot serve this case on its own.

`scrape_target` now refuses `port` together with an explicit `port_kind`.
Before, `port` won silently and the error text still named the
`ports.internal.<port_kind>` path that the call had never consulted.

`native_metrics_apps` drops an app that pins itself to compose through
`compose_mode_force` while the cluster runs swarm, because the scrape target
would then name the wrong object. Only the literal case: matrix declares that
override as a Jinja expression over its flavour, which this lookup cannot
resolve from the scraping role's variable scope, so variant 0 on swarm stays a
latent mismatch.

The two comment lines claiming `container_name:port` went with the change;
after it they state the opposite of what the code does.

Verified by mutation: with the swarm branch of `scrape_target` disabled, the
new test emits `matrix-synapse:9000`, the exact string CI reported.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-10-08 08:57:13 +02:00

107 lines
3.8 KiB
Python

"""Lookup ``scrape_target``: mode-aware ``<host>:<port>`` for a Prometheus
scrape target. Swarm resolves ``tasks.<entity>_<service_key>`` (per-task DNS);
compose uses ``compose_host`` or the service container name. ``port`` overrides
the ``services.<service_key>.ports.internal.<port_kind>`` lookup."""
from __future__ import annotations
import contextlib
from typing import Any
from ansible.errors import AnsibleError
from ansible.plugins.loader import lookup_loader
from ansible.plugins.lookup import LookupBase
from utils.roles.applications.config import get
from utils.roles.entity.name import get_entity_name
def _as_str(value: Any) -> str:
return "" if value is None else str(value).strip()
class LookupModule(LookupBase):
def run(
self,
terms: list[Any] | None,
variables: dict[str, Any] | None = None,
**kwargs: Any,
) -> list[str]:
if not terms:
raise AnsibleError("scrape_target lookup requires the application_id term")
application_id = _as_str(terms[0])
if not application_id:
raise AnsibleError("scrape_target: application_id must be non-empty")
service_key = _as_str(terms[1]) if len(terms) > 1 else ""
if not service_key:
raise AnsibleError("scrape_target: service_key must be non-empty")
explicit_port_kind = (
_as_str(terms[2]) if len(terms) > 2 else _as_str(kwargs.get("port_kind"))
)
port_kind = explicit_port_kind or "http"
compose_host = (
_as_str(terms[3]) if len(terms) > 3 else _as_str(kwargs.get("compose_host"))
)
port_override = _as_str(kwargs.get("port"))
if port_override and explicit_port_kind:
raise AnsibleError(
f"scrape_target: {application_id!r} passed both port={port_override!r} "
f"and port_kind={explicit_port_kind!r}; port wins and port_kind would be "
"silently dropped, so pass only one"
)
vars_ = variables or getattr(self._templar, "available_variables", {}) or {}
templar = getattr(self, "_templar", None)
raw_mode = vars_.get("DEPLOYMENT_MODE", "compose")
if templar is not None:
with contextlib.suppress(Exception):
raw_mode = templar.template(raw_mode)
deployment_mode = str(raw_mode).strip()
applications = lookup_loader.get(
"applications", loader=self._loader, templar=getattr(self, "_templar", None)
).run([], variables=vars_)[0]
port = port_override or _as_str(
get(
applications=applications,
application_id=application_id,
config_path=f"services.{service_key}.ports.internal.{port_kind}",
strict=False,
default="",
)
)
if not port:
raise AnsibleError(
f"scrape_target: no internal port for {application_id!r} "
f"(services.{service_key}.ports.internal.{port_kind})"
)
if deployment_mode == "swarm":
entity = get_entity_name(application_id)
if not entity:
raise AnsibleError(
f"scrape_target: cannot derive entity from {application_id!r}"
)
return [f"tasks.{entity}_{service_key}:{port}"]
host = compose_host or _as_str(
get(
applications=applications,
application_id=application_id,
config_path=f"services.{service_key}.name",
strict=False,
default="",
)
)
if not host:
raise AnsibleError(
f"scrape_target: no compose host for {application_id!r} "
f"(services.{service_key}.name)"
)
return [f"{host}:{port}"]