8.0.1 • Published 7 months ago

@osskit/monitor v8.0.1

Weekly downloads
-
License
-
Repository
github
Last release
7 months ago

Install

yarn add @osskit/monitor

Usage

Scoped

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());

// Custom labeling
export const monitor = createMonitor<['my_label']>({ scope: 'metrics' });

const customLabels = (id: string) => await monitor('query', async () => db.query(id), {context: {key: 'myId' }, labeling: { 'my_label': 'label' } });

Unscoped

import monitor from '@osskit/monitor'

const result = await monitor('query', async () => db.query());

// Custom labeling 
const customLabels = (id: string) => await monitor('query', async () => db.query(id), {context: {key: 'myId' }, labeling: { 'my_label': 'label' } });

With monitor options

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 });

With global options

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,
  errorLogLevel: 'fatal'
});

setGlobalContext(() => getDynamicContext());

API

createMonitor({ scope: string, options?: MonitorOptions })

scope

Type: string

The scope of the monitor's metrics

Will be used as the Prometheus metric name

Returns an instance of a function that calls monitor - <T>(method: string, callable: () => T, options?: MonitorOptions<T>)

monitor(method: string, callable: () => T, options?: MonitorOptions)

method

Type: string

Will be used for the method label of the metric, or the metric name if no parent scope was declared

setGlobalOptions({ options: MonitorGlobalOptions })

Set a number of options that will be used globally for all monitor invocations

setGlobalContext(value: () => Record<string, string>)

Invoke a function that returns a global context to use in all monitor invocation logs

Parameters

MonitorOptions

ParameterDescription
context?: booleanadd context that will be logged in all method's logs
logResult?: booleanlog the method's result
logExecutionStart?: booleanlog the start of the method's execution method.start
parseResult?: (e: any) => anytransform the method's result that will be returned
parseError?: (e: any) => anyif the method errored, transform the error that will be thrown
errorLogLevel?: pino.Levelif the method errored, which level should the message be, default - error
labeling?: Record<string, string>add custom labeled counters using keys and values

GlobalOptions

ParameterDescription
logResult?: booleanlog the monitored methods results
logExecutionStart?: booleanlog the start of the method's execution method.start
parseError?: (e: any) => anyif the method errored, transform the error that will be thrown
prometheusBuckets?: number[]use the following prometheus bucket list for monitor metrics across methods
logger?: BaseLoggersupply a pino BaseLogger for monitor to use in logging results
errorLogLevel?: pino.Levelif the method errored, which level should the message be, default - error

License

MIT License

7.1.0

10 months ago

8.0.1

7 months ago

8.0.0

7 months ago

7.0.0

1 year ago

6.0.0

2 years ago

5.2.0

2 years ago

5.1.1

2 years ago

5.1.0

2 years ago

5.0.0

2 years ago

3.0.0

2 years ago

4.0.0

2 years ago

2.6.1

2 years ago

2.6.0

2 years ago

2.5.1

2 years ago

2.5.0

2 years ago

1.2.0

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.4.1

2 years ago

1.4.0

2 years ago

1.3.0

2 years ago

1.1.2

2 years ago

2.3.0

2 years ago

2.2.0

2 years ago

2.3.2

2 years ago

2.4.0

2 years ago

2.3.1

2 years ago

2.3.3

2 years ago

2.1.0

2 years ago

2.0.0

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

0.0.17

3 years ago

0.0.14

3 years ago

0.0.15

3 years ago

0.0.16

3 years ago

0.0.13-beta.0

3 years ago

0.0.13-beta.1

3 years ago

0.0.13

3 years ago

0.0.10

3 years ago

0.0.11

3 years ago

0.0.12

3 years ago

0.0.9

3 years ago

0.0.8

3 years ago

0.0.7

3 years ago

0.0.3

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.6

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago