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

@osskit/monitor

v8.0.2

Published

<p align="center"> <img width="250" height="250" src="https://user-images.githubusercontent.com/15312980/174908438-b6f5eaea-7b81-4008-9cad-8a7c2a45bbaf.svg"> </p>

Downloads

25,994

Readme

GitHub Workflow Status License code style: prettier

Utilize a declarative API to wrap your functions to capture Prometheus metrics & logs for each function

Install

yarn add @osskit/monitor

Usage

Scoped

import { createMonitor } from '@osskit/monitor'

export const monitor = createMonitor({ scope: 'metrics' });

const result1 = await monitor('query', async () => db.query());
const result2 = await monitor('update', async () => db.update());

// Custom labeling
export const monitor = createMonitor<['my_label']>({ scope: 'metrics' });

const customLabels = (id: string) => await monitor('query', async () => db.query(id), {context: {key: 'myId' }, labeling: { 'my_label': 'label' } });

Unscoped

import monitor from '@osskit/monitor'

const result = await monitor('query', async () => db.query());

// Custom labeling 
const customLabels = (id: string) => await monitor('query', async () => db.query(id), {context: {key: 'myId' }, labeling: { 'my_label': 'label' } });

With monitor options

import { createMonitor } from '@osskit/monitor'

export const monitor = createMonitor({ scope: 'metrics' });

// Context
const result = (id: string) => await monitor('query', async () => db.query(id), { context: { id } });

// Parse & Log Results
const logResults = (id: string) => await monitor('query', async () => db.query(id), { logResult: true, parseResult: (res) => res.prop });

// Parse Error
const errored = (id: string) => await monitor('query', async () => db.query(id), { logResult: true, parseError: (e) => e.statusCode });

// Log Execution Start
const executionStart = (id: string) => await monitor('query', async () => db.query(id), { logExecutionStart: true });

With global options

import { setGlobalOptions, setGlobalContext } from '@osskit/monitor';
import  logger from './logger.js';

setGlobalOptions({
  context: { globalContextId: 'bla' },
  logResult: true,
  logExecutionStart: false,
  parseError: (res) => console.log(res),
  prometheusBuckets: [0.0001, 0.1, 0.5, 10],
  logger,
  errorLogLevel: 'fatal'
});

setGlobalContext(() => getDynamicContext());

API

createMonitor({ scope: string, options?: MonitorOptions })

scope

Type: string

The scope of the monitor's metrics

Will be used as the Prometheus metric name

Returns an instance of a function that calls monitor - <T>(method: string, callable: () => T, options?: MonitorOptions<T>)

monitor(method: string, callable: () => T, options?: MonitorOptions)

method

Type: string

Will be used for the method label of the metric, or the metric name if no parent scope was declared

setGlobalOptions({ options: MonitorGlobalOptions })

Set a number of options that will be used globally for all monitor invocations

setGlobalContext(value: () => Record<string, string>)

Invoke a function that returns a global context to use in all monitor invocation logs

Parameters

MonitorOptions

| Parameter | Description | |:-----------------------------------:|-----------------------------------------------------------------------------| | context?: boolean | add context that will be logged in all method's logs | | logResult?: boolean | log the method's result | | logExecutionStart?: boolean | log the start of the method's execution method.start | | parseResult?: (e: any) => any | transform the method's result that will be returned | | parseError?: (e: any) => any | if the method errored, transform the error that will be thrown | | errorLogLevel?: pino.Level | if the method errored, which level should the message be, default - error | | labeling?: Record<string, string> | add custom labeled counters using keys and values |

GlobalOptions

| Parameter | Description | |:------------------------------:|-----------------------------------------------------------------------------| | logResult?: boolean | log the monitored methods results | | logExecutionStart?: boolean | log the start of the method's execution method.start | | parseError?: (e: any) => any | if the method errored, transform the error that will be thrown | | prometheusBuckets?: number[] | use the following prometheus bucket list for monitor metrics across methods | | logger?: BaseLogger | supply a pino BaseLogger for monitor to use in logging results | | errorLogLevel?: pino.Level | if the method errored, which level should the message be, default - error |

License

MIT License