diff --git a/src/__generated__/config.yaml b/src/__generated__/config.yaml index b642962..84b87fa 100644 --- a/src/__generated__/config.yaml +++ b/src/__generated__/config.yaml @@ -9,7 +9,8 @@ packs: logging found in APM-like solutions, and is a solid baseline for any observability stack. permissions: - domains: null + domains: + - .+ capture: durationMs: type: number @@ -28,10 +29,37 @@ packs: description: The path of the request in the form of '/path/to/resource' hooks: pre: | - context.set("domain", request.domain) - context.set("url", request.url) - context.set("path", request.path) + capture("domain", request.domain) + capture("url", request.url) + capture("path", request.path) context.set("start", now()) post: | - taskless.capture("durationMs", now() - context.get("start")) - taskless.capture("status", response.getStatus()) + capture("durationMs", now() - context.get("start")) + capture("status", response.status) + - schema: 2 + name: '@taskless/observe' + version: 1.0.0 + description: | + Taskless observability + permissions: + domains: + - .+ + response: + - body + capture: + error: + type: string + description: >- + In the event of errors, this contains the error message from common + expected payload locations + hooks: + post: | + local code = response.status + local msg = get(response.body, "error") or + get(response.body, "message") or + get(response.body, "err", "type") or + nil + + if code >= 400 and msg then + capture("error", msg) + end diff --git a/src/__generated__/openapi.ts b/src/__generated__/openapi.ts index 8ed9e4e..e5b019a 100644 --- a/src/__generated__/openapi.ts +++ b/src/__generated__/openapi.ts @@ -148,20 +148,10 @@ export default { }, "hooks": { "type": "object", - "properties": { - "pre": { - "description": "The hook script, written in lua", - "type": "string" - }, - "post": { - "description": "The hook script, written in lua", - "type": "string" - } - }, - "required": [ - "pre", - "post" - ] + "additionalProperties": { + "description": "The hook script, written in lua", + "type": "string" + } }, "displays": { "description": "A set of pre-configured graphs or display modules available in this pack", diff --git a/src/lib/taskless.ts b/src/lib/taskless.ts index d647c19..77dbf09 100644 --- a/src/lib/taskless.ts +++ b/src/lib/taskless.ts @@ -395,15 +395,6 @@ export const taskless = ( // unblock http mock, letting requests through setLoaded(true); logger.trace("Unblocking HTTP wrapper"); - - // attach cleanup to process exit - process.on("exit", cleanup); - }; - - const cleanup = () => { - logger.trace("Performing cleanup"); - // disable queue timer - clearTimeout(timer); }; /** @@ -515,7 +506,10 @@ export const autoload = (secret?: string, options?: InitOptions) => { const t = taskless(secret, options); t.logger.debug("Initialized Taskless"); try { - t.addDefaultPacks(); + if (!secret) { + t.addDefaultPacks(); + } + t.load() .then(() => { t.logger.debug("Taskless Autoloader ran successfully"); diff --git a/src/types.ts b/src/types.ts index 37ab979..df9af8c 100644 --- a/src/types.ts +++ b/src/types.ts @@ -9,13 +9,13 @@ export function isDefined(value: T): value is NonNullable { export function isLogLevel(value: unknown): value is LogLevel { return ( typeof value === "string" && - ["debug", "info", "warn", "error"].includes(value) + ["trace", "debug", "info", "warn", "error"].includes(value) ); } export type MaybePromise = T | Promise; -type LogLevel = "debug" | "info" | "warn" | "error"; +type LogLevel = "trace" | "debug" | "info" | "warn" | "error"; export type Logger = { trace?: (message: string) => void; diff --git a/test/env.spec.ts b/test/env.spec.ts index d6ac5c2..beb2cf3 100644 --- a/test/env.spec.ts +++ b/test/env.spec.ts @@ -35,7 +35,7 @@ describe("Taskless environment and importing (requires build)", () => { const { stdout, stderr } = await execa({ preferLocal: true, env: { - TASKLESS_LOG_LEVEL: "debug", + TASKLESS_LOG_LEVEL: "trace", }, cwd: resolve(dirname(fileURLToPath(import.meta.url)), "../"), })`node --import=./dist/index.js test/fixtures/end.js`; @@ -70,7 +70,7 @@ describe("Taskless environment and importing (requires build)", () => { const { stdout, stderr } = await execa({ preferLocal: true, env: { - TASKLESS_LOG_LEVEL: "debug", + TASKLESS_LOG_LEVEL: "trace", TASKLESS_API_KEY: "test", TASKLESS_OPTIONS, }, @@ -79,8 +79,8 @@ describe("Taskless environment and importing (requires build)", () => { expect(stdout).toMatch(/initialized taskless/i); expect(stdout).toMatch(/taskless autoloader ran successfully/i); - expect(stdout).toMatch(/performing cleanup/i); - // console.log(stdout); + expect(stdout).toMatch(/shutting down taskless/i); + expect(eventListener, "Mock event server was called").toBeCalledTimes(1); }); @@ -88,7 +88,7 @@ describe("Taskless environment and importing (requires build)", () => { const { stdout, stderr } = await execa({ preferLocal: true, env: { - TASKLESS_LOG_LEVEL: "debug", + TASKLESS_LOG_LEVEL: "trace", TASKLESS_OPTIONS: "network=false;logging=false", }, cwd: resolve(dirname(fileURLToPath(import.meta.url)), "../"),