Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/alchemy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@
"worker": "./src/Cloudflare/Bridge.ts",
"import": "./lib/Cloudflare/Bridge.js"
},
"./Cloudflare/Workers/TestLoggerRuntime": {
"types": "./lib/Cloudflare/Workers/TestLoggerRuntime.d.ts",
"bun": "./src/Cloudflare/Workers/TestLoggerRuntime.ts",
"worker": "./src/Cloudflare/Workers/TestLoggerRuntime.ts",
"import": "./lib/Cloudflare/Workers/TestLoggerRuntime.js"
},
"./Cloudflare/Live": {
"types": "./lib/Cloudflare/Live.d.ts",
"bun": "./src/Cloudflare/Live.ts",
Expand Down
31 changes: 3 additions & 28 deletions packages/alchemy/src/Cli/commands/_shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,34 +169,9 @@ export const resourceFilter = Flag.string("filter").pipe(
Flag.map(Option.getOrUndefined),
);

export const TAIL_COLORS = [
"\x1b[36m", // cyan
"\x1b[35m", // magenta
"\x1b[33m", // yellow
"\x1b[32m", // green
"\x1b[34m", // blue
"\x1b[91m", // bright red
"\x1b[96m", // bright cyan
"\x1b[95m", // bright magenta
"\x1b[93m", // bright yellow
"\x1b[92m", // bright green
];
export const TAIL_RESET = "\x1b[0m";

export const formatLocalTimestamp = (date: Date): string => {
const y = date.getFullYear();
const mo = String(date.getMonth() + 1).padStart(2, "0");
const d = String(date.getDate()).padStart(2, "0");
const h = String(date.getHours()).padStart(2, "0");
const mi = String(date.getMinutes()).padStart(2, "0");
const s = String(date.getSeconds()).padStart(2, "0");
const ms = String(date.getMilliseconds()).padStart(3, "0");
const tz =
new Intl.DateTimeFormat("en-US", { timeZoneName: "short" })
.formatToParts(date)
.find((p) => p.type === "timeZoneName")?.value ?? "";
return `${y}-${mo}-${d} ${h}:${mi}:${s}.${ms} ${tz}`;
};
// Moved to `Tail.ts` so the shared tail orchestration (CLI + test harness)
// owns the formatting; re-exported here for the CLI commands that use them.
export { formatLocalTimestamp, TAIL_COLORS, TAIL_RESET } from "../../Tail.ts";

export const parseResourceFilter = (
filter: string | undefined,
Expand Down
55 changes: 5 additions & 50 deletions packages/alchemy/src/Cli/commands/tail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,25 @@ import * as Console from "effect/Console";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as Logger from "effect/Logger";
import * as Stream from "effect/Stream";
import * as Command from "effect/unstable/cli/Command";

import { findProviderByType, type LogLine } from "../../Provider.ts";
import { Stage } from "../../Stage.ts";
import * as State from "../../State/index.ts";
import * as Tail from "../../Tail.ts";
import { loadConfigProvider } from "../../Util/ConfigProvider.ts";
import { fileLogger } from "../../Util/FileLogger.ts";

import { AuthProviders } from "../../Auth/AuthProvider.ts";
import { withProfileOverride } from "../../Auth/Profile.ts";
import {
envFile,
formatLocalTimestamp,
importStack,
instrumentCommand,
parseResourceFilter,
profile,
resourceFilter,
script,
stage,
TAIL_COLORS,
TAIL_RESET,
} from "./_shared.ts";

export const tailCommand = Command.make(
Expand Down Expand Up @@ -62,7 +58,6 @@ export const tailCommand = Command.make(
const stack = yield* stackEffect;

yield* Effect.gen(function* () {
const state = yield* yield* State.State;
const filterSet = parseResourceFilter(filter);
const availableIds = [
...new Set(Object.values(stack.resources).map((r) => r.LogicalId)),
Expand All @@ -80,37 +75,9 @@ export const tailCommand = Command.make(
}
}

const fqns = Object.keys(stack.resources);
const tailable: {
logicalId: string;
stream: Stream.Stream<LogLine, any, any>;
}[] = [];

for (const fqn of fqns) {
const resource = stack.resources[fqn]!;
if (filterSet && !filterSet.has(resource.LogicalId)) continue;

const resourceState = yield* state.get({
stack: stack.name,
stage: stack.stage,
fqn,
});
if (!(resourceState as any)?.attr) continue;

const provider = yield* findProviderByType(resource.Type);
if (!provider.tail) continue;

tailable.push({
logicalId: resource.LogicalId,
stream: provider.tail({
id: resource.LogicalId,
fqn,
instanceId: (resourceState as any).instanceId,
props: (resourceState as any).props,
output: (resourceState as any).attr,
}),
});
}
const tailable = yield* Tail.discoverTailable(stack, {
filter: filterSet,
});

if (tailable.length === 0) {
if (filterSet) {
Expand All @@ -129,19 +96,7 @@ export const tailCommand = Command.make(
`Tailing: ${tailable.map((t) => t.logicalId).join(", ")}`,
);

const taggedStreams = tailable.map(({ logicalId, stream }, i) => {
const color = TAIL_COLORS[i % TAIL_COLORS.length]!;
return stream.pipe(
Stream.map(({ timestamp, message }) => {
const ts = formatLocalTimestamp(timestamp);
return `${color}${ts} [${logicalId}]${TAIL_RESET} ${message}`;
}),
);
});

yield* Stream.mergeAll(taggedStreams, {
concurrency: "unbounded",
}).pipe(Stream.runForEach((line) => Console.log(line)));
yield* Tail.tailAll(tailable);
}).pipe(Effect.provide(stack.services));
}).pipe(Effect.provide(services));
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ export const LocalWorkerProvider = () =>
: { kind: "effect", exports: props.exports ?? {} },
stack: { name: stack.name, stage: stack.stage },
extraOptions: props.build,
enableTestLogger: false,
} satisfies WorkerBundleOptions,
assets: props.assets,
dev: {
Expand Down
38 changes: 38 additions & 0 deletions packages/alchemy/src/Cloudflare/Workers/TestLoggerConstants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* Shared constants for the test-logging pipeline.
*
* Kept in a dependency-free file because both sides import it:
* - `TestLoggerRuntime.ts` is bundled INTO deployed Workers (imports
* `cloudflare:workers`, must not drag in node/distilled code), and
* - `TestLoggerWorker.ts` is deploy-side (distilled API + file locking,
* must not import `cloudflare:workers`).
*/
export const Constants = {
/** Account-level singleton worker hosting the log-buffer Durable Object. */
TEST_LOGGER_WORKER_NAME: "alchemy-test-logger",
/** The Durable Object class exported by the logger worker. */
TEST_LOGGER_CLASS_NAME: "AlchemyTestLogger",
/** DO namespace binding injected into workers deployed with test logging. */
TEST_LOGGER_DO_BINDING: "ALCHEMY_TEST_LOGGER",
/** Plain-text binding carrying the host worker's own script name. */
TEST_LOGGER_WORKER_NAME_BINDING: "ALCHEMY_TEST_LOGGER_WORKER_NAME",
} as const;

/**
* The Durable Object instance name for a stack+stage. One DO instance per
* deployed stack instance keeps runs isolated and bounds each instance's
* SQLite buffer to its own stack's logs.
*/
export const testLoggerInstanceName = (stack: {
name: string;
stage: string;
}): string => `${stack.name}/${stack.stage}`;

/** The shape of a buffered log row, as stored and streamed by the DO. */
export interface TestLogRow {
id: number;
worker: string;
message: string;
method: string;
timestamp: number;
}
95 changes: 95 additions & 0 deletions packages/alchemy/src/Cloudflare/Workers/TestLoggerRuntime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import { DurableObject, env, waitUntil } from "cloudflare:workers";
import { Constants, testLoggerInstanceName } from "./TestLoggerConstants.ts";

/**
* Runtime half of the test-logging pipeline. Bundled into Workers deployed
* with test logging enabled (see `WorkerBundle.ts`): the virtual entry calls
* {@link patchConsole} before the user module loads, so every `console.*`
* call is mirrored to the account's `alchemy-test-logger` Durable Object,
* which buffers rows and pushes them to any connected tail websocket.
*/

interface TestLoggerEnv {
ALCHEMY_STACK_NAME: string;
ALCHEMY_STAGE: string;
[Constants.TEST_LOGGER_WORKER_NAME_BINDING]: string;
[Constants.TEST_LOGGER_DO_BINDING]: DurableObjectNamespace<
DurableObject & {
log(entry: {
worker: string;
message: string;
method: string;
}): Promise<void>;
}
>;
}

const PATCHED_METHODS = [
"log",
"error",
"warn",
"info",
"debug",
"trace",
"dir",
] as const;
type PatchedMethod = (typeof PATCHED_METHODS)[number];

const hasTestLoggerEnv = (env: unknown): env is TestLoggerEnv =>
typeof env === "object" &&
env !== null &&
"ALCHEMY_STACK_NAME" in env &&
"ALCHEMY_STAGE" in env &&
Constants.TEST_LOGGER_DO_BINDING in env &&
Constants.TEST_LOGGER_WORKER_NAME_BINDING in env;

const render = (item: unknown): string => {
if (typeof item === "string") return item;
if (item instanceof Error) return item.stack ?? String(item);
try {
return JSON.stringify(item, (_key, value) =>
typeof value === "bigint" ? value.toString() : value,
);
} catch {
return String(item);
}
};

const patchConsoleMethod = (method: PatchedMethod) => {
const original = console[method];
console[method] = (...args: unknown[]) => {
original(...args);
if (!hasTestLoggerEnv(env)) return;
const stub = env[Constants.TEST_LOGGER_DO_BINDING].getByName(
testLoggerInstanceName({
name: env.ALCHEMY_STACK_NAME,
stage: env.ALCHEMY_STAGE,
}),
);
try {
waitUntil(
stub
.log({
worker: env[Constants.TEST_LOGGER_WORKER_NAME_BINDING],
message: args.map(render).join(" "),
method,
})
.catch(() => {}),
);
} catch {
// `waitUntil` throws outside a request context (e.g. logs during
// isolate startup) — the original console output already happened.
}
};
};

let patched = false;

/** Patch `console.*` to mirror log lines to the test-logger Durable Object. */
export const patchConsole = () => {
if (patched) return;
patched = true;
for (const method of PATCHED_METHODS) {
patchConsoleMethod(method);
}
};
Loading
Loading