Skip to content

Megapixel99/stats-tracker

Repository files navigation

Stats Tracker

This module exposes two functions: dashboard and tracker

The tracker function can be used to relay information about the current NodeJS process to the dashboard (wherever the dashboard function is running).

tracker(config)

The tracker takes one argument, config. The tracker connects to the dashboard via web sockets and exposes a web socket server which the dashboard can connect to.

The config can be used to pass the port of the web socket server and the name of the application.

const { tracker } = require('stats-tracker');

let t = tracker({
  server: server // configure the tracker to extend from your http/https server
  port: 3001, // only needed if you do not pass a server
  name: 'test',
  pod: 'test-1', // useful if running multiple instances of the same application, defaults to the name, in this case `test`
  logger: console, // defaults to console if nothing is passed
});

The functions that tracker exposes are:

bytes.sent(data):

The data argument the function takes needs to be a number and is assumed to be the number of bytes sent.

bytes.received(data):

The data argument the function takes needs to be a number and is assumed to be the number of bytes received.

data.sent(data):

The data argument the function takes is assumed to be raw data and thus will be serialized and converted to bytes.

data.received(data):

The data argument the function takes is assumed to be raw data and thus will be serialized and converted to bytes.

database.read(data):

The data argument the function takes is assumed to be a JSON array and the length of the JSON array will be sent to the dashboard.

database.written(data):

The data argument the function takes is assumed to be a JSON array and the length of the JSON array will be sent to the dashboard.

databaseRows.sent(data):

The data argument the function takes needs to be a number and is assumed to be the number of rows sent.

databaseRows.received(data):

The data argument the function takes needs to be a number and is assumed to be the number of rows received.

job.start(jobName [, start]):

This function will start a job, and returns a job so you can stop it later, it is up to you to end the job manually with job.stop(). The jobName argument is the name of the job that you will see in the dashboard. The optional start argument is the time when the job started in milliseconds (defaults to Date.now())

job.stop():

This function will stop a job and data about the job will be sent to the dashboard.

How to use job.start() and job.stop()

const { tracker } = require('stats-tracker');

let t = tracker({
  port: 3001,
  name: 'test',
  pod: 'test-1',
});

let job1 = t.job.start('jobName');

// some other code which takes a while to run goes here

job1.stop();

dashboard

The dashboard function exposes a web-based dashboard (built using express and ejs) to see information about the various trackers that are feeding information to it (via web sockets).

The dashboard function takes one argument, config.

The config and be used to pass the port you want the dashboard to run on, the URL of the Mongo Database you will be using, and the url(s) or the various applications running the tracker function along with usageLength and a logger.

const { dashboard } = require('stats-tracker');

dashboard({
  port: 3000,
  mongoUrl: 'mongodb://yourUrl',
  urls: ['ws://firstTracker', 'ws://secondTracker'],
  usageLength: 100, // determines how much CPU usage info to save in the database (for the average CPU usage to be determined), defaults to 100 and is updated every 5 seconds
  logger: console, // defaults to console if nothing is passed
});

If you want to spin up the dashboard as a standalone application you can run: npm run dashboard

For a full (running/working) demo, please see demo.js in the demo folder

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published