Skip to content

Commit

Permalink
fix: Collapses multiple logs under a single request id to one ndjson
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobo committed Oct 30, 2024
1 parent 5081c26 commit 2a864fc
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 56 deletions.
5 changes: 5 additions & 0 deletions .changeset/forty-maps-smash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@taskless/loader": patch
---

Optimizes local log output to individual requests
19 changes: 1 addition & 18 deletions src/dev/packcheck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,24 +69,7 @@ export async function packCheck(

msw.close();

// merge any logs that share a request id into a single output
const grouped = new Map<string, ConsolePayload>();

for (const log of logs) {
if (grouped.has(log.requestId)) {
const existingLog = grouped.get(log.requestId)!;
existingLog.sequenceIds.push(...log.sequenceIds);
existingLog.dimensions.push(...log.dimensions);
grouped.set(log.requestId, existingLog);
} else {
grouped.set(log.requestId, { ...log });
}
}

log(`[packCheck] Merged ${logs.length} logs into ${grouped.size} logs`);

const result = Array.from(grouped.values())[0] ?? {};
return result;
return logs[0];
}

export { type ConsolePayload } from "@~/types.js";
21 changes: 20 additions & 1 deletion src/lib/handler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { createSandbox, type Options } from "@~/lua/sandbox.js";
import {
type CaptureItem,
type ConsolePayload,
type CaptureCallback,
type HookName,
type Logger,
Expand All @@ -18,12 +20,14 @@ export const createHandler = ({
loaded,
factory,
logger,
useLogging,
capture,
getPacks,
}: {
loaded: Promise<boolean>;
factory: LuaFactory;
logger: Logger;
useLogging: boolean;
capture: CaptureCallback;
getPacks: () => Promise<Pack[]>;
}) =>
Expand Down Expand Up @@ -59,6 +63,11 @@ export const createHandler = ({

const cleanup: Array<() => Promise<void>> = [];

const logItem: ConsolePayload = {
requestId,
dimensions: [],
};

// create our engine for any matching packs
for (const pack of packs) {
// skip packs without hooks
Expand Down Expand Up @@ -90,7 +99,13 @@ export const createHandler = ({
permissions: pack.permissions,
request: info.request,
capture: {
callback: capture,
callback(entry: Omit<CaptureItem, "sequenceId">) {
capture(entry);
logItem.dimensions.push({
name: entry.dimension,
value: entry.value,
});
},
},
context: new Map(),
};
Expand Down Expand Up @@ -164,5 +179,9 @@ export const createHandler = ({
// cleanup
await Promise.allSettled(cleanup.map(async (step) => step()));

if (useLogging) {
logger.data(JSON.stringify(logItem));
}

return fetchResponse as StrictResponse<any>;
});
36 changes: 1 addition & 35 deletions src/lib/taskless.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,33 +194,6 @@ export const taskless = (
return networkPayload;
};

/** Sends a set of entries to the registered logging function */
const logEntries = (entries: CaptureItem[]) => {
// group all entries by their request id
const grouped = new Map<string, ConsolePayload>();

// convert entries to the grouped structure
for (const entry of entries) {
const group = grouped.get(entry.requestId) ?? {
requestId: entry.requestId,
sequenceIds: [],
dimensions: [],
};

group.sequenceIds.push(entry.sequenceId);
group.dimensions.push({
name: entry.dimension,
value: entry.value,
});

grouped.set(entry.requestId, group);
}

for (const [_id, line] of grouped.entries()) {
logger.data(JSON.stringify(line));
}
};

/** Flush all pending telemetry asynchronously */
const flush = async () => {
logger.trace("Flushing telemetry data");
Expand All @@ -229,10 +202,6 @@ export const taskless = (
? entriesToNetworkJson(entries)
: undefined;

if (useLogging) {
logEntries(entries);
}

if (
useNetwork &&
networkPayload &&
Expand Down Expand Up @@ -301,10 +270,6 @@ export const taskless = (
Atomics.wait(notifyHandle, 0, 0);
w.terminate();
}

if (useLogging) {
logEntries(entries);
}
};

const exitHandler = () => {
Expand Down Expand Up @@ -417,6 +382,7 @@ export const taskless = (
loaded,
factory,
logger,
useLogging,
capture,
getPacks: async () => packs,
});
Expand Down
2 changes: 0 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,6 @@ export type NetworkPayload = NonNullable<
export type ConsolePayload = {
/** The request ID. Can be used on the backend to merge related logs from a request */
requestId: string;
/** The sequenceIDs connected to this log entry */
sequenceIds: string[];
/** The dimension name & value that are recorded */
dimensions: Array<{
name: string;
Expand Down

0 comments on commit 2a864fc

Please sign in to comment.