@compas/insight
Advanced tools
Comparing version 0.0.121 to 0.0.122
@@ -76,3 +76,3 @@ /** | ||
*/ | ||
export type EventCall = | ||
export type InsightEventCall = | ||
| { | ||
@@ -87,3 +87,3 @@ type: "start" | "stop"; | ||
} | ||
| EventCall[]; | ||
| InsightEventCall[]; | ||
@@ -93,5 +93,7 @@ /** | ||
*/ | ||
export interface Event { | ||
export interface InsightEvent { | ||
log: Logger; | ||
signal?: AbortSignal; | ||
/** | ||
@@ -104,3 +106,3 @@ * If event is first event dispatched in chain | ||
callStack: EventCall[]; | ||
callStack: InsightEventCall[]; | ||
} | ||
@@ -111,3 +113,3 @@ | ||
*/ | ||
export function newEvent(logger: Logger): Event; | ||
export function newEvent(logger: Logger, signal?: AbortSignal): InsightEvent; | ||
@@ -117,3 +119,3 @@ /** | ||
*/ | ||
export function newEventFromEvent(event: Event): Event; | ||
export function newEventFromEvent(event: InsightEvent): InsightEvent; | ||
@@ -123,3 +125,3 @@ /** | ||
*/ | ||
export function eventStart(event: Event, name: string): void; | ||
export function eventStart(event: InsightEvent, name: string): void; | ||
@@ -129,3 +131,3 @@ /** | ||
*/ | ||
export function eventRename(event: Event, name: string): void; | ||
export function eventRename(event: InsightEvent, name: string): void; | ||
@@ -135,11 +137,5 @@ /** | ||
*/ | ||
export function eventStop(event: Event): void; | ||
export function eventStop(event: InsightEvent): void; | ||
/** | ||
* Create a test event. | ||
* event.log.info is a noop by default, but can be enabled via the passed in options. | ||
*/ | ||
export function newTestEvent(options?: { enabledLogs?: boolean }): Event; | ||
/** | ||
* Get the disk size (in bytes) and estimated row count for all tables and views. | ||
@@ -146,0 +142,0 @@ * To improve accuracy, run sql`ANALYZE` before this query, however make sure to read the |
@@ -10,3 +10,2 @@ export { newLogger } from "./src/logger/logger.js"; | ||
eventStop, | ||
newTestEvent, | ||
newEventFromEvent, | ||
@@ -13,0 +12,0 @@ } from "./src/events.js"; |
{ | ||
"name": "@compas/insight", | ||
"version": "0.0.121", | ||
"version": "0.0.122", | ||
"description": "Simple logger in NDJSON format", | ||
@@ -33,5 +33,5 @@ "main": "./index.js", | ||
"engines": { | ||
"node": ">=14" | ||
"node": ">=15" | ||
}, | ||
"gitHead": "702a6109dd9b21867737f6c3d7dc7d2bb2cc5560" | ||
"gitHead": "22f4e7c17a5d515aaab9b844cd7dc467aab21987" | ||
} |
@@ -1,4 +0,31 @@ | ||
import { newLogger } from "@compas/insight"; | ||
import { inspect } from "util"; | ||
/** | ||
* Nested timing and call information | ||
* | ||
* @typedef {InsightEventCallObject|(InsightEventCall[])} InsightEventCall | ||
*/ | ||
/** | ||
* Basic timing and call information | ||
* | ||
* @typedef {object} InsightEventCallObject | ||
* @property {"start"|"stop"} type | ||
* @property {string} name | ||
* @property {number} time Time in milliseconds since some epoch. This can either be the | ||
* unix epoch or process start | ||
*/ | ||
/** | ||
* Encapsulate information needed to store events | ||
* | ||
* @typedef {object} InsightEvent | ||
* @property {Logger} log | ||
* @property {AbortSignal|undefined} signal | ||
* @property {boolean} root Check if the event is the root event in the chain | ||
* @property {string|undefined} [name] | ||
* @property {InsightEventCall[]} callStack | ||
*/ | ||
/** | ||
* Create a new event from a logger | ||
@@ -9,7 +36,9 @@ * | ||
* @param {Logger} logger Logger should have a context, like the default `ctx.log` | ||
* @returns {Event} | ||
* @param {AbortSignal|undefined} [signal] | ||
* @returns {InsightEvent} | ||
*/ | ||
export function newEvent(logger) { | ||
export function newEvent(logger, signal) { | ||
return { | ||
log: logger, | ||
signal, | ||
root: true, | ||
@@ -26,4 +55,4 @@ name: undefined, | ||
* | ||
* @param {Event} event | ||
* @returns {Event} | ||
* @param {InsightEvent} event | ||
* @returns {InsightEvent} | ||
*/ | ||
@@ -33,4 +62,10 @@ export function newEventFromEvent(event) { | ||
event.callStack.push(callStack); | ||
if (event.signal?.aborted) { | ||
throw new TimeoutError(event); | ||
} | ||
return { | ||
log: event.log, | ||
signal: event.signal, | ||
root: false, | ||
@@ -47,3 +82,3 @@ name: undefined, | ||
* | ||
* @param {Event} event | ||
* @param {InsightEvent} event | ||
* @param {string} name | ||
@@ -55,2 +90,6 @@ * @returns {void} | ||
if (event.signal?.aborted) { | ||
throw new TimeoutError(event); | ||
} | ||
event.callStack.push({ | ||
@@ -68,3 +107,3 @@ type: "start", | ||
* | ||
* @param {Event} event | ||
* @param {InsightEvent} event | ||
* @param {string} name | ||
@@ -76,2 +115,6 @@ * @returns {void} | ||
event.callStack[0].name = name; | ||
if (event.signal?.aborted) { | ||
throw new TimeoutError(event); | ||
} | ||
} | ||
@@ -84,3 +127,3 @@ | ||
* | ||
* @param {Event} event | ||
* @param {InsightEvent} event | ||
* @returns {void} | ||
@@ -104,25 +147,49 @@ */ | ||
/** | ||
* Create a new test event | ||
* Timeout error, shaped like an @compas/stdlib AppError | ||
* | ||
* @since 0.1.0 | ||
* | ||
* @param {{ enableLogs?: boolean }} [options={}] | ||
* @returns {Event} | ||
* @class | ||
*/ | ||
export function newTestEvent(options = {}) { | ||
const log = newLogger({ ctx: { type: "test-event" } }); | ||
export class TimeoutError extends Error { | ||
/** | ||
* | ||
* @param {InsightEvent} event | ||
*/ | ||
constructor(event) { | ||
super(); | ||
options.enableLogs = options.enableLogs ?? false; | ||
this.key = "error.server.internal"; | ||
this.status = 500; | ||
this.info = { | ||
message: "Operation aborted", | ||
event, | ||
}; | ||
// Disable logging by default | ||
if (!options.enableLogs) { | ||
log.info = () => {}; | ||
Object.setPrototypeOf(this, TimeoutError.prototype); | ||
} | ||
return { | ||
log, | ||
root: true, | ||
name: undefined, | ||
callStack: [], | ||
}; | ||
/** | ||
* Format as object when the TimeoutError is passed to console.log / console.error. | ||
* This works because it uses `util.inspect` under the hood. | ||
* Util#inspect checks if the Symbol `util.inspect.custom` is available. | ||
*/ | ||
[inspect.custom]() { | ||
return { | ||
key: this.key, | ||
status: this.status, | ||
info: this.info, | ||
}; | ||
} | ||
/** | ||
* Format as object when the TimeoutError is passed to JSON.stringify(). | ||
* This is used in the compas insight logger in production mode. | ||
*/ | ||
toJSON() { | ||
return { | ||
key: this.key, | ||
status: this.status, | ||
info: this.info, | ||
}; | ||
} | ||
} |
20445
576