@fresh8/prom-client-timer-unit
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

prom-client-timer-unit

CircleCI Coverage Status styled with prettier

A helper function to help record time in other units than seconds. It really just divides the time measurment (in seconds) by whatever number you give it.

Usage

At a glance

Javascript

const myMetric = require('prom-client').Histogram()
const startTimer = require('@fresh8/prom-client-timer-unit')

const stopTimer = startTimer(myMetric, 1e-3)
// [...] your code doing things
stopTimer() // will calculate the time elapsed and register it in `myMetric`

Typescript

import * as promClient from 'prom-client'
import * as startTimer from '@fresh8/prom-client-timer-unit'

const myMetric = promClient.Histogram()
const stopTimer = startTimer(myMetric, 1e-3)
// [...] your code doing things
stopTimer() // will calculate the time elapsed and register it in `myMetric`

API

startTimer(metric, factor, labels)

  • metric : The prom-client metric, e.g: new promClient.Histogram(opts)
  • factor (optional): The factor to be applied to the observed number of seconds. Examples: 1e-3 for milliseconds, 60 for minutes. Defaults to 1
  • labels (optional): Labels to apply to the observation. This is to follow the same signature as the original startTimer function in prom-client. Defaults to {}
  • returns: stopTimer

Starts counting time using process.hrtime.

stopTimer(labels)

  • labels : Labels to apply to the observation. They will be merged with the labels given to startTimer (if any). Defaults to {}
  • returns: { labels, elapsed }. This is in case you want to re-use those values in something else than prometheus itself. ** labels: the result of combining labels given to startTimer and stopTimer ** elapsed: the time measured by the timer, in the correct unit.

Stops the timer and record the elapsed time in seconds divided by factor.

Credits

Implementation very heavily inspired from the original in prom-client

Readme

Keywords

none

Package Sidebar

Install

npm i @fresh8/prom-client-timer-unit

Weekly Downloads

4

Version

1.0.1

License

MIT

Last publish

Collaborators

  • t-leung-sportradar
  • jbellsr
  • toby_c_sr
  • domdo.es
  • sraugier
  • markdavies618
  • joestefan
  • maxweston
  • f8
  • samstefan
  • mattdaviesf8
  • gabriellfsouza
  • phl3bas