Monitored 🕵️♀️
A utility for monitoring services
Monitored is a wrapper function that writes success/error logs and StatsD metrics (gauge, increment, timing) after execution. It supports both asynchronous and synchronous functions.

Quick start
Yarn
yarn add monitored
Npm
npm install monitored
Initialize
Call setGlobalInstance
at the root of the project
To wire this package, you must pass an Options
object.
import { setGlobalInstance, Monitor } from 'monitored';
interface MonitorOptions {
serviceName: string;
plugins: MonitoredPlugin[];
logging?: {
logger: any;
logErrorsAsWarnings?: boolean;
disableSuccessLogs?: boolean;
};
shouldMonitorExecutionStart?: boolean;
mock?: boolean;
}
setGlobalInstance(
new Monitor({
serviceName: 'monitored-example',
logging: {
logger: logger,
logErrorsAsWarnings: false,
disableSuccessLogs: false,
},
plugins: [
new StatsdPlugin({
serviceName: 'test',
apiKey: 'key',
host: 'host',
root: 'root',
}),
new PrometheusPlugin(),
],
shouldMonitorExecutionStart: true,
})
);
API
monitored
After execution, a wrapper function writes success/error logs and StatsD metrics (gauge, increment, timing).
monitored
supports both Asynchronous and Synchronous functions:
const result = await monitored('functionName', async () => console.log('example'));
const result = monitored('functionName', () => {
console.log('example');
});
You can also pass a options
argument to monitored
:
type MonitoredOptions = {
context?: any;
logResult?: boolean;
parseResult?: (e: any) => any;
level?: 'info' | 'debug';
logAsError?: boolean;
logErrorAsInfo?: boolean
shouldMonitorError: e => boolean
shouldMonitorSuccess: (r: T) => boolean
tags?: Record<string, string>;
};
You can use context
to add more information to the log, such as user ID
const result = monitored(
'functionName',
() => {
console.log('example');
},
{context: {id: 'some context'}}
);
You can use tags
to add labels to metrics
const result = monitored(
'functionName',
() => {
console.log('example');
},
{tags: {'some-label': 'some-value'}}
);
Also, you can log the function result by setting logResult
to true
:
const result = monitored(
'functionName',
() => {
console.log('example');
},
{context: {id: 'some context'}, logResult: true}
);
flush
Wait until all current metrics are sent to the server.
We recommend using it at the end of lambda execution to ensure all metrics are sent.
import { getGlobalInstance } from 'monitored';
const flushTimeout: number = 2000;
await getGlobalInstance().flush(flushTimeout)
Testing
- Create a
.env
file with STATSD_API_KEY
and STATSD_HOST
values
- Run
yarn example
- Verify manually that console logs and metrics in the statsd server are valid
Contributing
Before creating an issue, please ensure that it hasn't already been reported/suggested, and double-check the documentation.
See the Contribution Guidelines if you'd like to submit a PR.
License
Licensed under the MIT License, Copyright © 2020-present Soluto.
Crafted by the Soluto Open Sourcerers🧙