vmarchaud-ot-metrics
v0.8.3
Published
OpenTelemetry metrics SDK
Downloads
4
Maintainers
Readme
OpenTelemetry Metrics SDK
OpenTelemetry metrics allow a user to collect data and export it to a metrics backend like Prometheus.
Installation
npm install --save @opentelemetry/metrics
Usage
Counter
Choose this kind of metric when the value is a quantity, the sum is of primary interest, and the event count and value distribution are not of primary interest. Counters are defined as Monotonic = true
by default, meaning that positive values are expected.
const { MeterProvider } = require('@opentelemetry/metrics');
// Initialize the Meter to capture measurements in various ways.
const meter = new MeterProvider().getMeter('your-meter-name');
const counter = meter.createCounter('metric_name', {
labelKeys: ['pid'],
description: 'Example of a counter'
});
const labels = { pid: process.pid };
// Create a BoundInstrument associated with specified label values.
const boundCounter = counter.bind(labels);
boundCounter.add(10);
Observable
Choose this kind of metric when only last value is important without worry about aggregation
const { MeterProvider, MetricObservable } = require('@opentelemetry/metrics');
// Initialize the Meter to capture measurements in various ways.
const meter = new MeterProvider().getMeter('your-meter-name');
const observer = meter.createObserver('metric_name', {
labelKeys: ['pid', 'core'],
description: 'Example of a observer'
});
function getCpuUsage() {
return Math.random();
}
const metricObservable = new MetricObservable();
observer.setCallback((observerResult) => {
// synchronous callback
observerResult.observe(getCpuUsage, { pid: process.pid, core: '1' });
// asynchronous callback
observerResult.observe(metricObservable, { pid: process.pid, core: '2' });
});
// simulate asynchronous operation
setInterval(()=> {
metricObservable.next(getCpuUsage());
}, 2000)
See examples/prometheus for a short example.
Measure
Work in progress
Useful links
- For more information on OpenTelemetry, visit: https://opentelemetry.io/
- For more about OpenTelemetry JavaScript: https://github.com/open-telemetry/opentelemetry-js
- For help or feedback on this project, join us on gitter
License
Apache 2.0 - See LICENSE for more information.