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

monitorado

v1.6.0

Published

Monitorado, metrics for Node.js apps.

Downloads

14

Readme

Monitorado

Version Build Status Coverage Status

A simple but effective library to track app metrics.

Basic usage

const monitorado = require("monitorado")

try {
    // Start a metric to calculate how long the DB cleanup takes...
    let mt = monitorado.start("db-cleanup")

    // Cleanup database...
    await myApp.cleanupDatabase()

    // End db-cleanup metric.
    mt.end()

} catch (ex) {
    // End metric with error.
    mt.end(ex)
}

// Get metrics output.
let output = monitorado.output()

// Last 1 minute metrics for db-cleanup...
console.dir(output["db-cleanup"].last_1min)

More features

for (let i = 0; i < 100; i++) {

    // Auto expire counter after 5 seconds.
    let expiringMt = monitorado.start("timeout-method", {expiresIn: 5000})
    await myApp.externalCall()

    // If externalCall takes more than 5 sec, counter will auto-expire
    // so calling .end() won't do anything.
    expiringMt.end()
}

// Create metric with a tag.
let anotherMt = monitorado.start("user-method", {tag: "Users"})
let result = await myApp.getUserStats()

// Add extra data to metric, for instance a user count.
anotherMt.setData("users", result.users)
anotherMt.end()

// Output metrics for timeout-method only, for the last 1, 2, and 5 minutes.
const outputOptions = {intervals: [1, 2, 5], keys:  ["timeout-method"]}
let timeoutOutput = monitorado.output(outputOptions)

Built-in HTTP server

try {
    // Set a custom port and base path.
    monitorado.settings.httpServer.port = 8080
    monitorado.settings.httpServer.path = "/metrics"

    // Or you can also load and manipulate settings directly via SetMeUp...
    const setmeup = require("setmeup")
    setmeup.load("settings.monitorado.json")

    // Start the built-in HTTP server
    monitorado.httpServer.start()
    // Calling http://your-host:8080/metrics will output the metrics.

    // You can also protect the endpoint with a token.
    monitorado.settings.httpServer.token = "my-custom-token"
    // Now you need to pass an Authorization: Bearer my-customer-token header.
}

Persisting metrics

try {
    // Save current finished metrics to the monitorado.json file.
    monitorado.metrics.saveTo()

    // You can also specify a different file path.
    monitorado.metrics.saveTo("/usr/data/monitorado.json")

    // Or simply get the exported JSON as as object.
    let data = monitorado.metrics.toJSON()
    // Send data above to an external storage, S3, etc...

    // Now load metrics from an exported file.
    monitorado.metrics.loadFrom("/usr/data/monitorado.json")

    // Or load from an exported object directly. For example, download JSON data from S3.
    let s3data = myS3.downloadFile("monitorado.json")
    monitorado.metrics.loadFrom(s3data)

    // You can also avoid loading metrics that already exists in memory by using avoidDuplicates = true.
    monitorado.metrics.loadFrom(s3data, true)
}

Default settings

In a nutshell, the default settings contains all the defaults used by Monitorado. You can override settings by creating a settings.json on the root of your app, or programatically by changing values directly on the monitorado.settings object.

Monitorado uses SetMeUp to manage its settings, so you might want to take a look there for some insights and how to define your custom settings.

Sample output

Below you'll find a sample output generated by Monitorado for a sample app, with comments added for reference:

{
    // Some system metrics taken from jaul.system.getInfo()
    "system": {
        "loadAvg": 1,
        "memoryUsage": 61
    },
    // Metrics for "ad.fetch"
    "ad.fetch": {
        "total_calls": 930, // total number of calls currently recorded
        // Following counters are for the last 1 minute...
        "last_1min": {
            "calls": 17, // 17 calls
            "errors": 0, // 0 calls with errors
            "expired": 1, // 1 expired calls
            "min": 305, // minimum elapsed time 305ms
            "max": 399, // maximum elapsed time 399ms
            "avg": 373, // average elapsed time of 373ms
            "p99": 399, // 99 percentile 399ms
            "p90": 385 // 90 percentile 385ms
        },
        // The following counters are for the last 20 minutes...
        "last_20min": {
            "calls": 840,
            "errors": 2,
            "expired": 1,
            "min": 305,
            "max": 970,
            "avg": 391,
            "p99": 399,
            "p90": 390,
            // Extra data appended to counters on last 20 minutes,
            // for example mt.setData("users", 35).
            "data": {
                "users": {
                    "min": 35,
                    "max": 35,
                    "total": 35
                }
            }
        }
    },
    // Same as above, for for "google.fetch"...
    "google.fetch": {
        "total_calls": 1410,
        "last_1min": {
            "calls": 19,
            "errors": 0,
            "expired": 0,
            "min": 2100,
            "max": 3755,
            "avg": 2900,
            "p99": 3755,
            "p90": 2920
        },
        "last_20min": {
            "calls": 1403,
            "errors": 0,
            "expired": 0,
            "min": 2008,
            "max": 3940,
            "avg": 2974,
            "p99": 3001,
            "p90": 2976
        }
    }
}

On the sample above, the first system key is shown because the systemMetrics setting has key = "system and fields "loadAvg" and "memoryUsage". For the list of available fields, please check the SystemMetrics interface from JAUL.

Each of the next keys "ad.fetch" and "google.fetch" represent a specific metric. The settings intervals for the above sample is set to "[1, 20]". The setting percentiles is "[99, 90]".

Please note that the total_calls for a particular metric can be higher than the calls for the highest interval shown on the output. The total_calls simply tells us how many counters are currently stored for that metric, and is directly dependant on the expireAfter setting.

API documentation

You can browse the full API documentation at https://monitorado.devv.com.