Skip to content

armgit / sie

Public Imported from GitHub
0

Open-source inference server and production cluster for all the models your agent needs.

fix(telemetry): preserve model-loading Prometheus metric name (#784)

Attila László Nagy04f808008897
diff --git a/deploy/helm/sie-cluster/templates/_otel-collector-config.tpl b/deploy/helm/sie-cluster/templates/_otel-collector-config.tpl
index 5ff9f4af5..d822deb01 100644
--- a/deploy/helm/sie-cluster/templates/_otel-collector-config.tpl
+++ b/deploy/helm/sie-cluster/templates/_otel-collector-config.tpl
@@ -249,6 +249,7 @@ processors:
- context: metric
statements:
- 'set(unit, "") where name == "sie.gateway.pool.pinned_model.loaded"'
+ - 'set(unit, "") where name == "sie.gateway.model_loads_in_progress"'
transform/prometheus_application_identity:
error_mode: propagate
metric_statements:
diff --git a/telemetry/contract.yaml b/telemetry/contract.yaml
index 167b1fe9f..597d535aa 100644
--- a/telemetry/contract.yaml
+++ b/telemetry/contract.yaml
@@ -660,6 +660,9 @@ metric_processing:
sie.gateway.pool.pinned_model.loaded:
unit_override: ""
prometheus_name: sie_gateway_pool_pinned_model_loaded
+ sie.gateway.model_loads_in_progress:
+ unit_override: ""
+ prometheus_name: sie_gateway_model_loads_in_progress
datapoint_attributes:
policy: exact_metric_attribute_set
unknown: drop
diff --git a/tools/ci/tests/test_collector_metric_privacy.py b/tools/ci/tests/test_collector_metric_privacy.py
index 26b0b5f28..6eab1d8d8 100644
--- a/tools/ci/tests/test_collector_metric_privacy.py
+++ b/tools/ci/tests/test_collector_metric_privacy.py
@@ -3,7 +3,12 @@
from __future__ import annotations
import json
+import re
+import subprocess
+import sys
+import time
import urllib.request
+import uuid
from pathlib import Path
import pytest
@@ -12,6 +17,7 @@ from opentelemetry.proto.collector.metrics.v1.metrics_service_pb2 import ExportM
from opentelemetry.proto.metrics.v1.metrics_pb2 import AGGREGATION_TEMPORALITY_DELTA
from tools.ci.tests.test_collector_trace_privacy import SENTINEL, run, wait_for
+from tools.ci.tests.test_helm_render import render_collector_config
pytestmark = pytest.mark.docker
@@ -134,6 +140,286 @@ def batches(path: Path):
return [json.loads(line) for line in path.read_text().splitlines()]
+def model_loading_payload():
+ wire = ExportMetricsServiceRequest()
+ resource = wire.resource_metrics.add()
+ for key, value in {
+ "service.name": "sie-gateway",
+ "service.instance.id": "model-loading-test",
+ "service.version": "test-version",
+ }.items():
+ add(resource.resource.attributes, key, value)
+ metric = resource.scope_metrics.add().metrics.add(name="sie.gateway.model_loads_in_progress", unit="1")
+ for profile, bundle, value in [("cpu", "default", 0), ("l4", "default", 1), ("cpu", "candle", 0)]:
+ point = metric.gauge.data_points.add(time_unix_nano=time.time_ns(), as_double=value)
+ for key, label in {"pool": "default", "machine_profile": profile, "bundle": bundle}.items():
+ add(point.attributes, key, label)
+ return wire.SerializeToString()
+
+
+def require_cached_collector_image(image):
+ try:
+ run("docker", "image", "inspect", image)
+ except subprocess.CalledProcessError:
+ pytest.fail(
+ f"Docker must be available with {image} already cached; this test never pulls images.", pytrace=False
+ )
+
+
+def model_loading_container_user(directory):
+ owner = directory.stat()
+ return f"--user={owner.st_uid}:{owner.st_gid}"
+
+
+def model_loading_startup_diagnostics(container):
+ results = []
+ commands = (
+ [
+ "docker",
+ "inspect",
+ "--format",
+ "{{.State.Status}} exit={{.State.ExitCode}} oom={{.State.OOMKilled}}",
+ container,
+ ],
+ ["docker", "logs", "--tail", "20", container],
+ )
+ for command in commands:
+ try:
+ result = subprocess.run( # noqa: S603 - fixed diagnostics for the exact test-owned container
+ command, check=False, capture_output=True, text=True, errors="replace", timeout=5
+ )
+ results.append(f"{command[1]} exit={result.returncode}: {(result.stdout + result.stderr)[-4096:]}")
+ except (OSError, subprocess.SubprocessError) as error:
+ results.append(f"{command[1]} unavailable: {type(error).__name__}")
+ return "Collector startup diagnostics:\n" + "\n".join(results)
+
+
+def wait_for_model_loading_collector(container, health_check):
+ try:
+ wait_for(health_check)
+ except AssertionError as error:
+ error.add_note(model_loading_startup_diagnostics(container))
+ raise
+
+
+def cleanup_model_loading_collector(container, original_error):
+ try:
+ removed = subprocess.run( # noqa: S603 - exact test-owned container
+ ["docker", "rm", "--force", container], # noqa: S607 - local Docker CLI
+ check=False,
+ capture_output=True,
+ text=True,
+ timeout=20,
+ )
+ remaining = subprocess.run( # noqa: S603 - exact test-owned container
+ ["docker", "ps", "--all", "--filter", f"name=^/{container}$", "--format", "{{.Names}}"], # noqa: S607 - local Docker CLI
+ check=True,
+ capture_output=True,
+ text=True,
+ timeout=10,
+ )
+ if remaining.stdout.strip():
+ raise RuntimeError(f"test-owned collector was not removed: {container}")
+ if original_error is None:
+ removed.check_returncode()
+ except Exception as cleanup_error:
+ if original_error is not None:
+ raise BaseExceptionGroup("collector test and cleanup failed", [original_error, cleanup_error]) from None
+ raise
+
+
+def test_model_loading_image_prerequisite_never_pulls(monkeypatch):
+ calls = []
+
+ def missing(*args):
+ calls.append(args)
+ raise subprocess.CalledProcessError(1, args)
+
+ monkeypatch.setitem(require_cached_collector_image.__globals__, "run", missing)
+ image = "otel/opentelemetry-collector-contrib:0.119.0"
+ with pytest.raises(pytest.fail.Exception, match="already cached; this test never pulls"):
+ require_cached_collector_image(image)
+ assert calls == [("docker", "image", "inspect", image)]
+
+
+def test_model_loading_container_uses_private_fixture_owner(tmp_path):
+ tmp_path.chmod(0o700)
+ owner = tmp_path.stat()
+ assert model_loading_container_user(tmp_path) == f"--user={owner.st_uid}:{owner.st_gid}"
+ assert owner.st_mode & 0o777 == 0o700
+
+
[email protected]("diagnostics_fail", [False, True])
+def test_model_loading_startup_failure_retains_bounded_diagnostics(monkeypatch, diagnostics_fail):
+ original_error = AssertionError("collector not ready")
+ calls = []
+
+ def failed_wait(_predicate):
+ raise original_error
+
+ def fake_run(args, **kwargs):
+ calls.append((args, kwargs))
+ if diagnostics_fail:
+ raise subprocess.TimeoutExpired(args, kwargs["timeout"])
+ raw_output = b"exited exit=1 oom=false" if args[1] == "inspect" else b"x" * 8192 + b"permission denied\xff"
+ output = raw_output.decode("utf-8", errors=kwargs.get("errors", "strict"))
+ return subprocess.CompletedProcess(args, 0, output, "")
+
+ monkeypatch.setitem(wait_for_model_loading_collector.__globals__, "wait_for", failed_wait)
+ monkeypatch.setattr(subprocess, "run", fake_run)
+ with pytest.raises(AssertionError) as caught:
+ wait_for_model_loading_collector("sie-model-loading-test", lambda: True)
+ assert caught.value is original_error
+ (note,) = caught.value.__notes__
+ assert len(note) < 8300
+ assert "TimeoutExpired" in note if diagnostics_fail else "exited exit=1 oom=false" in note
+ if not diagnostics_fail:
+ assert "permission denied" in note
+ assert "\ufffd" in note
+ assert [args[1] for args, _ in calls] == ["inspect", "logs"]
+ assert calls[1][0] == ["docker", "logs", "--tail", "20", "sie-model-loading-test"]
+ assert all(args[-1] == "sie-model-loading-test" and kwargs["timeout"] == 5 for args, kwargs in calls)
+ assert all(kwargs["errors"] == "replace" for _, kwargs in calls)
+
+
[email protected]("removed", [False, True])
[email protected]("original_error", [None, RuntimeError("startup or test failure")])
+def test_model_loading_cleanup_preserves_failures(monkeypatch, removed, original_error):
+ container = "sie-model-loading-test"
+ calls = []
+
+ def fake_run(args, **kwargs):
+ calls.append((args, kwargs))
+ return subprocess.CompletedProcess(args, 0 if removed or args[1] == "ps" else 1, "", "")
+
+ monkeypatch.setattr(subprocess, "run", fake_run)
+ expected = type(original_error) if original_error else None if removed else subprocess.CalledProcessError
+ if expected:
+ with pytest.raises(expected) as caught:
+ try:
+ if original_error:
+ raise original_error
+ finally:
+ cleanup_model_loading_collector(container, sys.exception())
+ if original_error:
+ assert caught.value is original_error
+ else:
+ cleanup_model_loading_collector(container, None)
+ assert calls[0][0] == ["docker", "rm", "--force", container]
+ assert calls[1][0] == ["docker", "ps", "--all", "--filter", f"name=^/{container}$", "--format", "{{.Names}}"]
+ assert [kwargs["timeout"] for _, kwargs in calls] == [20, 10]
+
+
[email protected]("verification_fails", [False, True])
+def test_model_loading_cleanup_failure_reports_both_errors(monkeypatch, verification_fails):
+ original_error = RuntimeError("original failure")
+
+ def fake_run(args, **_kwargs):
+ if args[1] == "ps" and verification_fails:
+ raise subprocess.CalledProcessError(1, args)
+ return subprocess.CompletedProcess(args, 0, "sie-model-loading-test" if args[1] == "ps" else "", "")
+
+ monkeypatch.setattr(subprocess, "run", fake_run)
+ with pytest.raises(ExceptionGroup) as caught:
+ cleanup_model_loading_collector("sie-model-loading-test", original_error)
+ assert caught.value.exceptions[0] is original_error
+ assert isinstance(caught.value.exceptions[1], subprocess.CalledProcessError if verification_fails else RuntimeError)
+
+
+def test_model_loading_gauge_preserves_prometheus_name_and_remote_unit(tmp_path):
+ config, image = render_collector_config(tmp_path)
+ assert image == "otel/opentelemetry-collector-contrib:0.119.0"
+ require_cached_collector_image(image)
+ exporter = config["exporters"]["prometheus"]
+ assert exporter["add_metric_suffixes"] is True
+ exporter["endpoint"] = "0.0.0.0:9464"
+ config["exporters"] = {
+ "prometheus": exporter,
+ "file/remote": {"path": "/out/remote.json", "flush_interval": "100ms"},
+ }
+ pipelines = config["service"]["pipelines"]
+ config["service"]["pipelines"] = {
+ key: pipelines[key] for key in ["metrics/prometheus/gateway", "metrics/betterstack/gateway"]
+ }
+ pipelines["metrics/betterstack/gateway"]["exporters"] = ["file/remote"]
+ config["receivers"]["otlp/gateway"]["protocols"]["http"] = {"endpoint": "0.0.0.0:4338"}
+ config["processors"]["batch"]["timeout"] = "100ms"
+ (tmp_path / "collector.yaml").write_text(yaml.safe_dump(config))
+ container = f"sie-model-loading-{uuid.uuid4().hex}"
+ try:
+ run(
+ "docker",
+ "run",
+ "--detach",
+ "--pull=never",
+ "--name",
+ container,
+ "--cpus=1",
+ "--memory=256m",
+ "--pids-limit=64",
+ "--cap-drop=ALL",
+ "--security-opt=no-new-privileges",
+ "--read-only",
+ model_loading_container_user(tmp_path),
+ "-p",
+ "127.0.0.1::4338",
+ "-p",
+ "127.0.0.1::9464",
+ "-p",
+ "127.0.0.1::13133",
+ "-v",
+ f"{tmp_path}:/out",
+ image,
+ "--config=/out/collector.yaml",
+ )
+ ports = {port: run("docker", "port", container, f"{port}/tcp").split(":")[-1] for port in [4338, 9464, 13133]}
+
+ def get(port, path=""):
+ with urllib.request.urlopen(f"http://127.0.0.1:{ports[port]}{path}", timeout=2) as response:
+ return response.read(1_048_577).decode()
+
+ wait_for_model_loading_collector(container, lambda: get(13133))
+ request = urllib.request.Request(
+ f"http://127.0.0.1:{ports[4338]}/v1/metrics",
+ data=model_loading_payload(),
+ headers={"Content-Type": "application/x-protobuf"},
+ )
+ with urllib.request.urlopen(request, timeout=5) as response: # noqa: S310 - fixed loopback HTTP
+ assert response.status == 200
+ remote = wait_for(lambda: batches(tmp_path / "remote.json"))
+ text = wait_for(
+ lambda: value if "sie_gateway_model_loads_in_progress" in (value := get(9464, "/metrics")) else None
+ )
+ assert len(text) <= 1_048_576
+ assert "sie_gateway_model_loads_in_progress_ratio" not in text
+ samples = re.findall(
+ r"^sie_gateway_model_loads_in_progress\{([^}]+)\} ([01])(?:\.0)?(?: \d+)?$", text, re.MULTILINE
+ )
+ assert len(samples) == 3
+ observed = {}
+ for labels, value in samples:
+ attributes = dict(re.findall(r'(\w+)="([^"\\]*)"', labels))
+ assert attributes["pool"] == "default"
+ assert attributes["producer_service"] == "sie-gateway"
+ assert attributes["producer_instance"] == "model-loading-test"
+ assert re.fullmatch(r"[0-9a-f-]{36}", attributes["collector_generation"])
+ observed[attributes["machine_profile"], attributes["bundle"]] = int(value)
+ assert observed == {("cpu", "default"): 0, ("l4", "default"): 1, ("cpu", "candle"): 0}
+ metrics = [
+ metric
+ for batch in remote
+ for resource in batch["resourceMetrics"]
+ for scope in resource["scopeMetrics"]
+ for metric in scope["metrics"]
+ ]
+ assert len(metrics) == 1
+ assert metrics[0]["name"] == "sie.gateway.model_loads_in_progress"
+ assert metrics[0]["unit"] == "1"
+ assert sorted(point["asDouble"] for point in metrics[0]["gauge"]["dataPoints"]) == [0, 0, 1]
+ finally:
+ cleanup_model_loading_collector(container, sys.exception())
+
+
@pytest.mark.parametrize("receiver", ["gateway", "application"])
def test_remote_metric_maps_have_one_scalar_per_retained_key(tmp_path, receiver):
config, image = rendered_config()
diff --git a/tools/ci/tests/test_helm_render.py b/tools/ci/tests/test_helm_render.py
index dd1ed5659..3a156ff69 100644
--- a/tools/ci/tests/test_helm_render.py
+++ b/tools/ci/tests/test_helm_render.py
@@ -62,6 +62,45 @@ def render_template(tmp_path: Path, values: dict, template: str) -> subprocess.C
)
+def render_collector_config(tmp_path: Path, *, remote_enabled: bool = True) -> tuple[dict, str]:
+ chart = tmp_path / "collector-chart"
+ (chart / "templates").mkdir(parents=True)
+ (chart / "Chart.yaml").write_text("apiVersion: v2\nname: collector-test\nversion: 0.0.0\n")
+ shutil.copyfile(ROOT / helm.CHART_DIR / "templates/_otel-collector-config.tpl", chart / "templates/_collector.tpl")
+ (chart / "templates/config.yaml").write_text(
+ "apiVersion: v1\nkind: ConfigMap\nmetadata:\n name: test\ndata:\n collector.yaml: |\n"
+ ' {{- include "sie-cluster.otel.collectorConfig" .Values | nindent 4 }}\n'
+ )
+ defaults = yaml.safe_load((ROOT / helm.CHART_DIR / "values.yaml").read_text())
+ collector = defaults["observability"]["otel"]["collector"]
+ collector["betterStack"].update({"enabled": remote_enabled, "endpoint": "https://example.invalid"})
+ values = {
+ "metricsEnabled": True,
+ "logsEnabled": False,
+ "tracesEnabled": False,
+ "prometheusEnabled": True,
+ "collector": collector,
+ "traceEndpoint": "",
+ "logEndpoint": "",
+ "betterStack": collector["betterStack"],
+ "deploymentEnvironment": "dev",
+ "cloudRegion": "test-region",
+ "localTraceExporters": [],
+ "logExporters": [],
+ }
+ (chart / "values.yaml").write_text(yaml.safe_dump(values))
+ rendered = subprocess.run( # noqa: S603 - fixed local Helm test chart
+ ["mise", "exec", "--", "helm", "template", "test", str(chart)], # noqa: S607 - mise-managed Helm
+ cwd=ROOT,
+ capture_output=True,
+ text=True,
+ check=True,
+ timeout=30,
+ ).stdout
+ image = collector["image"]
+ return yaml.safe_load(yaml.safe_load(rendered)["data"]["collector.yaml"]), f"{image['repository']}:{image['tag']}"
+
+
def worker_statefulsets(tmp_path: Path, values: dict) -> list[dict]:
result = render_workers(tmp_path, values)
assert result.returncode == 0, result.stderr
@@ -174,6 +213,10 @@ def test_worker_scaledobject_appends_the_sixth_trigger(tmp_path: Path) -> None:
"sie_gateway_rejected_rate_l4_default",
"sie_gateway_model_loads_in_progress_l4_default",
]
+ loading = triggers[-1]["metadata"]
+ assert "max(sie_gateway_model_loads_in_progress{" in loading["query"]
+ assert "model_loads_in_progress_ratio" not in loading["query"]
+ assert loading["ignoreNullValues"] == "false"
hook = render_template(tmp_path, values, "templates/hooks/keda-ready-test.yaml")
assert hook.returncode == 0, hook.stderr
@@ -190,6 +233,37 @@ def test_worker_scaledobject_appends_the_sixth_trigger(tmp_path: Path) -> None:
assert job["spec"]["activeDeadlineSeconds"] == timeout + 60
[email protected]("remote_enabled", [False, True])
+def test_model_loading_unit_override_is_prometheus_only(tmp_path: Path, remote_enabled: bool) -> None:
+ name = "sie.gateway.model_loads_in_progress"
+ contract = yaml.safe_load((ROOT / "telemetry/contract.yaml").read_text())
+ (metric,) = [metric for metric in contract["metrics"] if metric["name"] == name]
+ assert metric["unit"] == "1"
+ assert metric["prometheus_name"] == "sie_gateway_model_loads_in_progress"
+ assert contract["metric_processing"]["prometheus_compatibility"][name] == {
+ "unit_override": "",
+ "prometheus_name": metric["prometheus_name"],
+ }
+ config, _ = render_collector_config(tmp_path, remote_enabled=remote_enabled)
+ processors = config["processors"]
+ compatibility = processors["transform/prometheus_compatibility"]["metric_statements"]
+ assert f'set(unit, "") where name == "{name}"' in compatibility[0]["statements"]
+ canonical = processors["transform/contract_metrics"]["metric_statements"]
+ (descriptor,) = [group for group in canonical if group["context"] == "metric"]
+ assert any(
+ statement.startswith('set(unit, "1")') and "sie[.]gateway[.]model_loads_in_progress" in statement
+ for statement in descriptor["statements"]
+ )
+ pipelines = config["service"]["pipelines"]
+ for receiver in ("gateway", "application"):
+ local = pipelines[f"metrics/prometheus/{receiver}"]["processors"]
+ assert local.index("transform/contract_metrics") < local.index("transform/prometheus_compatibility")
+ if remote_enabled:
+ assert (
+ "transform/prometheus_compatibility" not in pipelines[f"metrics/betterstack/{receiver}"]["processors"]
+ )
+
+
def test_scaledobject_apply_hook_also_runs_on_rollback(tmp_path: Path) -> None:
hooks = hook_events(tmp_path, AUTOSCALING_VALUES, KEDA_APPLY_TEMPLATE)