feat(nacos): show service instance health at a glance

Closes #10116
This commit is contained in:
t8y2 committed 2026-09-27 05:54:32 +00:00
1 parent 5f8bcf74a5
commit e797954334
16 files changed
+273 -17

No files matched your search

@@ -1,8 +1,21 @@
import { describe, expect, it } from "vitest";
import { nacosInstanceMatchesPatch, nacosInstanceRefIdentity, nacosIpAddressIsValid, nacosJsonObjectMatches, nacosServiceDetailMatches } from "../nacosServiceManagement";
import { nacosInstanceMatchesPatch, nacosInstanceRefIdentity, nacosIpAddressIsValid, nacosJsonObjectMatches, nacosServiceDetailMatches, nacosServiceInstanceHealthSummary } from "../nacosServiceManagement";
describe("Nacos service management state reconciliation", () => {
it.each([
[{ ipCount: 3, healthyInstanceCount: 3 }, "allHealthy"],
[{ ipCount: 3, healthyInstanceCount: 2 }, "partiallyHealthy"],
[{ ipCount: 3, healthyInstanceCount: 0 }, "noHealthyInstances"],
[{ ipCount: 0, healthyInstanceCount: 0 }, "noInstances"],
] as const)("classifies the service instance health matrix for %o", (service, status) => {
expect(nacosServiceInstanceHealthSummary(service)).toEqual({ status, healthy: service.healthyInstanceCount, total: service.ipCount });
});
it.each([{ ipCount: 2 }, { healthyInstanceCount: 1 }, { ipCount: 1, healthyInstanceCount: 2 }, { ipCount: -1, healthyInstanceCount: 0 }])("does not infer health from incomplete or inconsistent counts for %o", (service) => {
expect(nacosServiceInstanceHealthSummary(service)).toBeNull();
});
it("keeps unknown, persistent and ephemeral instances as separate identities", () => {
const base = { serviceName: "api", ip: "127.0.0.1", port: 8080, clusterName: "blue" };
expect(new Set([nacosInstanceRefIdentity(base), nacosInstanceRefIdentity({ ...base, ephemeral: false }), nacosInstanceRefIdentity({ ...base, ephemeral: true })]).size).toBe(3);
@@ -1,4 +1,22 @@
import type { NacosInstanceInfo, NacosInstancePatch, NacosInstanceRef, NacosServiceDetail, NacosServiceUpsert } from "@/types/nacos";
import type { NacosInstanceInfo, NacosInstancePatch, NacosInstanceRef, NacosServiceDetail, NacosServiceInfo, NacosServiceUpsert } from "@/types/nacos";
export type NacosServiceInstanceHealth = "allHealthy" | "partiallyHealthy" | "noHealthyInstances" | "noInstances";
export interface NacosServiceInstanceHealthSummary {
status: NacosServiceInstanceHealth;
healthy: number;
total: number;
}
export function nacosServiceInstanceHealthSummary(service: Pick<NacosServiceInfo, "ipCount" | "healthyInstanceCount">): NacosServiceInstanceHealthSummary | null {
const total = service.ipCount;
const healthy = service.healthyInstanceCount;
if (total == null || healthy == null || !Number.isInteger(total) || !Number.isInteger(healthy) || total < 0 || healthy < 0 || healthy > total) return null;
if (total === 0) return { status: "noInstances", healthy, total };
if (healthy === total) return { status: "allHealthy", healthy, total };
if (healthy === 0) return { status: "noHealthyInstances", healthy, total };
return { status: "partiallyHealthy", healthy, total };
}
function canonicalJson(value: unknown): unknown {
if (Array.isArray(value)) return value.map(canonicalJson);