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

exframe-metrics

v1.4.1

Published

Utility functions for instrumenting metrics.

Downloads

1,592

Readme

exframe-metrics

Utility functions for instrumenting metrics.

Install

npm install exframe-metrics

Usage

instrument

instrument function takes a function as input, adds duration histogram and count metrics to it, executes it, and returns a Promise that is fulfilled with result of the function.

const myFunction = () => 'banana';

const result = await instrument(myFunction);

console.log(result);
// > banana

console.log(await prometheusClient.register.metrics());
// # HELP function_myFunction_duration_seconds Length of time, in seconds, the function took to complete
// # TYPE function_myFunction_duration_seconds histogram
// function_myFunction_duration_seconds_bucket{le="0.005"} 1
// function_myFunction_duration_seconds_bucket{le="0.01"} 1
// function_myFunction_duration_seconds_bucket{le="0.025"} 1
// function_myFunction_duration_seconds_bucket{le="0.05"} 1
// function_myFunction_duration_seconds_bucket{le="0.1"} 1
// function_myFunction_duration_seconds_bucket{le="0.25"} 1
// function_myFunction_duration_seconds_bucket{le="0.5"} 1
// function_myFunction_duration_seconds_bucket{le="1"} 1
// function_myFunction_duration_seconds_bucket{le="2.5"} 1
// function_myFunction_duration_seconds_bucket{le="5"} 1
// function_myFunction_duration_seconds_bucket{le="10"} 1
// function_myFunction_duration_seconds_bucket{le="+Inf"} 1
// function_myFunction_duration_seconds_sum 0.000182651
// function_myFunction_duration_seconds_count 1

// # HELP function_myFunction_calls_total Total number of times the function was called
// # TYPE function_myFunction_calls_total counter
// function_myFunction_calls_total 1

lazyInstrument

lazyInstrument function has a similar outcome as instrument except that it does not execute the input function directly and instead returns a different async function, instrumented with metrics, that can be called later. Useful for generating middleware functions.

const myFunction = () => 'banana';

const myDeferredFunction = await lazyInstrument(myFunction);

const result = await myDeferredFunction();

console.log(result);
// > banana

API

instrument(fn, options?)

Returns a Promise that is either fulfilled with the input function's result, or rejected with an error if the function throws.

fn

Type: function

Function to be instrumented. Default metrics added are:

  • duration: Histogram metric that measures the total time the function took to execute. The default name of this metric is "function_<function_name>_duration_seconds"
  • count: Counter metric that measures the total number of calls to the function with the same name (or metricName if provided). The default name of this metrics is: "function_<function_name>_calls_total"

options

Type: object

metricName

Type: String

Overrides the fn name to use for the base part of the metric names. Note: This property is required if an anonymous function is provided

metricPrefix

Type: String

String to prepend to an auto-generated metric name. Ignored if metricName is provided