Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

nstats

Package Overview
Dependencies
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nstats

A fast and compact way to get all your network and process stats for your node application.

  • 5.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
475
decreased by-45.53%
Maintainers
1
Weekly downloads
 
Created
Source

Nstats

A fast and compact way to get all your network and process stats for your node application. Websocket, http/s, express and prometheus compatible!

Installation

$ npm install nstats

Quick Start (Express)

// ws is a websocket server (ws.js) and httpServer is an http or https node server.
var nstats = require('nstats')(ws, httpServer);

//use it with express
app.use(nstats.express());

//display the stats!
console.log(nstats.data); // non-stringifyed
console.log(nstats.toJson()) // stringifyed
console.log(nstats.toPrometheus()) //  prometheus format

Quick Start (Fastify 4.x.x)

// ws is a websocket server (ws.js) and httpServer is an http or https node server.
var nstats = require('nstats')();

//use it with express
app.register(nstats.fastify(),{
  ignored_routes:['/metrics','/health']
});

app.get('/metrics', (req, res) => {
  res.code(200).send(nstats.toPrometheus());
});

app.get('/health', (req, res) => res.code(200).send('All Systems Nominal'));


//display the stats!
console.log(nstats.data); // non-stringifyed
console.log(nstats.toJson()) // stringifyed
console.log(nstats.toPrometheus()) //  prometheus format

Grafana Sample

Import the Grafana Dashboard example inside the Grafana folder to view the the stats in a graph like manner.

Example

const express = require('express');
const nstats = require('nstats');
const WebSocket = require('ws');
const app = express();
const server = require('http').createServer(app);
const port = 3042;
const wss = new WebSocket.Server({ server });

var stats = require('nstats')(wss, server);
stats.interval = 500;
stats.serverName = "TestServer";

wss.on('connection', function connection(ws) {
  ws.on('message', function incoming(message) {
      ws.send(message);
  });
});

app.use(stats.express());

app.get('/', (req, res) => res.send());
app.get('/metrics', (req, res) => res.send(stats.toPrometheus()));
app.get('/stats', (req, res) => {
  res.type('json')
  stats.calc(()=>res.send(stats.toJson()));
});

server.listen(port, () => console.log(`Example app listening on port ${port}!`));

##Properties

####stats.data The stats.data object is a JavaScript object containing all the stats.

{
    "uptime": 209.653,
    "totalMemory": "29.17",
    "activeSockets": 2,
    "responseOverhead": {
        "avg": 0.5810644392156861,
        "highest": 5.175994,
        "lowest": 0.260562,
        "total": 148.17143199999995
    },
    "avgWriteKBs": "3.26",
    "avgReadKBs": "0.28",
    "avgPacketsSecond": "1.22",
    "totalBytesWritten": 700381,
    "totalMBytesWritten": 0.67,
    "totalBytesRead": 60813,
    "totalMBytesRead": 0.06,
    "writeKBS": "0.00",
    "readKBS": "0.00",
    "packetsSecond": "0.00",
    "totalPackets": 255,
    "http": {
        "GET": {
            "200": 255
        }
    }
}

feel free to add your own stats you want to keep track off too!

stats.data['foo'] = "some dank stat";

####stats.interval A time in milliseconds on when the stats will refresh and calculate.

stats.interval = 5000; // default is 1 second

Set this to 0 if you do not want it to loop.

##Methods stats.addWeb(req,res,sTime)

A method to add network usage for http requests not done with express or w.s. res is optional if you want stats.data.http data. sTime (The start time of when you received the req BigInt) is optional if you want responseOverhead data.

var req = http.get("http://google.com", function(res) {
    res.on('end', function() {
      stats.addWeb(req, res);
    });
});

Since its not express or ws, it does not know about it.


stats.calc()

A method to allow you to manually trigger when the stats are computed.

optional call back can be passed to let you know its done.

stats.calc(function(){
  //its done!
  console.log(stats.data);
});

Keywords

FAQs

Package last updated on 27 Jul 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc