feat(nacos): add Nacos dashboard metrics

This commit is contained in:
二丫讲梵 authored and GitHub committed 2026-07-27 12:16:39 +08:00
1 parent 98bf3d1dfd
commit f9c2fb27d3
41 files changed
+3268 -36

No files matched your search

+31
View File
@@ -18,6 +18,8 @@ import {
isNacosConfigSaveSnapshotCurrent,
isNacosConfigDeleteSnapshotInScope,
nacosConfigFileExtension,
nacosMetricsCandidates,
normalizeNacosMetricsUrl,
parseNacosRawBody,
parseNacosRawQuery,
normalizeNacosEndpoint,
@@ -41,12 +43,41 @@ describe("nacosAdmin helpers", () => {
contextPath: "/gateway",
detectedVersion: "v3",
});
expect(normalizeNacosEndpoint("http://127.0.0.1:8848", { implementation: "nacos", versionMode: "v3" })).toMatchObject({
serverAddr: "http://127.0.0.1:8848",
contextPath: "/nacos",
detectedVersion: "v3",
});
expect(normalizeNacosEndpoint("http://127.0.0.1:8848", { implementation: "nacos", versionMode: "v3", contextPath: "/" })).toMatchObject({
serverAddr: "http://127.0.0.1:8848",
contextPath: "/",
detectedVersion: "v3",
});
const savedAutoRootEndpoint = normalizeNacosEndpoint("http://127.0.0.1:8080", { implementation: "nacos", versionMode: "auto" });
expect(savedAutoRootEndpoint).toMatchObject({ serverAddr: "http://127.0.0.1:8080", contextPath: "" });
expect(
normalizeNacosEndpoint(savedAutoRootEndpoint.serverAddr, {
implementation: "nacos",
versionMode: "auto",
contextPath: savedAutoRootEndpoint.contextPath || undefined,
}),
).toMatchObject({ serverAddr: "http://127.0.0.1:8080", contextPath: "" });
expect(normalizeNacosEndpoint("http://rnacos.example:8848/nacos", { implementation: "rnacos" })).toMatchObject({
serverAddr: "http://rnacos.example:8848",
contextPath: "/nacos",
});
expect(() => normalizeNacosEndpoint("http://user:secret@nacos.example", { implementation: "nacos" })).toThrow(/embedded credentials/i);
});
it("derives and validates Prometheus endpoints", () => {
expect(nacosMetricsCandidates("http://127.0.0.1:8818", "/nacos", "nacos")).toEqual(["http://127.0.0.1:8818/nacos/actuator/prometheus", "http://127.0.0.1:8818/actuator/prometheus"]);
expect(nacosMetricsCandidates("http://127.0.0.1:3848", "/nacos", "rnacos")).toEqual(["http://127.0.0.1:3848/metrics", "http://127.0.0.1:3848/nacos/metrics", "http://127.0.0.1:3848/rnacos/metrics"]);
expect(normalizeNacosMetricsUrl("http://localhost:8818/metrics?node=a")).toBe("http://localhost:8818/metrics?node=a");
expect(() => normalizeNacosMetricsUrl("file:///tmp/metrics")).toThrow(/HTTP or HTTPS/);
expect(() => normalizeNacosMetricsUrl("http://user:secret@localhost/metrics")).toThrow(/credentials/);
expect(() => normalizeNacosMetricsUrl("http://localhost/metrics#fragment")).toThrow(/fragment/);
expect(() => normalizeNacosMetricsUrl("http://localhost/metrics#")).toThrow(/fragment/);
});
it("parses raw query and body text", () => {
expect(parseNacosRawQuery("?dataId=a&group=DEFAULT_GROUP")).toEqual({ dataId: "a", group: "DEFAULT_GROUP" });
expect(parseNacosRawQuery("")).toBeUndefined();
@@ -0,0 +1,144 @@
import { describe, expect, it } from "vitest";
import { appendDashboardSample, averageDurationMsSeries, counterRateSeries, dashboardMetric, dashboardNamespaceLabel, dashboardSeries, errorRateSeries, formatDashboardPercent, isHealthyNacosNode, ratioPercent, type NacosDashboardSample } from "@/lib/nacos/nacosDashboard";
function sample(at: number, metrics: NacosDashboardSample["snapshot"]["metrics"]): NacosDashboardSample {
return {
at,
snapshot: {
namespace: "",
metrics,
nodes: [],
warnings: [],
},
};
}
describe("nacosDashboard helpers", () => {
it("reads metric series and leaves gaps for missing values", () => {
const samples = [sample(1, { serviceCount: 2 }), sample(2, { serviceCount: 5 }), sample(3, undefined)];
expect(dashboardMetric(samples[1], "serviceCount")).toBe(5);
expect(dashboardSeries(samples, "serviceCount")).toEqual([2, 5, null]);
});
it("calculates rates and leaves gaps after counter resets", () => {
const samples = [
{ ...sample(1_000, {}), snapshot: { ...sample(1_000, {}).snapshot, prometheus: { source: { kind: "nacos" as const, endpoint: "http://metrics" }, resource: {}, traffic: { httpRequestsTotal: 10 }, config: {}, naming: {} } } },
{ ...sample(3_000, {}), snapshot: { ...sample(3_000, {}).snapshot, prometheus: { source: { kind: "nacos" as const, endpoint: "http://metrics" }, resource: {}, traffic: { httpRequestsTotal: 20 }, config: {}, naming: {} } } },
{ ...sample(5_000, {}), snapshot: { ...sample(5_000, {}).snapshot, prometheus: { source: { kind: "nacos" as const, endpoint: "http://metrics" }, resource: {}, traffic: { httpRequestsTotal: 2 }, config: {}, naming: {} } } },
];
expect(counterRateSeries(samples, (item) => item.snapshot.prometheus?.traffic.httpRequestsTotal)).toEqual([null, 5, null]);
});
it("leaves counter-derived gaps when the Prometheus source changes", () => {
const samples: NacosDashboardSample[] = [
{
...sample(1_000, {}),
snapshot: {
...sample(1_000, {}).snapshot,
prometheus: { source: { kind: "nacos", endpoint: "http://metrics-a" }, resource: {}, traffic: { httpRequestsTotal: 10, httpErrorsTotal: 1, httpDurationSecondsTotal: 1, httpDurationCount: 10 }, config: {}, naming: {} },
},
},
{
...sample(2_000, {}),
snapshot: {
...sample(2_000, {}).snapshot,
prometheus: { source: { kind: "nacos", endpoint: "http://metrics-b" }, resource: {}, traffic: { httpRequestsTotal: 100, httpErrorsTotal: 20, httpDurationSecondsTotal: 50, httpDurationCount: 100 }, config: {}, naming: {} },
},
},
];
expect(counterRateSeries(samples, (item) => item.snapshot.prometheus?.traffic.httpRequestsTotal)).toEqual([null, null]);
expect(
averageDurationMsSeries(
samples,
(item) => item.snapshot.prometheus?.traffic.httpDurationSecondsTotal,
(item) => item.snapshot.prometheus?.traffic.httpDurationCount,
),
).toEqual([null, null]);
expect(
errorRateSeries(
samples,
(item) => item.snapshot.prometheus?.traffic.httpErrorsTotal,
(item) => item.snapshot.prometheus?.traffic.httpRequestsTotal,
),
).toEqual([null, null]);
});
it("uses the private source fingerprint when redacted endpoints match", () => {
const samples: NacosDashboardSample[] = [
{
...sample(1_000, {}),
snapshot: {
...sample(1_000, {}).snapshot,
prometheus: { source: { kind: "nacos", endpoint: "http://metrics", fingerprint: "source-a" }, resource: {}, traffic: { httpRequestsTotal: 10 }, config: {}, naming: {} },
},
},
{
...sample(2_000, {}),
snapshot: {
...sample(2_000, {}).snapshot,
prometheus: { source: { kind: "nacos", endpoint: "http://metrics", fingerprint: "source-b" }, resource: {}, traffic: { httpRequestsTotal: 20 }, config: {}, naming: {} },
},
},
];
expect(counterRateSeries(samples, (item) => item.snapshot.prometheus?.traffic.httpRequestsTotal)).toEqual([null, null]);
});
it("calculates average latency and error percentage from counter deltas", () => {
const samples: NacosDashboardSample[] = [
{
...sample(1_000, {}),
snapshot: {
...sample(1_000, {}).snapshot,
prometheus: { source: { kind: "nacos", endpoint: "http://metrics" }, resource: {}, traffic: { httpRequestsTotal: 100, httpErrorsTotal: 2, httpDurationSecondsTotal: 10, httpDurationCount: 100 }, config: {}, naming: {} },
},
},
{
...sample(2_000, {}),
snapshot: {
...sample(2_000, {}).snapshot,
prometheus: { source: { kind: "nacos", endpoint: "http://metrics" }, resource: {}, traffic: { httpRequestsTotal: 120, httpErrorsTotal: 4, httpDurationSecondsTotal: 14, httpDurationCount: 120 }, config: {}, naming: {} },
},
},
];
expect(
averageDurationMsSeries(
samples,
(item) => item.snapshot.prometheus?.traffic.httpDurationSecondsTotal,
(item) => item.snapshot.prometheus?.traffic.httpDurationCount,
),
).toEqual([null, 200]);
expect(
errorRateSeries(
samples,
(item) => item.snapshot.prometheus?.traffic.httpErrorsTotal,
(item) => item.snapshot.prometheus?.traffic.httpRequestsTotal,
),
).toEqual([null, 10]);
});
it("keeps the newest samples within the requested limit", () => {
let samples: NacosDashboardSample[] = [];
for (let at = 1; at <= 4; at++) samples = appendDashboardSample(samples, sample(at, {}), 3);
expect(samples.map((item) => item.at)).toEqual([2, 3, 4]);
});
it("normalizes ratio and percentage resource values", () => {
expect(ratioPercent(0.25)).toBe(25);
expect(ratioPercent(25)).toBe(25);
expect(formatDashboardPercent(0.125)).toBe("12.5%");
expect(formatDashboardPercent(undefined)).toBe("—");
});
it("prefers the dashboard response namespace over the requested fallback", () => {
expect(dashboardNamespaceLabel("prod", undefined)).toBe("prod");
expect(dashboardNamespaceLabel("", "dev")).toBe("public");
expect(dashboardNamespaceLabel(undefined, "dev")).toBe("dev");
expect(dashboardNamespaceLabel(undefined, undefined)).toBe("public");
});
it("derives node health from explicit state when alive is absent", () => {
expect(isHealthyNacosNode({ state: "UP" })).toBe(true);
expect(isHealthyNacosNode({ state: "DOWN" })).toBe(false);
expect(isHealthyNacosNode({ alive: false, state: "UP" })).toBe(false);
});
});