Files
computer-playbook/plugins/lookup/native_metrics_apps.py
T
kevinveenbirkenbachandClaude Opus 4.7 fbecc09d24 refactor(tests): regroup meta tests, add native-metrics contract
- New bidirectional contract test ensures `services.prometheus.native_metrics.enabled`
  and `roles/<role>/templates/prometheus.yml.j2` stay consistent across the roles tree,
  replacing per-role spot checks for gitea/mattermost/matrix. Document the `native-metrics`
  suppression rule and apply it to web-app-mattermost (prepared-but-default-off fragment).
- Move every test in `tests/integration/meta/config/` into the per-domain layout under
  `tests/integration/roles/meta/{services,server}`. Group prometheus tests under
  `services/prometheus/`, repositories tests under `services/repositories/`, the
  `server.csp` shape test under `server/`. Move `test_non_empty.py` one level up
  (scans all meta/*.yml, not just services.yml).
- Delete `tests/integration/meta/spots/` — both files were only `assertIn` over file
  contents (brittle, no behavioural coverage, knowledge belongs in code comments or
  proper integration tests).
- Harden `test_project_root_import.py` to catch chained `Path(__file__)…parent.parent.…`
  walks (the attribute-access twin of `parents[N]` that previously slipped through).
  Refactor 6 surfaced sites to import canonical `PROJECT_ROOT` / `ROLES_DIR`, plus
  delete the now-redundant `_find_roles_dir` / `_find_project_root` helpers in
  `native_metrics_apps` lookup and `invokable_paths` filter plugin.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-17 12:50:35 +02:00

67 lines
2.2 KiB
Python

from __future__ import annotations
from typing import Any
from ansible.plugins.lookup import LookupBase
from utils.cache import ROLES_DIR
from utils.cache.applications import get_merged_applications
from utils.roles.applications.config import get as get_app_conf
class LookupModule(LookupBase):
"""
Return a sorted list of deployed application IDs that satisfy both:
1. services.prometheus.native_metrics.enabled: true in their role config
2. a prometheus.yml.j2 template at roles/<app_id>/templates/
Used by web-app-prometheus/templates/configuration/prometheus.yml.j2 to auto-discover apps
that expose a native /metrics endpoint without hardcoding each app name.
Usage in a template:
{% for app_id in lookup('native_metrics_apps') %}
{% include 'roles/' + app_id + '/templates/prometheus.yml.j2' %}
{% endfor %}
'applications' is obtained via get_merged_applications — the same merged view
that backs lookup('applications').
"""
def run(
self,
terms: list[Any],
variables: dict[str, Any] | None = None,
**kwargs: Any,
) -> list[list[str]]:
vars_ = variables or getattr(self._templar, "available_variables", {}) or {}
roles_dir = ROLES_DIR
applications = get_merged_applications(
variables=vars_,
roles_dir=kwargs.get("roles_dir") or str(roles_dir),
templar=getattr(self, "_templar", None),
)
group_names: list[str] = vars_.get("group_names", [])
result: list[str] = []
for app_id in sorted(applications.keys()):
if app_id not in group_names:
continue
enabled = get_app_conf(
applications=applications,
application_id=app_id,
config_path="services.prometheus.native_metrics.enabled",
strict=False,
default=False,
skip_missing_app=True,
)
if not enabled:
continue
scrape_template = roles_dir / app_id / "templates" / "prometheus.yml.j2"
if scrape_template.exists():
result.append(app_id)
return [result]