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

autometric

v2.0.0

Published

A library to create automated metrics over various async pattern

Downloads

2

Readme

autometric

Build Status Coverage Status

The aim of this module is to collect metrics automatically with the prom-client on different asynchronous execution pattern. It is written in TypeScript and includes its type definitions, of course. It uses prom-client as peer dependency, so don't forget to add it to your module!

This module is in development. Currently supported are Promises, connect-like middlewares and a pass-through stream. The goal is to enhance the module with support for child-processes and observables patterns in future releases.

How to use

First you have to install this library from npm:

npm install --save autometric
# OR
yarn add autometric

Promises

TypeScript:

import { createAutometricPromise, IAutometricPromiseOptions, ICreateAutometricPromiseOptions } from "autometric";

const AutometricPromise = createAutometricPromise("my_metric_promise", {labels: {labels: "are-optional"}});
// function to change the labels after getting the result
const rewriteLabels = (
    currentLabels: {[name: string]: string},
    result: boolean | Error, // according to the generic type of the Promise or the Error from the reject
    createOptions: ICreateAutometricPromiseOptions,
    callOptions: IAutometricPromiseOptions,
) => {
    if (result === true) {
        return { ...currentLabels, status: "success" };
    }
    if (result instanceof Error && result.message === "Value is smaller the 1%") {
        return { ...currentLabels, status: "error" };
    }
    return currentLabels;
};
// options are optional
const options = { labels: { additional: "add-additional-labels", labels: "or-overwrite-them" }, rewriteLabels };

new AutometricPromise<boolean>((resolve, reject) => {
    setTimeout(() => {
        const random = Math.random() > 0.5;
        if (random < 0.01) {
            return reject(new Error("Value is smaller the 1%"));
        }
        resolve(Math.random() > 0.5);
    }, 100);
}, options);

JavaScript:

import { createAutometricPromise } from "autometric";

const AutometricPromise = createAutometricPromise("my_metric_promise", {labels: {labels: "are-optional"}});
// function to change the labels after getting the result
const rewriteLabels = (currentLabels, result, createOptions, callOptions) => {
    if (result === true) {
        return { ...currentLabels, status: "success" };
    }
    if (result instanceof Error && result.message === "Value is smaller the 1%") {
        return { ...currentLabels, status: "error" };
    }
    return currentLabels;
};
// options are optional
const options = { labels: { additional: "add-additional-labels", labels: "or-overwrite-them" }, rewriteLabels };

new AutometricPromise<boolean>((resolve, reject) => {
    setTimeout(() => {
        const random = Math.random() > 0.5;
        if (random < 0.01) {
            return reject(new Error("Value is smaller the 1%"));
        }
        resolve(Math.random() > 0.5);
    }, 100);
}, options);

The promise of autometric will collect the following metrics:

  • name + "_calls_total" as Counter: counts Promises that start its execution.
  • name + "_durations_ms" as Summary: summary over the duration from execution start until it resolves or rejects. It will add a status (success / fail) to the metric as long as not disabled via the noStatusLabel option.

With the pass-through stream

TypeScript:

import { createAutometricStreamPipe, IAutometricCreateOptions, IAutometricStreamOptions } from "autometric";
import { createReadStream, createWriteStream } from "fs";

const AutometricPipe = createAutometricStreamPipe("my_metric_stream", {labels: {labels: "are-optional"}});
// function to change the labels after getting a chunk
const rewriteLabels = (
    currentLabels: {[name: string]: string},
    chunk: Buffer,
    encoding: string,
    createOptions: IAutometricCreateOptions,
    callOptions: IAutometricStreamOptions,
) => {
    return {...currentLabels, encoding};
};
// options are optional
const options = { labels: { additional: "add-additional-labels", labels: "or-overwrite-them" }, rewriteLabels };

const metricsPipe = new AutometricPipe(options);

const reader = createReadStream("path/to/read");
const writer = createWriteStream("path/to/write");

reader.pipe(metricsPipe).pipe(writer);

JavaScript:

import { createAutometricStreamPipe } from "autometric";
import { createReadStream, createWriteStream } from "fs";

const AutometricPipe = createAutometricStreamPipe("my_metric_stream", {labels: {labels: "are-optional"}});
// function to change the labels after getting a chunk
const rewriteLabels = (currentLabels, chunk, encoding, createOptions, callOptions) => {
    return {...currentLabels, encoding};
};
// options are optional
const options = { labels: { additional: "add-additional-labels", labels: "or-overwrite-them" }, rewriteLabels };

const metricsPipe = new AutometricPipe(options);

const reader = createReadStream("path/to/read");
const writer = createWriteStream("path/to/write");

reader.pipe(metricsPipe).pipe(writer);

The pass-through pipe of autometric will collect the following metrics:

  • name + "_chunk_sizes_bytes" as Summary: summary over the size in bytes of a chunk (only for strings or Buffers)
  • name + "_durations_ms" as Summary: summary over the duration from the first chunk until the end event.
  • name + "_elapsed_time_ms" as Summary: Time between emits of chunks
  • name + "_ends_total" as Counter: counts streams that ends
  • name + "_non_emits_total" as Counter: counts streams that ends without emitting any data
  • name + "_throughput_bytes" as Summary: a summary over the processed bytes in one stream

Connect-like middlware

This middleware is build for express, but it should also work with other connect-like middlewares

TypeScript:

import { createAutometricMiddleware } from "autometric";
import * as express from "express";
const app = express();


const middlewareObject = createAutometricMiddleware("my_metric_middleware", {labels: {labels: "are-optional"}});
// function to change the labels after getting a chunk
const rewriteLabels = (currentLabels, chunk, encoding, createOptions, callOptions) => {
    return {...currentLabels, encoding};
};
// options are optional
const options = {
    labels: {
        additional: "add-additional-labels",
        labels: "or-overwrite-them",
        service: "user-auth",
    },
    rewriteLabels,
};

app.get("/api/user", middlewareObject.createMiddleware(options), (req, res) => {
    // ...
});

JavaScript:

const { createAutometricMiddleware } = require("autometric");
const express = require("express");
const app = express();


const middlewareObject = createAutometricMiddleware("my_metric_middleware", {labels: {labels: "are-optional"}});
// function to change the labels after getting a chunk
const rewriteLabels = (currentLabels, chunk, encoding, createOptions, callOptions) => {
    return {...currentLabels, encoding};
};
// options are optional
const options = {
    labels: {
        additional: "add-additional-labels",
        labels: "or-overwrite-them",
        service: "user-auth",
    },
    rewriteLabels,
};

app.get("/api/user", middlewareObject.createMiddleware(options), (req, res) => {
    // ...
});

Get the metrics

This module uses the prom-client as peer dependency. To get all metrics - including foreign ones - you can use the prom-client register like that:

import { createServer } from "http";
import { register } from "prom-client";

createServer((req, res) => {
    res.writeHead(200, register.contentType);
    res.end(register.metrics());
}).listen(9000);

You can also take the static property register of the resulting classes to output only the specific metrics, or merge them with other registers:

AutometricPromise.register.metrics();
// OR
AutometricStreamPassThrough.register.metrics();
// OR
middlewareObject.register.metrics();

Meta metrics from autometric

Autometric also collects metrics from the creation of Autometric metrics collectors. You can access them like this:

import { autometricRegister } from "autometric";
autometricRegister.metrics();

Scripts Tasks

Scripts registered in package.json:

  • transpile: Transpile TypeScript Code to JavaScript
  • dist: Make a ready for distribution version
  • lint: Use the linter for TypeScript Code
  • test: Run software- and coverage-tests in node.
  • doc: Build the API documentation.

Every command is also available as dockerized version, by prefixing docker: (ex.: docker:lint)

License

This library is released under the ISC License.

Links