collector-worker: bound ingest POST with INGEST_PUSH_TIMEOUT_MS (BIT-2600)
Add AbortController timeout to pushEventsToIngest (error ingest_push_timeout), env INGEST_PUSH_TIMEOUT_MS (default 60s, minimum 1s), Vitest coverage, and compose/env.example documentation. Made-with: Cursor
This commit is contained in:
1 parent
b29f72bef4
commit
1099cbb724
7 files changed
+96
No files matched your search
@@ -31,6 +31,7 @@ ER_COLLECTOR_PROJECT_ALPHA_TOKEN=er_tst_a_secret_one
|
||||
# TICK_INTERVAL_MS=3000
|
||||
# SOURCE_REFRESH_MS=15000
|
||||
# HTTP_FETCH_TIMEOUT_MS=15000
|
||||
# INGEST_PUSH_TIMEOUT_MS=60000
|
||||
# MAX_BACKOFF_EXPONENT=8
|
||||
# COLLECTOR_DEFAULT_INGEST_TOKEN=
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ export type CollectorEnv = {
|
||||
DATABASE_URL: string;
|
||||
INGEST_BASE_URL: string;
|
||||
HTTP_FETCH_TIMEOUT_MS: number;
|
||||
INGEST_PUSH_TIMEOUT_MS: number;
|
||||
TICK_INTERVAL_MS: number;
|
||||
SOURCE_REFRESH_MS: number;
|
||||
MAX_BACKOFF_EXPONENT: number;
|
||||
@@ -30,10 +31,12 @@ export function loadEnv(): CollectorEnv {
|
||||
if (typeof _ingestBase !== "string" || _ingestBase.trim() === "") {
|
||||
throw new Error("INGEST_BASE_URL is required (e.g. http://ingest-api:3041)");
|
||||
}
|
||||
const _ingestPushMs = _parseIntWithDefault(process.env.INGEST_PUSH_TIMEOUT_MS, 60_000);
|
||||
return {
|
||||
DATABASE_URL: _databaseUrl.trim(),
|
||||
INGEST_BASE_URL: _ingestBase.trim().replace(/\/$/, ""),
|
||||
HTTP_FETCH_TIMEOUT_MS: _parseIntWithDefault(process.env.HTTP_FETCH_TIMEOUT_MS, 30_000),
|
||||
INGEST_PUSH_TIMEOUT_MS: Math.max(1000, _ingestPushMs),
|
||||
TICK_INTERVAL_MS: _parseIntWithDefault(process.env.TICK_INTERVAL_MS, 2000),
|
||||
SOURCE_REFRESH_MS: _parseIntWithDefault(process.env.SOURCE_REFRESH_MS, 10_000),
|
||||
MAX_BACKOFF_EXPONENT: _parseIntWithDefault(process.env.MAX_BACKOFF_EXPONENT, 8),
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import { pushEventsToIngest } from "./pushEvents.js";
|
||||
|
||||
const _sampleEvent = {
|
||||
schema_version: 1,
|
||||
occurred_at: "2026-04-12T10:00:00.000Z",
|
||||
level: "error",
|
||||
message: "test",
|
||||
} as const;
|
||||
|
||||
describe("pushEventsToIngest", () => {
|
||||
const _origFetch = global.fetch;
|
||||
|
||||
afterEach(() => {
|
||||
global.fetch = _origFetch;
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
it("short-circuits empty events without calling fetch", async () => {
|
||||
global.fetch = vi.fn() as typeof fetch;
|
||||
const _r = await pushEventsToIngest({
|
||||
ingestBaseUrl: "http://ingest",
|
||||
bearerToken: "t",
|
||||
events: [],
|
||||
timeoutMs: 5000,
|
||||
});
|
||||
expect(_r).toEqual({ ok: true, status: 200, accepted: 0 });
|
||||
expect(global.fetch).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("returns ingest_push_timeout when the request is aborted", async () => {
|
||||
global.fetch = vi.fn(async (_url, init) => {
|
||||
await new Promise<Response>((_resolve, reject) => {
|
||||
const _sig = init?.signal;
|
||||
if (_sig === undefined) {
|
||||
reject(new Error("expected AbortSignal"));
|
||||
return;
|
||||
}
|
||||
if (_sig.aborted) {
|
||||
reject(new DOMException("The operation was aborted.", "AbortError"));
|
||||
return;
|
||||
}
|
||||
_sig.addEventListener("abort", () => {
|
||||
reject(new DOMException("The operation was aborted.", "AbortError"));
|
||||
});
|
||||
});
|
||||
return new Response();
|
||||
}) as typeof fetch;
|
||||
const _r = await pushEventsToIngest({
|
||||
ingestBaseUrl: "http://ingest",
|
||||
bearerToken: "t",
|
||||
events: [_sampleEvent],
|
||||
timeoutMs: 30,
|
||||
});
|
||||
expect(_r).toEqual({ ok: false, status: null, error: "ingest_push_timeout" });
|
||||
});
|
||||
|
||||
it("returns ok when ingest accepts the batch", async () => {
|
||||
global.fetch = vi.fn(
|
||||
async () =>
|
||||
new Response(null, {
|
||||
status: 200,
|
||||
}),
|
||||
) as typeof fetch;
|
||||
const _r = await pushEventsToIngest({
|
||||
ingestBaseUrl: "http://ingest",
|
||||
bearerToken: "t",
|
||||
events: [_sampleEvent],
|
||||
timeoutMs: 5000,
|
||||
});
|
||||
expect(_r).toEqual({ ok: true, status: 200, accepted: 1 });
|
||||
expect(global.fetch).toHaveBeenCalledWith(
|
||||
"http://ingest/v1/ingest/events",
|
||||
expect.objectContaining({
|
||||
method: "POST",
|
||||
signal: expect.any(AbortSignal),
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -8,11 +8,14 @@ export async function pushEventsToIngest(input: {
|
||||
ingestBaseUrl: string;
|
||||
bearerToken: string;
|
||||
events: IngestEventInput[];
|
||||
timeoutMs: number;
|
||||
}): Promise<PushIngestResult> {
|
||||
if (input.events.length === 0) {
|
||||
return { ok: true, status: 200, accepted: 0 };
|
||||
}
|
||||
const _url = `${input.ingestBaseUrl}/v1/ingest/events`;
|
||||
const _controller = new AbortController();
|
||||
const _timer = setTimeout(() => _controller.abort(), input.timeoutMs);
|
||||
try {
|
||||
const _res = await fetch(_url, {
|
||||
method: "POST",
|
||||
@@ -21,6 +24,7 @@ export async function pushEventsToIngest(input: {
|
||||
Authorization: `Bearer ${input.bearerToken}`,
|
||||
},
|
||||
body: JSON.stringify({ events: input.events }),
|
||||
signal: _controller.signal,
|
||||
});
|
||||
if (!_res.ok) {
|
||||
const _t = await _res.text();
|
||||
@@ -32,7 +36,12 @@ export async function pushEventsToIngest(input: {
|
||||
}
|
||||
return { ok: true, status: _res.status, accepted: input.events.length };
|
||||
} catch (_err) {
|
||||
if (_err instanceof Error && _err.name === "AbortError") {
|
||||
return { ok: false, status: null, error: "ingest_push_timeout" };
|
||||
}
|
||||
const _msg = _err instanceof Error ? _err.message : "ingest_post_failed";
|
||||
return { ok: false, status: null, error: _msg };
|
||||
} finally {
|
||||
clearTimeout(_timer);
|
||||
}
|
||||
}
|
||||
@@ -139,6 +139,7 @@ export async function runHttpSourceOnce(input: {
|
||||
ingestBaseUrl: env.INGEST_BASE_URL,
|
||||
bearerToken: _token,
|
||||
events: _fetched.events,
|
||||
timeoutMs: env.INGEST_PUSH_TIMEOUT_MS,
|
||||
});
|
||||
|
||||
if (!_push.ok) {
|
||||
|
||||
@@ -27,6 +27,7 @@ INGEST_BASE_URL=http://127.0.0.1:3041
|
||||
TICK_INTERVAL_MS=3000
|
||||
SOURCE_REFRESH_MS=15000
|
||||
HTTP_FETCH_TIMEOUT_MS=15000
|
||||
INGEST_PUSH_TIMEOUT_MS=60000
|
||||
MAX_BACKOFF_EXPONENT=8
|
||||
|
||||
# --- admin-ui (Next.js server-side API calls) — target the Fastify `api/` service (default PORT 3200) ---
|
||||
|
||||
@@ -99,6 +99,7 @@ services:
|
||||
TICK_INTERVAL_MS: ${TICK_INTERVAL_MS:-3000}
|
||||
SOURCE_REFRESH_MS: ${SOURCE_REFRESH_MS:-15000}
|
||||
HTTP_FETCH_TIMEOUT_MS: ${HTTP_FETCH_TIMEOUT_MS:-15000}
|
||||
INGEST_PUSH_TIMEOUT_MS: ${INGEST_PUSH_TIMEOUT_MS:-60000}
|
||||
MAX_BACKOFF_EXPONENT: ${MAX_BACKOFF_EXPONENT:-8}
|
||||
NODE_ENV: production
|
||||
ER_COLLECTOR_PROJECT_ALPHA_TOKEN: ${ER_COLLECTOR_PROJECT_ALPHA_TOKEN:-er_tst_a_secret_one}
|
||||
|
||||
Reference in new issue
Block a user