Skip to content

Commit

Permalink
0.1.15
Browse files Browse the repository at this point in the history
  • Loading branch information
ovx committed Feb 27, 2024
1 parent 7365619 commit d0002fd
Show file tree
Hide file tree
Showing 13 changed files with 99 additions and 24 deletions.
9 changes: 9 additions & 0 deletions deno_dist/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import process from "node:process";
import { ValidationSchema } from './types.ts';
import { Inspector } from './inspector.ts';

export function isBun() {
return !!process.versions.bun;
}

export function isInspectorLike(inspector: any) {
return (
!!inspector &&
(inspector instanceof Inspector ||
('instruments' in inspector && 'createSession' in inspector))
);
}

export function getFunctionCallStack(
ignore: string[] = ['getFunctionCallStack']
): string[] {
Expand Down
19 changes: 13 additions & 6 deletions deno_dist/inspector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { LogsInstrument } from './instruments/logs.ts';
import { MetricsInstrument } from './instruments/metrics.ts';
import { NetworkInstrument } from './instruments/network.ts';
import { TracesInstrument } from './instruments/traces.ts';
import { MemoryStore } from './store.ts';
import defaultDashboards from './default-dashboards.ts';
import defaultMeasurements from './default-measurements.ts';
import type { Dashboard, InspectorInit, SessionInit } from './types.ts';
Expand All @@ -21,9 +22,9 @@ export class Inspector {
return defaultMeasurements;
}

env: Record<string, string> = {};
readonly env: Record<string, string> = {};

instruments: {
readonly instruments: {
errors: ErrorsInstrument;
events: EventsInstrument;
logs: LogsInstrument;
Expand All @@ -32,9 +33,9 @@ export class Inspector {
traces: TracesInstrument;
};

sessions: Set<Session> = new Set();
readonly sessions: Set<Session> = new Set();

store: Store;
readonly store: Store;

get info() {
const cpus = os.cpus();
Expand All @@ -54,8 +55,13 @@ export class Inspector {
};
}

constructor(init: InspectorInit) {
const { activate = true, env = true, instruments = {}, store } = init;
constructor(init: InspectorInit = {}) {
const {
activate = true,
env = true,
instruments = {},
store = new MemoryStore(),
} = init;
this.store = store;
this.instruments = {
errors: new ErrorsInstrument(store, instruments.errors),
Expand Down Expand Up @@ -102,6 +108,7 @@ export class Inspector {
return session;
}

/** @deprecated */
getInstrument(instrument: keyof typeof this.instruments) {
if (!this.instruments[instrument]) {
throw new Error(`Unknown instrument "${instrument}".`);
Expand Down
2 changes: 1 addition & 1 deletion deno_dist/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export interface InspectorInit {
activate?: boolean;
env?: boolean;
instruments?: InspectorInitInstruments;
store: Store;
store?: Store;
}

export interface ErrorsInstrumentValue {
Expand Down
1 change: 1 addition & 0 deletions dist/helpers.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ValidationSchema } from './types.js';
export declare function isBun(): boolean;
export declare function isInspectorLike(inspector: any): boolean;
export declare function getFunctionCallStack(ignore?: string[]): string[];
export declare function getModulesFromCallStack(stack: string[]): string[];
export declare function validate(schema: Record<string, ValidationSchema>, obj: any): void;
6 changes: 6 additions & 0 deletions dist/helpers.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { Inspector } from './inspector.js';
export function isBun() {
return !!process.versions.bun;
}
export function isInspectorLike(inspector) {
return (!!inspector &&
(inspector instanceof Inspector ||
('instruments' in inspector && 'createSession' in inspector)));
}
export function getFunctionCallStack(ignore = ['getFunctionCallStack']) {
const obj = {};
Error.captureStackTrace(obj);
Expand Down
11 changes: 6 additions & 5 deletions dist/inspector.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ import type { MeasurementConfig, Store } from '@exotjs/measurements/types';
export declare class Inspector {
static defaultDashboards(): Dashboard[];
static defaultMeasurements(): MeasurementConfig[];
env: Record<string, string>;
instruments: {
readonly env: Record<string, string>;
readonly instruments: {
errors: ErrorsInstrument;
events: EventsInstrument;
logs: LogsInstrument;
metrics: MetricsInstrument;
network: NetworkInstrument;
traces: TracesInstrument;
};
sessions: Set<Session>;
store: Store;
readonly sessions: Set<Session>;
readonly store: Store;
get info(): {
apiVersion: string;
arch: string;
Expand All @@ -34,10 +34,11 @@ export declare class Inspector {
runtimeVersion: string;
startedAt: number;
};
constructor(init: InspectorInit);
constructor(init?: InspectorInit);
destroy(): void;
activate(): void;
deactivate(): void;
createSession(init?: SessionInit): Session;
/** @deprecated */
getInstrument(instrument: keyof typeof this.instruments): ErrorsInstrument | EventsInstrument | LogsInstrument | MetricsInstrument | NetworkInstrument | TracesInstrument;
}
6 changes: 4 additions & 2 deletions dist/inspector.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { LogsInstrument } from './instruments/logs.js';
import { MetricsInstrument } from './instruments/metrics.js';
import { NetworkInstrument } from './instruments/network.js';
import { TracesInstrument } from './instruments/traces.js';
import { MemoryStore } from './store.js';
import defaultDashboards from './default-dashboards.js';
import defaultMeasurements from './default-measurements.js';
export class Inspector {
Expand Down Expand Up @@ -36,8 +37,8 @@ export class Inspector {
startedAt: performance.timeOrigin,
};
}
constructor(init) {
const { activate = true, env = true, instruments = {}, store } = init;
constructor(init = {}) {
const { activate = true, env = true, instruments = {}, store = new MemoryStore(), } = init;
this.store = store;
this.instruments = {
errors: new ErrorsInstrument(store, instruments.errors),
Expand Down Expand Up @@ -79,6 +80,7 @@ export class Inspector {
});
return session;
}
/** @deprecated */
getInstrument(instrument) {
if (!this.instruments[instrument]) {
throw new Error(`Unknown instrument "${instrument}".`);
Expand Down
2 changes: 1 addition & 1 deletion dist/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export interface InspectorInit {
activate?: boolean;
env?: boolean;
instruments?: InspectorInitInstruments;
store: Store;
store?: Store;
}
export interface ErrorsInstrumentValue {
attributes?: Record<string, any>;
Expand Down
9 changes: 9 additions & 0 deletions lib/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
import { ValidationSchema } from './types.js';
import { Inspector } from './inspector.js';

export function isBun() {
return !!process.versions.bun;
}

export function isInspectorLike(inspector: any) {
return (
!!inspector &&
(inspector instanceof Inspector ||
('instruments' in inspector && 'createSession' in inspector))
);
}

export function getFunctionCallStack(
ignore: string[] = ['getFunctionCallStack']
): string[] {
Expand Down
19 changes: 13 additions & 6 deletions lib/inspector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { LogsInstrument } from './instruments/logs.js';
import { MetricsInstrument } from './instruments/metrics.js';
import { NetworkInstrument } from './instruments/network.js';
import { TracesInstrument } from './instruments/traces.js';
import { MemoryStore } from './store.js';
import defaultDashboards from './default-dashboards.js';
import defaultMeasurements from './default-measurements.js';
import type { Dashboard, InspectorInit, SessionInit } from './types.js';
Expand All @@ -21,9 +22,9 @@ export class Inspector {
return defaultMeasurements;
}

env: Record<string, string> = {};
readonly env: Record<string, string> = {};

instruments: {
readonly instruments: {
errors: ErrorsInstrument;
events: EventsInstrument;
logs: LogsInstrument;
Expand All @@ -32,9 +33,9 @@ export class Inspector {
traces: TracesInstrument;
};

sessions: Set<Session> = new Set();
readonly sessions: Set<Session> = new Set();

store: Store;
readonly store: Store;

get info() {
const cpus = os.cpus();
Expand All @@ -54,8 +55,13 @@ export class Inspector {
};
}

constructor(init: InspectorInit) {
const { activate = true, env = true, instruments = {}, store } = init;
constructor(init: InspectorInit = {}) {
const {
activate = true,
env = true,
instruments = {},
store = new MemoryStore(),
} = init;
this.store = store;
this.instruments = {
errors: new ErrorsInstrument(store, instruments.errors),
Expand Down Expand Up @@ -102,6 +108,7 @@ export class Inspector {
return session;
}

/** @deprecated */
getInstrument(instrument: keyof typeof this.instruments) {
if (!this.instruments[instrument]) {
throw new Error(`Unknown instrument "${instrument}".`);
Expand Down
2 changes: 1 addition & 1 deletion lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ export interface InspectorInit {
activate?: boolean;
env?: boolean;
instruments?: InspectorInitInstruments;
store: Store;
store?: Store;
}

export interface ErrorsInstrumentValue {
Expand Down
11 changes: 9 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@exotjs/inspector",
"version": "0.1.14",
"description": "Exot application inspector",
"version": "0.1.15",
"description": "Exot Inspector - monitoring, tracing and debugging for Node.js, Bun and Deno.",
"author": "Daniel Regeci",
"license": "MIT",
"keywords": [
Expand Down Expand Up @@ -36,6 +36,10 @@
"types": "./dist/index.d.ts",
"import": "./dist/index.js"
},
"./helpers": {
"types": "./dist/helpers.d.ts",
"import": "./dist/helpers.js"
},
"./session": {
"types": "./dist/session.d.ts",
"import": "./dist/session.js"
Expand All @@ -50,6 +54,9 @@
"types": [
"./dist/types"
],
"helpers": [
"./dist/helpers"
],
"session": [
"./dist/session"
],
Expand Down
26 changes: 26 additions & 0 deletions tests/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,37 @@ import { describe, expect, it } from 'vitest';
import {
getFunctionCallStack,
getModulesFromCallStack,
isInspectorLike,
validate,
} from '../lib/helpers.js';
import { Inspector } from '../lib/inspector.js';
import { ValidationSchema } from '../lib/types.js';

describe('helpers', () => {
describe('isInspectorLike()', () => {
it('should return false if undefined', () => {
expect(isInspectorLike(void 0)).toEqual(false);
});

it('should return false if null', () => {
expect(isInspectorLike(null)).toEqual(false);
});

it('should return false if an object', () => {
expect(isInspectorLike({})).toEqual(false);
});

it('should return true if instance', () => {
expect(isInspectorLike(new Inspector())).toEqual(true);
});

it('should return true if an instance-compatible object', () => {
expect(
isInspectorLike({ instruments: {}, createSession: () => {} })
).toEqual(true);
});
});

describe('validate()', () => {
it('should throw if the object does not match the schema', () => {
expect(() =>
Expand Down

0 comments on commit d0002fd

Please sign in to comment.