Merge pull request #3981 from bluewave-labs/fix/docker-capture-monitors

docker capture fixes
This commit is contained in:
Alexander Holliday authored and GitHub committed 2026-09-21 10:30:16 -07:00
commit c5303add56
14 files changed
+181 -52

No files matched your search

+1 -1
View File
@@ -144,7 +144,7 @@ The **Docker host** field accepts three forms:
| Remote daemon | `tcp://docker.example.com:2376` | Always uses mutual TLS; the port defaults to `2376`. Unencrypted daemons on `2375` are not supported. |
| Capture agent | `https://capture.example.com/api/v1/metrics/docker` | Enter the full endpoint ending in `/metrics/docker` and its authorization secret. Checkmate requests `all=true` so stopped containers are included. |
Capture-backed monitors normalize Capture's container state, health, CPU, memory, start time and exposed ports into the same Docker pages. Capture does not currently provide restart counts, mounts, published host-port bindings or container logs, so those values are shown as unavailable.
Capture-backed monitors normalize Capture's container state, health, CPU, memory, start time and exposed ports into the same Docker pages. Capture does not currently provide restart counts, mounts, published host-port bindings or container logs, so those values are shown as unavailable. Capture also reports a container whose health check is still starting as healthy, so the `starting` health state only appears on direct monitors, and stopped containers carry no CPU or memory metrics.
**Monitoring the local socket.** The reference Compose file does not mount the socket, so add it and grant the container the host's `docker` group. The image runs as an unprivileged user and cannot read the socket otherwise. Find the group id with `stat -c %g /var/run/docker.sock`, then:
+11 -4
View File
@@ -351,6 +351,7 @@ const CreateMonitorPage = () => {
const watchedType = watch("type") as MonitorType;
const watchedUrl = watch("url") as string;
const isCaptureDocker = watchedType === "docker" && isCaptureDockerUrl(watchedUrl);
const watchedMethod = watch("method") as HttpMethod | undefined;
const watchedProxyMode = watch("proxyMode") as ProxyMode | undefined;
const watchedUseAdvancedMatching = watch("useAdvancedMatching") as boolean;
@@ -381,6 +382,13 @@ const CreateMonitorPage = () => {
}
}, [watchedType, showTypeSelector, reset, clearErrors]);
// The logs switch is hidden for Capture hosts, so a value left over from a socket host must not be submitted.
useEffect(() => {
if (isCaptureDocker) {
setValue("dockerLogsEnabled", false);
}
}, [isCaptureDocker, setValue]);
const generalSettingsConfig = useMemo(
() => getGeneralSettingsConfig(watchedType, t),
[watchedType, t]
@@ -711,8 +719,7 @@ const CreateMonitorPage = () => {
)}
{/* Capture API secret - used by hardware and Capture-backed Docker monitors */}
{(generalSettingsConfig.showSecret ||
(watchedType === "docker" && isCaptureDockerUrl(watchedUrl))) && (
{(generalSettingsConfig.showSecret || isCaptureDocker) && (
<FormTextField
name="secret"
fieldLabel={t("pages.createMonitor.form.general.option.secret.label")}
@@ -721,7 +728,7 @@ const CreateMonitorPage = () => {
)}
/>
)}
{watchedType === "docker" && isCaptureDockerUrl(watchedUrl) && (
{isCaptureDocker && (
<FormSwitchField
name="ignoreTlsErrors"
label={t("pages.createMonitor.form.ignoreTls.option.tls.label")}
@@ -954,7 +961,7 @@ const CreateMonitorPage = () => {
/>
)}
{showStep(1) && watchedType === "docker" && !isCaptureDockerUrl(watchedUrl) && (
{showStep(1) && watchedType === "docker" && !isCaptureDocker && (
<ConfigBox
title={t("pages.createMonitor.form.dockerLogs.title")}
subtitle={t("pages.createMonitor.form.dockerLogs.description")}
@@ -10,6 +10,7 @@ import type { DockerContainerInfo } from "@/Types/Check";
// Utils
import { formatDuration } from "@/Utils/TimeUtils";
import { PLACEHOLDER } from "@/Utils/FormatUtils";
import { getDockerPalette } from "@/Utils/MonitorUtils";
export const DockerContainerStatusBoxes = ({
@@ -40,11 +41,11 @@ export const DockerContainerStatusBoxes = ({
/>
<StatBox
title={t("common.labels.uptime")}
subtitle={runningFor === undefined ? "—" : formatDuration(runningFor)}
subtitle={runningFor === undefined ? PLACEHOLDER : formatDuration(runningFor)}
/>
<StatBox
title={t("common.labels.restarts")}
subtitle={restartCount === undefined ? "—" : String(restartCount)}
subtitle={restartCount === undefined ? PLACEHOLDER : String(restartCount)}
/>
<StatBox
title={t("common.labels.health")}
@@ -13,7 +13,7 @@ import type { Theme } from "@mui/material";
import type { TFunction } from "i18next";
import type { DockerContainerStats } from "@/Types/Monitor";
import { LAYOUT } from "@/Utils/Theme/constants";
import { formatPercentage } from "@/Utils/FormatUtils";
import { formatPercentage, PLACEHOLDER } from "@/Utils/FormatUtils";
import prettyBytes from "pretty-bytes";
import { BaseBox } from "@/Components/design-elements";
import { dedupeDockerPorts, getDockerMountLabel } from "@/Utils/MonitorUtils";
@@ -88,7 +88,7 @@ export const TabOverview = ({
Ports
</Typography>
<Stack>
{dedupedPorts.length === 0 && <Typography>—</Typography>}
{dedupedPorts.length === 0 && <Typography>{PLACEHOLDER}</Typography>}
{dedupedPorts.map((port) => {
const containerPort = `${port.privatePort}/${port.protocol}`;
const label =
@@ -117,7 +117,7 @@ export const TabOverview = ({
Volumes
</Typography>
<Stack>
{!mounts?.length && <Typography>—</Typography>}
{!mounts?.length && <Typography>{PLACEHOLDER}</Typography>}
{mounts?.map((mount) => {
return (
<Typography key={`${mount.source}/${mount.destination}`}>
@@ -12,7 +12,7 @@ import type { Header } from "@/Components/design-elements/Table";
import type { DockerContainerInfo } from "@/Types/Check";
// Utils
import { formatPercentage } from "@/Utils/FormatUtils";
import { formatPercentage, PLACEHOLDER } from "@/Utils/FormatUtils";
interface DockerContainersTableProps {
monitorId: string | undefined;
@@ -91,7 +91,7 @@ export const DockerContainersTable = ({
id: "restarts",
content: t("common.labels.restarts"),
render: (row) => {
return <Typography>{row.restartCount}</Typography>;
return <Typography>{row.restartCount ?? PLACEHOLDER}</Typography>;
},
},
];
@@ -1,7 +1,7 @@
import Box from "@mui/material/Box";
import { DetailGauge } from "@/Components/design-elements";
import { getPercentage, formatPercentageFromWhole } from "@/Utils/FormatUtils";
import { getPercentage, formatPercentageFromWhole, PLACEHOLDER } from "@/Utils/FormatUtils";
import prettyBytes from "pretty-bytes";
import { useTranslation } from "react-i18next";
import { useTheme } from "@mui/material";
@@ -11,7 +11,6 @@ interface StatGaugesProps {
diagnostics: Diagnostics | null;
}
const PLACEHOLDER = "—";
export const StatGauges = ({ diagnostics }: StatGaugesProps) => {
const theme = useTheme();
+1 -1
View File
@@ -6,12 +6,12 @@ import { useTheme } from "@mui/material";
import { useTranslation } from "react-i18next";
import type { Diagnostics } from "@/Types/Diagnostics";
import { formatDuration, formatMs } from "@/Utils/TimeUtils";
import { PLACEHOLDER } from "@/Utils/FormatUtils";
interface StatsProps {
diagnostics: Diagnostics | null;
}
const PLACEHOLDER = "—";
export const Stats = ({ diagnostics }: StatsProps) => {
const { t } = useTranslation();
+3
View File
@@ -47,3 +47,6 @@ export const getPercentage = (value: number, total: number) => {
if (!value || !total) return 0;
return (value / total) * 100;
};
// Shown wherever a value is not available for the current data source.
export const PLACEHOLDER = "—";
+6 -15
View File
@@ -267,21 +267,12 @@ export const monitorSchema = monitorSchemaUnion.superRefine((data, ctx) => {
});
}
if (data.type === "docker" && isCaptureDockerUrl(data.url)) {
if (!data.secret.trim()) {
ctx.addIssue({
code: "custom",
path: ["secret"],
message: "Capture API secret is required",
});
}
if (data.dockerLogsEnabled) {
ctx.addIssue({
code: "custom",
path: ["dockerLogsEnabled"],
message: "Capture-backed Docker monitors do not support container logs",
});
}
if (data.type === "docker" && isCaptureDockerUrl(data.url) && !data.secret.trim()) {
ctx.addIssue({
code: "custom",
path: ["secret"],
message: "Capture API secret is required",
});
}
if (data.type === "docker" && isDockerTlsUrl(data.url)) {
+7 -12
View File
@@ -134,15 +134,11 @@ const refineDockerUrl = (data: { type?: string; url?: string }, ctx: z.Refinemen
}
};
const refineCaptureDocker =
(mode: "create" | "edit") => (data: { type?: string; url?: string; secret?: string; dockerLogsEnabled?: boolean }, ctx: z.RefinementCtx) => {
if (data.type !== "docker" || !isCaptureDockerUrl(data.url)) return;
const secretMissing = mode === "create" ? !data.secret?.trim() : data.secret !== undefined && !data.secret.trim();
if (secretMissing) ctx.addIssue({ code: "custom", path: ["secret"], message: "Capture API secret is required" });
if (data.dockerLogsEnabled) {
ctx.addIssue({ code: "custom", path: ["dockerLogsEnabled"], message: "Capture-backed Docker monitors do not support container logs" });
}
};
// Edits are checked in the monitor service against the stored secret, since the body may omit either the url or the secret.
const refineCaptureSecret = (data: { type?: string; url?: string; secret?: string }, ctx: z.RefinementCtx) => {
if (data.type !== "docker" || !isCaptureDockerUrl(data.url)) return;
if (!data.secret?.trim()) ctx.addIssue({ code: "custom", path: ["secret"], message: "Capture API secret is required" });
};
type DockerTlsFields = {
type?: string;
@@ -236,7 +232,7 @@ export const createMonitorBodyValidation = z
.superRefine(refineRegexPattern)
.superRefine(refineProxySelection)
.superRefine(refineDockerUrl)
.superRefine(refineCaptureDocker("create"))
.superRefine(refineCaptureSecret)
.superRefine(refineDockerTls("create"));
export const editMonitorBodyValidation = z
@@ -286,7 +282,6 @@ export const editMonitorBodyValidation = z
.superRefine(refineRegexPattern)
.superRefine(refineProxySelection)
.superRefine(refineDockerUrl)
.superRefine(refineCaptureDocker("edit"))
.superRefine(refineDockerTls("edit"));
export const pauseMonitorParamValidation = z.object({
@@ -366,7 +361,7 @@ const importedMonitorSchema = z
.superRefine(refineRegexPattern)
.superRefine(refineProxySelection)
.superRefine(refineDockerUrl)
.superRefine(refineCaptureDocker("create"));
.superRefine(refineCaptureSecret);
export const importMonitorsBodyValidation = z.object({
monitors: z.array(importedMonitorSchema).min(1, "At least one monitor is required"),
+25 -1
View File
@@ -34,7 +34,7 @@ import { IJobScheduler } from "@/worker/worker.interface.js";
import { DateRange } from "@/types/query.js";
import { IDockerLogsRepository } from "@/domain/docker/docker-log.repository.interface.js";
import { IEncryptionService } from "@/service/encryption/encryptionService.js";
import { isDockerTlsUrl } from "@/utils/dockerHost.js";
import { isCaptureDockerUrl, isDockerTlsUrl } from "@/utils/dockerHost.js";
const SERVICE_NAME = "MonitorService";
@@ -194,6 +194,9 @@ export class MonitorService implements IMonitorService {
// Encrypt docker TLS keys
this.encryptDockerTls(body, "create");
if (body.type === "docker" && isCaptureDockerUrl(body.url)) {
body.dockerLogsEnabled = false;
}
const monitor = await this.monitorsRepository.create(body, teamId, userId);
if (!monitor) {
@@ -561,6 +564,7 @@ export class MonitorService implements IMonitorService {
});
}
}
await this.normalizeCaptureDocker(body, monitorId, teamId);
const editedMonitor = await this.monitorsRepository.updateById(monitorId, teamId, body, { unsetProxyId });
await this.scheduler.updateJob(editedMonitor);
return editedMonitor;
@@ -749,6 +753,26 @@ export class MonitorService implements IMonitorService {
return { imported: createdMonitors!.length, errors };
};
// Capture-backed Docker monitors authenticate with the API secret and never collect logs; socket and TLS hosts have no secret.
private normalizeCaptureDocker = async (body: Partial<Monitor>, monitorId: string, teamId: string) => {
if (body.type !== "docker") return;
if (body.url !== undefined && !isCaptureDockerUrl(body.url)) {
body.secret = "";
return;
}
if (body.url === undefined && body.secret === undefined) return;
const stored = await this.monitorsRepository.findById(monitorId, teamId);
if (body.url === undefined && !isCaptureDockerUrl(stored.url)) return;
body.dockerLogsEnabled = false;
const secret = body.secret === undefined ? stored.secret : body.secret.trim();
if (!secret) {
throw new AppError({ message: "Capture API secret is required", status: 422, service: SERVICE_NAME, method: "editMonitor" });
}
};
private encryptDockerTls = (body: Partial<Monitor>, mode: "create" | "edit") => {
if (body.type !== "docker" && mode === "create") return;
+19 -9
View File
@@ -151,14 +151,16 @@ export class DockerProvider implements IStatusProvider<DockerStatusPayload> {
private toCaptureContainer(container: CaptureDockerContainer): DockerContainerInfo {
const startedAt = container.started_at && container.started_at > 0 ? new Date(container.started_at * 1000).toISOString() : undefined;
const stats = container.stats
? {
cpuPct: container.stats.cpu_percent / 100,
memoryUsedBytes: container.stats.memory_usage,
memoryLimitBytes: container.stats.memory_limit,
memoryPct: container.stats.memory_percent / 100,
}
: {};
// Capture reports zeroed stats for stopped containers; only running ones carry real metrics, matching toContainerInfo.
const stats =
container.status === "running" && container.stats
? {
cpuPct: container.stats.cpu_percent / 100,
memoryUsedBytes: container.stats.memory_usage,
memoryLimitBytes: container.stats.memory_limit,
memoryPct: container.stats.memory_percent / 100,
}
: {};
return {
id: container.container_id,
name: container.container_name || container.container_id.slice(0, 12),
@@ -177,6 +179,14 @@ export class DockerProvider implements IStatusProvider<DockerStatusPayload> {
};
}
private isCapturePayload = (payload: unknown): payload is CaptureDockerStatusPayload => {
if (!payload || typeof payload !== "object") return false;
const { data, errors } = payload as Partial<CaptureDockerStatusPayload>;
if (!Array.isArray(data) && data !== null) return false;
if (errors === undefined || errors === null) return true;
return Array.isArray(errors) && errors.every((error) => Array.isArray(error?.metric));
};
private handleCapture = async (monitor: Monitor, ctx?: CheckContext): Promise<MonitorStatusResponse<DockerStatusPayload>> => {
const captureUrl = new URL(monitor.url.trim());
captureUrl.searchParams.set("all", "true");
@@ -184,7 +194,7 @@ export class DockerProvider implements IStatusProvider<DockerStatusPayload> {
if (!response.status) return this.failed(monitor, response.code, response.message, response.responseTime ?? 0);
const payload = response.payload;
if (!payload || typeof payload === "string" || (!Array.isArray(payload.data) && payload.data !== null)) {
if (!this.isCapturePayload(payload)) {
this.logger.warn({
message: "Capture returned an invalid Docker metrics payload",
service: SERVICE_NAME,
@@ -289,6 +289,46 @@ describe("DockerProvider", () => {
message: "Capture returned an invalid Docker metrics payload",
});
});
it("rejects a malformed errors field as an invalid payload instead of throwing", async () => {
const httpHandle = jest.fn();
const { provider } = setup({ httpHandle });
const monitor = makeMonitor({ url: "http://capture:59232/api/v1/metrics/docker", secret: "secret" });
for (const errors of [{}, [{ err: "no metric" }], [{ metric: "docker.client", err: "x" }]]) {
httpHandle.mockResolvedValueOnce(makeCaptureResponse({ payload: { data: [], errors } }));
const result = await provider.handle(monitor);
expect(result).toMatchObject({
status: false,
code: NETWORK_ERROR,
message: "Capture returned an invalid Docker metrics payload",
});
}
});
it("records no stats for containers that are not running", async () => {
const response = makeCaptureResponse({
payload: {
data: [
makeCaptureContainer({
status: "exited",
running: false,
stats: { cpu_percent: 0, memory_usage: 0, memory_limit: 0, memory_percent: 0 },
}),
],
errors: [],
},
});
const { provider } = setup({ httpHandle: jest.fn().mockResolvedValue(response) });
const result = await provider.handle(makeMonitor({ url: "http://capture:59232/api/v1/metrics/docker", secret: "secret" }));
const [container] = result.payload!.containers;
expect(container.state).toBe("exited");
expect(container).not.toHaveProperty("cpuPct");
expect(container).not.toHaveProperty("memoryPct");
expect(container).not.toHaveProperty("memoryUsedBytes");
});
});
// ── TLS host urls ────────────────────────────────────────────────────
@@ -2160,4 +2160,63 @@ describe("MonitorService — Docker TLS credentials", () => {
expect(monitorsRepository.updateById).not.toHaveBeenCalled();
});
});
describe("editMonitor for Capture hosts", () => {
const captureUrl = "http://capture:59232/api/v1/metrics/docker";
it("rejects a url change to Capture with a 422 when no secret is stored or provided", async () => {
const monitorsRepository = createMonitorsRepositoryMock();
(monitorsRepository.findById as jest.Mock).mockResolvedValue(makeMonitor({ type: "docker", url: "unix:///var/run/docker.sock" }));
const { service } = createService({ monitorsRepository });
await expectAppError(
() => service.editMonitor({ teamId: TEAM_ID, monitorId: MONITOR_ID, body: { type: "docker", url: captureUrl } }),
422,
/Capture API secret is required/
);
expect(monitorsRepository.updateById).not.toHaveBeenCalled();
});
it("keeps the stored secret and disables logs when only the url changes to Capture", async () => {
const monitorsRepository = createMonitorsRepositoryMock();
(monitorsRepository.findById as jest.Mock).mockResolvedValue(makeMonitor({ type: "docker", secret: "stored-secret", dockerLogsEnabled: true }));
(monitorsRepository.updateById as jest.Mock).mockResolvedValue(makeMonitor({ type: "docker" }));
const { service } = createService({ monitorsRepository });
await service.editMonitor({ teamId: TEAM_ID, monitorId: MONITOR_ID, body: { type: "docker", url: captureUrl } });
const [, , patch] = (monitorsRepository.updateById as jest.Mock).mock.calls[0] as [string, string, Record<string, unknown>];
expect(patch).not.toHaveProperty("secret");
expect(patch.dockerLogsEnabled).toBe(false);
});
it("rejects a blank secret against a stored Capture url when the body omits the url", async () => {
const monitorsRepository = createMonitorsRepositoryMock();
(monitorsRepository.findById as jest.Mock).mockResolvedValue(makeMonitor({ type: "docker", url: captureUrl, secret: "stored-secret" }));
const { service } = createService({ monitorsRepository });
await expectAppError(
() => service.editMonitor({ teamId: TEAM_ID, monitorId: MONITOR_ID, body: { type: "docker", secret: " " } }),
422,
/Capture API secret is required/
);
expect(monitorsRepository.updateById).not.toHaveBeenCalled();
});
it("clears the secret without reading the stored monitor when the url changes to a socket", async () => {
const monitorsRepository = createMonitorsRepositoryMock();
(monitorsRepository.updateById as jest.Mock).mockResolvedValue(makeMonitor({ type: "docker" }));
const { service } = createService({ monitorsRepository });
await service.editMonitor({
teamId: TEAM_ID,
monitorId: MONITOR_ID,
body: { type: "docker", url: "unix:///var/run/docker.sock", secret: "stale-capture-secret" },
});
expect(monitorsRepository.findById).not.toHaveBeenCalled();
const [, , patch] = (monitorsRepository.updateById as jest.Mock).mock.calls[0] as [string, string, Record<string, unknown>];
expect(patch.secret).toBe("");
});
});
});