mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-10 04:36:40 +00:00
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>
This commit is contained in:
1 parent
058e76e58c
commit
dbf3554baf
10 files changed
+234
-176
No files matched your search
@@ -1,111 +0,0 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
from ansible.errors import AnsibleFilterError
|
||||
|
||||
from plugins.filter.native_metrics_target import native_metrics_target
|
||||
|
||||
|
||||
def _make_applications(
|
||||
app_id: str,
|
||||
container: str,
|
||||
port: int,
|
||||
entity_name: str,
|
||||
service_key: str | None = None,
|
||||
) -> dict:
|
||||
native_metrics: dict = {"port": port}
|
||||
if service_key is not None:
|
||||
native_metrics["service_key"] = service_key
|
||||
return {
|
||||
app_id: {
|
||||
"services": {
|
||||
entity_name: {"name": container},
|
||||
"prometheus": {"native_metrics": native_metrics},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class TestNativeMetricsTargetSuccess(unittest.TestCase):
|
||||
def test_returns_container_colon_port(self):
|
||||
apps = _make_applications("web-app-gitea", "gitea", 3000, "gitea")
|
||||
with patch(
|
||||
"plugins.filter.native_metrics_target.get_entity_name", return_value="gitea"
|
||||
):
|
||||
result = native_metrics_target("web-app-gitea", apps)
|
||||
self.assertEqual(result, "gitea:3000")
|
||||
|
||||
def test_different_app(self):
|
||||
apps = _make_applications(
|
||||
"web-app-mattermost", "mattermost", 8067, "mattermost"
|
||||
)
|
||||
with patch(
|
||||
"plugins.filter.native_metrics_target.get_entity_name",
|
||||
return_value="mattermost",
|
||||
):
|
||||
result = native_metrics_target("web-app-mattermost", apps)
|
||||
self.assertEqual(result, "mattermost:8067")
|
||||
|
||||
def test_service_key_override(self):
|
||||
apps = _make_applications(
|
||||
"web-app-matrix", "matrix-synapse", 9000, "synapse", service_key="synapse"
|
||||
)
|
||||
with patch(
|
||||
"plugins.filter.native_metrics_target.get_entity_name",
|
||||
return_value="matrix",
|
||||
):
|
||||
result = native_metrics_target("web-app-matrix", apps)
|
||||
self.assertEqual(result, "matrix-synapse:9000")
|
||||
|
||||
|
||||
class TestNativeMetricsTargetErrors(unittest.TestCase):
|
||||
def test_raises_when_container_name_missing(self):
|
||||
apps = {
|
||||
"web-app-gitea": {
|
||||
"services": {
|
||||
"prometheus": {"native_metrics": {"port": 3000}},
|
||||
}
|
||||
}
|
||||
}
|
||||
with (
|
||||
patch(
|
||||
"plugins.filter.native_metrics_target.get_entity_name",
|
||||
return_value="gitea",
|
||||
),
|
||||
self.assertRaises(AnsibleFilterError),
|
||||
):
|
||||
native_metrics_target("web-app-gitea", apps)
|
||||
|
||||
def test_raises_when_port_missing(self):
|
||||
apps = {
|
||||
"web-app-gitea": {
|
||||
"services": {
|
||||
"gitea": {"name": "gitea"},
|
||||
"prometheus": {"native_metrics": {}},
|
||||
}
|
||||
}
|
||||
}
|
||||
with (
|
||||
patch(
|
||||
"plugins.filter.native_metrics_target.get_entity_name",
|
||||
return_value="gitea",
|
||||
),
|
||||
self.assertRaises(AnsibleFilterError),
|
||||
):
|
||||
native_metrics_target("web-app-gitea", apps)
|
||||
|
||||
def test_raises_when_app_not_in_applications(self):
|
||||
with (
|
||||
patch(
|
||||
"plugins.filter.native_metrics_target.get_entity_name",
|
||||
return_value="gitea",
|
||||
),
|
||||
self.assertRaises(AnsibleFilterError),
|
||||
):
|
||||
native_metrics_target("web-app-gitea", {})
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,119 @@
|
||||
"""Tests for the native_metrics_target lookup.
|
||||
|
||||
The lookup delegates host resolution to scrape_target, so these run the real
|
||||
scrape_target rather than a stub: the regression being guarded is that swarm
|
||||
must not receive a compose container name, and only the delegation proves it.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import unittest
|
||||
from typing import Any
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
from ansible.errors import AnsibleError
|
||||
|
||||
from plugins.lookup.native_metrics_target import LookupModule
|
||||
from plugins.lookup.scrape_target import LookupModule as ScrapeTargetLookup
|
||||
|
||||
ENTITIES = {"web-app-matrix": "matrix", "web-app-gitea": "gitea"}
|
||||
|
||||
|
||||
class _Templar:
|
||||
def __init__(self, vars_=None):
|
||||
self.available_variables = vars_ or {}
|
||||
|
||||
def template(self, value, **_):
|
||||
return value
|
||||
|
||||
|
||||
def _apps(native_metrics: dict[str, Any]) -> dict[str, Any]:
|
||||
return {
|
||||
"web-app-matrix": {
|
||||
"services": {
|
||||
"prometheus": {"native_metrics": native_metrics},
|
||||
"synapse": {
|
||||
"name": "matrix-synapse",
|
||||
"ports": {"internal": {"http": 8008}},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def _run(terms, *, mode, apps):
|
||||
variables = {"DEPLOYMENT_MODE": mode}
|
||||
lookup = LookupModule()
|
||||
lookup._templar = _Templar(variables)
|
||||
lookup._loader = MagicMock()
|
||||
|
||||
def _dispatch(name, **_kwargs):
|
||||
if name == "applications":
|
||||
return MagicMock(run=lambda *_a, **_k: [apps])
|
||||
scrape = ScrapeTargetLookup()
|
||||
scrape._templar = _Templar(variables)
|
||||
scrape._loader = MagicMock()
|
||||
return scrape
|
||||
|
||||
with (
|
||||
patch(
|
||||
"plugins.lookup.native_metrics_target.lookup_loader"
|
||||
) as native_loader_mock,
|
||||
patch("plugins.lookup.scrape_target.lookup_loader") as scrape_loader_mock,
|
||||
patch(
|
||||
"plugins.lookup.native_metrics_target.get_entity_name",
|
||||
side_effect=lambda app_id: ENTITIES.get(app_id, ""),
|
||||
),
|
||||
patch(
|
||||
"plugins.lookup.scrape_target.get_entity_name",
|
||||
side_effect=lambda app_id: ENTITIES.get(app_id, ""),
|
||||
),
|
||||
):
|
||||
native_loader_mock.get.side_effect = _dispatch
|
||||
scrape_loader_mock.get.return_value = MagicMock(run=lambda *_a, **_k: [apps])
|
||||
return lookup.run(terms, variables=variables)
|
||||
|
||||
|
||||
class TestNativeMetricsTargetLookup(unittest.TestCase):
|
||||
def test_swarm_uses_per_task_dns_not_the_container_name(self):
|
||||
apps = _apps({"port": 9000, "service_key": "synapse"})
|
||||
result = _run(["web-app-matrix"], mode="swarm", apps=apps)
|
||||
self.assertEqual(result, ["tasks.matrix_synapse:9000"])
|
||||
|
||||
def test_compose_uses_the_container_name(self):
|
||||
apps = _apps({"port": 9000, "service_key": "synapse"})
|
||||
result = _run(["web-app-matrix"], mode="compose", apps=apps)
|
||||
self.assertEqual(result, ["matrix-synapse:9000"])
|
||||
|
||||
def test_service_key_defaults_to_the_entity_name(self):
|
||||
apps = {
|
||||
"web-app-gitea": {
|
||||
"services": {
|
||||
"prometheus": {"native_metrics": {"port": 3000}},
|
||||
"gitea": {
|
||||
"name": "gitea",
|
||||
"ports": {"internal": {"http": 3000}},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
result = _run(["web-app-gitea"], mode="swarm", apps=apps)
|
||||
self.assertEqual(result, ["tasks.gitea_gitea:3000"])
|
||||
|
||||
def test_native_metrics_port_takes_precedence_over_the_internal_port(self):
|
||||
apps = _apps({"port": 9000, "service_key": "synapse"})
|
||||
result = _run(["web-app-matrix"], mode="compose", apps=apps)
|
||||
self.assertNotIn("8008", result[0])
|
||||
|
||||
def test_missing_port_raises(self):
|
||||
apps = _apps({"service_key": "synapse"})
|
||||
with self.assertRaises(AnsibleError):
|
||||
_run(["web-app-matrix"], mode="compose", apps=apps)
|
||||
|
||||
def test_missing_application_id_raises(self):
|
||||
with self.assertRaises(AnsibleError):
|
||||
_run([], mode="compose", apps=_apps({"port": 9000}))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user