npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@svedbg/nstats

v5.0.0

Published

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

Downloads

2

Readme

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 3.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);
});