← Back to team overview

sts-sponsors team mailing list archive

[Merge] ~adam-collard/maas-ci/+git/system-tests:extract_o11y into ~maas-committers/maas-ci/+git/system-tests:master

 

Adam Collard has proposed merging ~adam-collard/maas-ci/+git/system-tests:extract_o11y into ~maas-committers/maas-ci/+git/system-tests:master.

Commit message:
Extract o11y configuration to o11y.py



Requested reviews:
  MAAS Committers (maas-committers)

For more details, see:
https://code.launchpad.net/~adam-collard/maas-ci/+git/system-tests/+merge/435675
-- 
Your team MAAS Committers is requested to review the proposed merge of ~adam-collard/maas-ci/+git/system-tests:extract_o11y into ~maas-committers/maas-ci/+git/system-tests:master.
diff --git a/systemtests/fixtures.py b/systemtests/fixtures.py
index 3b3b045..eff8651 100644
--- a/systemtests/fixtures.py
+++ b/systemtests/fixtures.py
@@ -15,6 +15,7 @@ from pytest_steps import one_fixture_per_step
 from .api import UnauthenticatedMAASAPIClient
 from .config import ADMIN_EMAIL, ADMIN_PASSWORD, ADMIN_USER
 from .lxd import CLILXD, get_lxd
+from .o11y import setup_o11y
 from .region import MAASRegion
 from .tls import MAAS_CONTAINER_CERTS_PATH, setup_tls
 from .vault import Vault, VaultNotReadyError, setup_vault
@@ -494,46 +495,7 @@ def maas_region(
         yaml.dump(credentials, fh)
 
     if o11y := config.get("o11y"):
-        host_path_to_agent = o11y["grafana_agent_file_path"].strip()
-        agent_path = "/opt/agent/agent-linux-amd64"
-        if not lxd.file_exists(maas_container, agent_path):
-            lxd.execute(maas_container, ["mkdir", "-p", "/opt/agent"])
-            lxd.push_file(
-                maas_container,
-                host_path_to_agent,
-                agent_path,
-                mode="0755",
-            )
-            agent_maas_sample = "/usr/share/maas/grafana_agent/agent.yaml.example"
-            if installed_from_snap:
-                agent_maas_sample = f"/snap/maas/current{agent_maas_sample}"
-            lxd.execute(
-                maas_container,
-                ["cp", agent_maas_sample, "/opt/agent/agent.yml"],
-            )
-            o11y_ip = o11y["o11y_ip"]
-            # FIXME: Could we have an uniq identifier for each system-tests execution?
-            #        hostname could be the name of the jenkins job execution?
-            hostname = "maas-system-maas"
-            telemetry_run_cmd = dedent(
-                f"""
-            systemd-run -u telemetry \
-    -E HOSTNAME="{hostname}" \
-    -E AGENT_WAL_DIR="/var/lib/grafana-agent/wal" \
-    -E AGENT_POS_DIR="/var/lib/grafana-agent/positions" \
-    -E PROMETHEUS_REMOTE_WRITE_URL="http://{o11y_ip}:9090/api/v1/write"; \
-    -E LOKI_API_URL="http://{o11y_ip}:3100/loki/api/v1/push"; \
-    -E MAAS_LOGS="/var/snap/maas/common/log/" \
-    -E MAAS_IS_REGION="true" \
-    -E MAAS_IS_RACK="true" \
-    -E MAAS_AZ="default" \
-    {agent_path} \
-        -config.expand-env \
-        -config.file=/opt/agent/agent.yml \
-        -server.http.address="0.0.0.0:3100" -server.grpc.address="0.0.0.0:9095"
-            """
-            )
-            lxd.execute(maas_container, ["sh", "-c", telemetry_run_cmd])
+        setup_o11y(o11y, lxd, maas_container, installed_from_snap)
     yield region
 
 
diff --git a/systemtests/o11y.py b/systemtests/o11y.py
new file mode 100644
index 0000000..f26e1a6
--- /dev/null
+++ b/systemtests/o11y.py
@@ -0,0 +1,51 @@
+from textwrap import dedent
+from typing import TYPE_CHECKING, Any
+
+if TYPE_CHECKING:
+    from .lxd import CLILXD
+
+AGENT_PATH = "/opt/agent/agent-linux-amd64"
+
+
+def setup_o11y(
+    o11y: dict[str, Any], lxd: CLILXD, maas_container: str, installed_from_snap: bool
+) -> None:
+    if not lxd.file_exists(maas_container, AGENT_PATH):
+        host_path_to_agent = o11y["grafana_agent_file_path"].strip()
+        lxd.execute(maas_container, ["mkdir", "-p", "/opt/agent"])
+        lxd.push_file(
+            maas_container,
+            host_path_to_agent,
+            AGENT_PATH,
+            mode="0755",
+        )
+        agent_maas_sample = "/usr/share/maas/grafana_agent/agent.yaml.example"
+        if installed_from_snap:
+            agent_maas_sample = f"/snap/maas/current{agent_maas_sample}"
+        lxd.execute(
+            maas_container,
+            ["cp", agent_maas_sample, "/opt/agent/agent.yml"],
+        )
+        o11y_ip = o11y["o11y_ip"]
+        # FIXME: Could we have an uniq identifier for each system-tests execution?
+        #        hostname could be the name of the jenkins job execution?
+        hostname = "maas-system-maas"
+        telemetry_run_cmd = dedent(
+            f"""
+        systemd-run -u telemetry \
+-E HOSTNAME="{hostname}" \
+-E AGENT_WAL_DIR="/var/lib/grafana-agent/wal" \
+-E AGENT_POS_DIR="/var/lib/grafana-agent/positions" \
+-E PROMETHEUS_REMOTE_WRITE_URL="http://{o11y_ip}:9090/api/v1/write"; \
+-E LOKI_API_URL="http://{o11y_ip}:3100/loki/api/v1/push"; \
+-E MAAS_LOGS="/var/snap/maas/common/log/" \
+-E MAAS_IS_REGION="true" \
+-E MAAS_IS_RACK="true" \
+-E MAAS_AZ="default" \
+{AGENT_PATH} \
+    -config.expand-env \
+    -config.file=/opt/agent/agent.yml \
+    -server.http.address="0.0.0.0:3100" -server.grpc.address="0.0.0.0:9095"
+        """
+        )
+        lxd.execute(maas_container, ["sh", "-c", telemetry_run_cmd])

Follow ups