docker capture fixes
This commit is contained in:
1 parent
d7f8bcc9e1
commit
4a2429d316
13 files changed
+180
-51
No files matched your search
@@ -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"),
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in new issue
Block a user