A collection of helpers for prom-client
Switch branches/tags
Nothing to show
Clone or download
Latest commit e0c0512 Feb 6, 2018

README.md

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