feat(telemetry): add canonical VPS host monitoring

This commit is contained in:
DCCONSTRUCTIONS
2026-08-22 17:29:30 +03:00
parent a3e7a015de
commit 23ea96fbb2
33 changed files with 2541 additions and 51 deletions
@@ -0,0 +1,102 @@
import assert from "node:assert/strict";
import test from "node:test";
import { readFile } from "node:fs/promises";
import {
recordInfrastructureHostTelemetry,
} from "../src/host-telemetry-repository.mjs";
const edgeId = "73da0c42-a641-4559-b8f7-23509b60bfe9";
const hostId = "adf2a5b6-3c0b-4a39-998c-07dfb7818ad1";
const serviceId = "01f14736-f5c2-4867-9cbc-2d268996a871";
const projectId = "ad7b357c-c7ac-4bf8-a638-c7f956e9aa71";
const ownerId = "78da71d5-f48f-4de0-8e47-729f6d644151";
test("records a normalized host observation only through the Edge-to-host graph", async () => {
const queries = [];
const now = new Date();
const client = {
async query(sql, parameters) {
queries.push({ sql, parameters });
if (queries.length === 1) return { rows: [{
service_instance_id: serviceId,
host_id: hostId,
project_id: projectId,
owner_scope_id: ownerId,
host_key: "robot2b-b2-edge-vps",
}] };
if (queries.length === 2) return { rows: [{
id: "7ea94f66-6eed-4a27-8f04-e67060fa7e94",
received_at: now,
expires_at: new Date(now.valueOf() + 15_000),
replayed: false,
}] };
return { rows: [] };
},
};
const result = await recordInfrastructureHostTelemetry(client, {
authenticatedEdgeRef: `edge:${edgeId}`,
snapshot: snapshot(now.toISOString()),
});
assert.equal(result.status, "recorded");
assert.equal(result.replayed, false);
assert.equal(result.hostRef, `host:${hostId}`);
assert.match(queries[0].sql, /device_infrastructure_service_instances/);
assert.equal(queries[0].parameters[0], edgeId);
assert.match(queries[1].sql, /device_infrastructure_host_telemetry_samples/);
assert.equal(queries[1].parameters[5], edgeId);
assert.equal(JSON.stringify(queries).includes("password"), false);
assert.match(queries[2].sql, /delete from device_infrastructure_host_telemetry_samples/);
});
test("rejects telemetry whose host key disagrees with the canonical graph", async () => {
const client = {
async query() {
return { rows: [{
service_instance_id: serviceId,
host_id: hostId,
project_id: projectId,
owner_scope_id: ownerId,
host_key: "canonical-host",
}] };
},
};
await assert.rejects(
() => recordInfrastructureHostTelemetry(client, {
authenticatedEdgeRef: `edge:${edgeId}`,
snapshot: snapshot(new Date().toISOString()),
}),
/device_host_telemetry_host_key_mismatch/,
);
});
test("host telemetry migration is additive, observation-backed and secret-free", async () => {
const sql = await readFile(new URL("../migrations/017_infrastructure_host_telemetry.sql", import.meta.url), "utf8");
assert.match(sql, /create table if not exists device_infrastructure_host_telemetry_samples/);
assert.match(sql, /observation\.observation/);
assert.match(sql, /references device_infrastructure_hosts/);
assert.match(sql, /references device_infrastructure_service_instances/);
assert.doesNotMatch(sql, /insert into device_infrastructure_hosts/i);
assert.doesNotMatch(sql, /password|private.key|credential/i);
});
function snapshot(observedAt) {
return {
schemaVersion: "nodedc.infrastructure.host-telemetry.v1",
profile: "linux-host-telegraf-v1",
hostKey: "robot2b-b2-edge-vps",
observedAt,
source: { agent: "telegraf", agentVersion: "1.38.4", collectorRef: "service:nodedc-host-telemetry-agent" },
hardware: { hostname: "koffyvngij", architecture: "x64", platform: "linux", kernelRelease: "6.8.0", cpuModel: "KVM CPU", logicalProcessors: 1 },
cpu: { usagePercent: 20, load1: 0.2, load5: 0.1, load15: 0.05 },
memory: { totalBytes: 1024, availableBytes: 700, freeBytes: null, usedBytes: 324, usedPercent: 31.6 },
swap: { totalBytes: 0, availableBytes: null, freeBytes: 0, usedBytes: 0, usedPercent: 0 },
system: { uptimeSeconds: 100, users: 1, processes: { total: 10, running: 1, sleeping: 9, blocked: 0, zombies: 0 } },
disks: [],
network: [],
services: [],
};
}