Skip to content

Repository files navigation

GitHub Workflow StatusLicensecode style: prettier

Utilize a declarative API to wrap your functions to capture Prometheus metrics & logs for each function

Install

yarn add @osskit/monitor

Usage

Scoped

import{createMonitor}from'@osskit/monitor'exportconstmonitor=createMonitor({scope: 'metrics'});constresult1=awaitmonitor('query',async()=>db.query());constresult2=awaitmonitor('update',async()=>db.update());// Custom labelingexportconstmonitor=createMonitor<['my_label']>({scope: 'metrics'});constcustomLabels=(id: string)=>awaitmonitor('query',async()=>db.query(id),{context: {key: 'myId'},labeling: {'my_label': 'label'}});

Unscoped

importmonitorfrom'@osskit/monitor'constresult=awaitmonitor('query',async()=>db.query());// Custom labeling constcustomLabels=(id: string)=>awaitmonitor('query',async()=>db.query(id),{context: {key: 'myId'},labeling: {'my_label': 'label'}});

With monitor options

import{createMonitor}from'@osskit/monitor'exportconstmonitor=createMonitor({scope: 'metrics'});// Contextconstresult=(id: string)=>awaitmonitor('query',async()=>db.query(id),{context: { id }});// Parse & Log ResultsconstlogResults=(id: string)=>awaitmonitor('query',async()=>db.query(id),{logResult: true,parseResult: (res)=>res.prop});// Parse Errorconsterrored=(id: string)=>awaitmonitor('query',async()=>db.query(id),{logResult: true,parseError: (e)=>e.statusCode});// Log Execution StartconstexecutionStart=(id: string)=>awaitmonitor('query',async()=>db.query(id),{logExecutionStart: true});

With global options

import{setGlobalOptions,setGlobalContext}from'@osskit/monitor';importloggerfrom'./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 pinoBaseLogger for monitor to use in logging results
errorLogLevel?: pino.Levelif the method errored, which level should the message be, default - error

License

MIT License

About

Monitor your services declaratively

Topics

Resources

Code of conduct

Stars

19 stars

Watchers

1 watching

Forks

Used by

Contributors

Languages