1.0.3 • Published 1 year ago
@foxsense-innovations-internal/vigil-sdk-js v1.0.3
Vigil - SDK
SDK for API monitoring, Job monitoring and Error monitoring.
Installation
npm
$ npm i @foxsense-innovations-internal/vigil-sdk-js
yarn
$ yarn add @foxsense-innovations-internal/vigil-sdk-js
Usage
# import Vigil to initialize the API KEY.
const { Vigil } = require('@foxsense-innovations-internal/vigil-sdk-js');
Vigil.initialize(<API_KEY>);
API Monitoring
# import ApiMonitoring to monitor the APIs in the application.
const { ApiMonitoring } = require('@foxsense-innovations-internal/vigil-sdk-js');
# instantiate a new object of ApiMonitoring.
const apiMonitor = new ApiMonitoring();
# apply as middleware to monitor all the APIs in the application.
app.use(apiMonitor.apiMonitoringMiddleware());
# import ApiMonitoring to monitor the APIs in the application.
const { ApiMonitoring } = require('@foxsense-innovations-internal/vigil-sdk-js');
# instantiate a new object of ApiMonitoring.
const apiMonitor = new ApiMonitoring();
# add middleware to specific APIs in the application.
app.get('/',apiMonitor.apiMonitoringMiddleware(),(req,res) => {
...
...
});
app.post('/',apiMonitor.apiMonitoringMiddleware(),(req,res) => {
...
...
});
# This particular API will not be monitored since the middleware is not applied to this API.
app.delete('/',(req,res) => {
...
...
});
Options
Options are not mandatory. | Options | Mandatory | Default | Values | | ------- | --------- | ------- | --------------------------------------------- | | exclude | FALSE | {} | { GET: [], POST: [], PATCH: [], DELETE: [], } |
Exclude
- Excludes the given API path's for each method. These API are not monitored.
- You can use express way of applying/ not applying global middleware to specific routes.
# import ApiMonitoring to monitor the APIs in the application.
const { ApiMonitoring } = require('@foxsense-innovations-internal/vigil-sdk-js');
# instantiate a new object of ApiMonitoring.
const apiMonitor = new ApiMonitoring();
const options = {
exclude: {
GET: ['/','/health']
}
};
# '/' and '/health' APIs of GET method are not monitored.
app.use(apiMonitor.apiMonitoringMiddleware());
Job Monitoring
# import JobMonitoring to monitor the Jobs in the application.
const { JobMonitoring } = require('@foxsense-innovations-internal/vigil-sdk-js');
# instantiate a new object of JobMonitoring.
const jobMonitor = new JobMonitoring();
cron.schecule('<cron-expression>',() => {
try {
// log that the job triggered with a start message.
jobMonitor.logJobStart({
jobId: <job-id>,
startMessage: <job-start-message>
});
...
...
// log that the job has completed its execution with a success message.
jobMonitor.logJobEnd({
jobId: <job-id>,
stopMessage: <job-end-message>
});
} catch(error) {
// if the job fails at some point,
// then its failure can be logged with a failure message.
jobMonitor.logJobFailed({
jobId: <job-id>,
stopMessage: <job-failure-message>
});
}
});
Error Monitoring
# import ErrorMonitoring to capture any exceptions/errors that occur in the application.
const { ErrorMonitoring } = require('@foxsense-innovations-internal/vigil-sdk-js');
# instantiate a new object of ErrorMonitoring.
const errorMonitor = new ErrorMonitoring();
function() {
try {
...
...
} catch(error) {
// capture the exception from the error object.
errorMonitor.captureException(error);
}
}
Options
Options are not mandatory. | Options | Mandatory | Default | Value | | ------- | --------- | ------- | -----------------| | tags | FALSE | [] | Array of strings | | context | FALSE | {} | An object |
Tags and Context
- Gives extra information about the exception that occurred.
# import ErrorMonitoring to capture exceptions/errors.
const { ErrorMonitoring } = require('@foxsense-innovations-internal/vigil-sdk-js');
# instantiate a new object of ErrorMonitoring.
const errorMonitor = new ErrorMonitoring();
function() {
try {
...
...
} catch(error) {
const options = {
tags: ['tag1','tag2'],
context: {
<key1>: <value1>,
<key2>: <value2>
}
};
// capture the exception from the error object
// and pass extra information about the exception in options.
errorMonitor.captureException(error, options);
}
}