Giter Site home page Giter Site logo

opencensus-node-default-metrics's Introduction

Default metrics for node.js using opencensus

Node version Downloads Count Build Status Known Vulnerabilities Coverage Status License

Collect default nodejs metrics using opencensus framework. This module is inspired and based on the prom-client and node-prometheus-gc-stats package.

Install

Install with npm:

npm i opencensus-default-metrics --save

Install with yarn:

yarn add opencensus-default-metrics

Usage

See example folder for a sample usage. The library does not bundle any web framework. To expose the metrics you have to use the opencensus exporters.

Default metrics

There are some default metrics recommended by Prometheus itself. To collect these, call collectDefaultMetrics

NOTE: Some of the metrics, concerning File Descriptors and Memory, are only available on Linux.

In addition, some Node-specific metrics are included, such as event loop lag, active handles and Node.js version. See what metrics there are in lib/metrics.

collectDefaultMetrics takes 1 options object with 3 entries, a timeout for how often the probe should be fired, an optional prefix for metric names and an opencensus stats object to which metrics should be registered. By default probes are launched every 10 seconds, but this can be modified like this:

const client = require('opencensus-default-metrics');

const collectDefaultMetrics = client.collectDefaultMetrics;

// Probe every 5th second.
collectDefaultMetrics({ timeout: 5000 });

To register metrics to another stats instance, pass it in as stats:

const { globalStats } = require('@opencensus/core');
const client = require('opencensus-default-metrics');

const collectDefaultMetrics = client.collectDefaultMetrics;

collectDefaultMetrics({ stats: globalStats });

To prefix metric names with your own arbitrary string, pass in a prefix:

const client = require('opencensus-default-metrics');

const collectDefaultMetrics = client.collectDefaultMetrics;

// Probe every 5th second.
collectDefaultMetrics({ prefix: 'my_application_' });

You can get the full list of metrics by inspecting client.metricsList.

collectDefaultMetrics returns an identification when invoked, which is a reference to the Timer used to keep the probes going. This can be passed to clearInterval in order to stop all probes.

NOTE: Existing intervals are automatically cleared when calling collectDefaultMetrics.

const client = require('opencensus-default-metrics');

const collectDefaultMetrics = client.collectDefaultMetrics;

const interval = collectDefaultMetrics();

// ... some time later

clearInterval(interval);

NOTE: unref is called on the interval internally, so it will not keep your node process going indefinitely if it's the only thing keeping it from shutting down.

Stop polling default metrics

To stop collecting the default metrics, you have to call the function and pass it to clearInterval.

const client = require('opencensus-default-metrics');

clearInterval(client.collectDefaultMetrics());

// Clear the stats
client.stats.clear();

opencensus-node-default-metrics's People

Contributors

dependabot[bot] avatar rhaymo avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.