Utilize a declarative API to wrap your functions to capture Prometheus metrics & logs for each function
yarn add @osskit/monitor
import { createMonitor } from '@osskit/monitor'
export const monitor = createMonitor({ scope: 'metrics' });
const result1 = await monitor('query', async () => db.query());
const result2 = await monitor('update', async () => db.update());
// `metrics.query`, `metrics.update` for logs & metrics
import monitor from '@osskit/monitor'
const result = await monitor('query', async () => db.query());
// `query` for logs & metrics
import { createMonitor } from '@osskit/monitor'
export const monitor = createMonitor({ scope: 'metrics' });
// Context
const result = (id: string) => await monitor('query', async () => db.query(id), { context: { id } });
// Parse & Log Results
const logResults = (id: string) => await monitor('query', async () => db.query(id), { logResult: true, parseResult: (res) => res.prop });
// Parse Error
const errored = (id: string) => await monitor('query', async () => db.query(id), { logResult: true, parseError: (e) => e.statusCode });
// Log Execution Start
const executionStart = (id: string) => await monitor('query', async () => db.query(id), { logExecutionStart: true });
import { setGlobalOptions, setGlobalContext } from '@osskit/monitor';
import logger from './logger.js';
setGlobalOptions({
context: { globalContextId: 'bla' },
logResult: true,
logExecutionStart: false,
parseError: (res) => console.log(res),
prometheusBuckets: [0.0001, 0.1, 0.5, 10],
logger,
});
setGlobalContext(() => getDynamicContext());
Type: string
The scope of the monitor's metrics
Returns an instance of a function that calls monitor
- <T>(method: string, callable: () => T, options?: MonitorOptions<T>)
Type: string
{ context?: Record<string, any>; logResult?: boolean; logExecutionStart?: boolean; parseResult?: (res: any) => any; parseError?: (e: any) => any; }
{ context?: Record<string, any>; logResult?: boolean; logExecutionStart?: boolean; parseError?: (e: any) => any; prometheusBuckets?: number[]; logger?: BaseLogger; // pino's BaseLogger instance }
MIT License