Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add middleware to allow DEBUG=inngest:* debug logs #217

Closed
wants to merge 4 commits into from
Closed
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
5 changes: 5 additions & 0 deletions .changeset/strange-taxis-hide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"inngest": patch
---

Add middleware to allow `DEBUG=inngest:*` debug logs
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"canonicalize": "^1.0.8",
"chalk": "^4.1.2",
"cross-fetch": "^3.1.5",
"debug": "^4.3.4",
"h3": "^1.0.2",
"hash.js": "^1.1.7",
"json-stringify-safe": "^5.0.1",
Expand All @@ -74,6 +75,7 @@
"@jest/globals": "^29.5.0",
"@microsoft/api-extractor": "^7.33.7",
"@types/aws-lambda": "^8.10.108",
"@types/debug": "^4.1.8",
"@types/express": "^4.17.13",
"@types/jest": "^27.4.1",
"@types/json-stringify-safe": "^5.0.0",
Expand Down
94 changes: 93 additions & 1 deletion src/components/Inngest.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import debug from "debug";
import { InngestApi } from "../api/api";
import { envKeys } from "../helpers/consts";
import { devServerAvailable, devServerUrl } from "../helpers/devserver";
Expand All @@ -9,7 +10,7 @@ import {
skipDevServer,
} from "../helpers/env";
import { fixEventKeyMissingSteps, prettyError } from "../helpers/errors";
import { stringify } from "../helpers/strings";
import { slugify, stringify } from "../helpers/strings";
import { type SendEventPayload } from "../helpers/types";
import { DefaultLogger, ProxyLogger, type Logger } from "../middleware/logger";
import {
Expand Down Expand Up @@ -522,6 +523,97 @@ export class Inngest<TOpts extends ClientOptions = ClientOptions> {
* can correctly access comments on mutated input and output.
*/
const builtInMiddleware = (<T extends MiddlewareStack>(m: T): T => m)([
new InngestMiddleware({
name: "Inngest: Debug",
init({ client }) {
const clientDebug = debug(`inngest:${slugify(client.name)}`);

clientDebug("client initialized");

return {
onFunctionRun({ ctx, fn }) {
const runDebug = clientDebug.extend(`run:${ctx.runId}`);

runDebug("function run started", {
trigger: ctx.event?.name,
fn: { id: fn.id(client.name), name: fn.name },
});

return {
transformInput() {
runDebug(
"starting middleware function run input transformations"
);
},
beforeMemoization() {
runDebug(
"finished middleware function run input transformations"
);
runDebug("starting memoization");
},
afterMemoization() {
runDebug("finished memoization");
},
beforeExecution() {
runDebug("starting execution of new code during function run");
},
afterExecution() {
runDebug("finished execution of new code during function run");
},
transformOutput({ result, step }) {
const dataOutput = result.error
? [
"it failed with error:",
Object.prototype.hasOwnProperty.call(
result.error ?? {},
"message"
)
? (result.error as Record<string, unknown>).message
: result.error,
]
: ["it succeeded with output:", result.data];

if (step) {
runDebug(`ran step ${step.name} (${step.op});`, ...dataOutput);
} else {
runDebug("ran main function body;", ...dataOutput);
}

runDebug(
"starting middleware function run output transformations"
);
},
beforeResponse() {
runDebug(
"finished middleware function run output transformations"
);
runDebug("starting response from function run");
},
};
},
onSendEvent() {
const sendDebug = clientDebug.extend("send");

return {
transformInput({ payloads }) {
sendDebug(
"starting middleware send input transformations;",
`${payloads.length} detected payload(s) before`
);
},
transformOutput({ payloads }) {
sendDebug(
"finished middleware send input transformations;",
`${payloads.length} detected payload(s) after`
);

sendDebug("starting send output transformations");
},
};
},
};
},
}),
new InngestMiddleware({
name: "Inngest: Logger",
init({ client }) {
Expand Down
9 changes: 8 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1105,6 +1105,13 @@
dependencies:
"@types/node" "*"

"@types/debug@^4.1.8":
version "4.1.8"
resolved "https://registry.yarnpkg.com/@types/debug/-/debug-4.1.8.tgz#cef723a5d0a90990313faec2d1e22aee5eecb317"
integrity sha512-/vPO1EPOs306Cvhwv7KfVfYvOJqA/S/AXjaHQiJboCZzcNDb+TIJFN9/2C9DZ//ijSKWioNyUxD792QmDJ+HKQ==
dependencies:
"@types/ms" "*"

"@types/express-serve-static-core@^4.17.18":
version "4.17.30"
resolved "https://registry.yarnpkg.com/@types/express-serve-static-core/-/express-serve-static-core-4.17.30.tgz#0f2f99617fa8f9696170c46152ccf7500b34ac04"
Expand Down Expand Up @@ -1190,7 +1197,7 @@
resolved "https://registry.yarnpkg.com/@types/minimist/-/minimist-1.2.2.tgz#ee771e2ba4b3dc5b372935d549fd9617bf345b8c"
integrity sha512-jhuKLIRrhvCPLqwPcx6INqmKeiA5EWrsCOPhrlFSrbrmU4ZMPjj5Ul/oLCMDO98XRUIwVm78xICz4EPCektzeQ==

"@types/ms@^0.7.31":
"@types/ms@*", "@types/ms@^0.7.31":
version "0.7.31"
resolved "https://registry.yarnpkg.com/@types/ms/-/ms-0.7.31.tgz#31b7ca6407128a3d2bbc27fe2d21b345397f6197"
integrity sha512-iiUgKzV9AuaEkZqkOLDIvlQiL6ltuZd9tGcW3gwpnX8JbuiuhFlEGmmFXEXkN50Cvq7Os88IY2v0dkDqXYWVgA==
Expand Down