What is @opentelemetry/sdk-metrics-base?
@opentelemetry/sdk-metrics-base is a package that provides the core functionalities for collecting and reporting metrics in applications using OpenTelemetry. It allows developers to create and manage various types of metrics, such as counters, gauges, and histograms, and export them to different backends for monitoring and analysis.
What are @opentelemetry/sdk-metrics-base's main functionalities?
Creating a Meter
This code demonstrates how to create a MeterProvider and obtain a Meter instance. A Meter is used to create and manage metrics.
const { MeterProvider } = require('@opentelemetry/sdk-metrics-base');
const meterProvider = new MeterProvider();
const meter = meterProvider.getMeter('example-meter');
Creating a Counter
This code shows how to create a Counter metric and increment its value. Counters are used to count occurrences of events.
const counter = meter.createCounter('example_counter', {
description: 'An example counter'
});
counter.add(10);
Creating a Histogram
This code demonstrates how to create a Histogram metric and record a value. Histograms are used to measure the distribution of values.
const histogram = meter.createHistogram('example_histogram', {
description: 'An example histogram'
});
histogram.record(100);
Creating an UpDownCounter
This code shows how to create an UpDownCounter metric and adjust its value up or down. UpDownCounters are used to track values that can increase or decrease.
const upDownCounter = meter.createUpDownCounter('example_updowncounter', {
description: 'An example updown counter'
});
upDownCounter.add(5);
upDownCounter.add(-3);
Exporting Metrics
This code demonstrates how to export metrics using a ConsoleMetricExporter. Metrics can be exported to various backends for monitoring and analysis.
const { ConsoleMetricExporter, PeriodicExportingMetricReader } = require('@opentelemetry/sdk-metrics-base');
const exporter = new ConsoleMetricExporter();
meterProvider.addMetricReader(new PeriodicExportingMetricReader({ exporter }));
Other packages similar to @opentelemetry/sdk-metrics-base
prom-client
prom-client is a Prometheus client for Node.js that allows you to create and manage metrics, such as counters, gauges, histograms, and summaries. It is similar to @opentelemetry/sdk-metrics-base in that it provides a way to collect and export metrics, but it is specifically designed for use with Prometheus.
statsd
statsd is a simple, lightweight daemon that listens for statistics, like counters and timers, sent over UDP or TCP and sends aggregates to one or more pluggable backend services (e.g., Graphite). It is similar to @opentelemetry/sdk-metrics-base in that it collects and reports metrics, but it is more focused on network-based metric collection.
newrelic
newrelic is a Node.js agent for New Relic, which provides performance monitoring and management for applications. It is similar to @opentelemetry/sdk-metrics-base in that it collects and reports metrics, but it is specifically designed to work with the New Relic platform.
OpenTelemetry Metrics SDK
OpenTelemetry metrics allow a user to collect data and export it to a metrics backend like Prometheus.
Work In Progress
The OpenTelemetry SDK in this directory is undergoing drastic changes. If you need to use metrics, we recommend you use version 0.27.0
.
Installation
npm install --save "@opentelemetry/sdk-metrics-base@~0.27.0"
Usage
Please see the version 0.27.0
README.
TODO: Add usage information for updated SDK
Installation of the Latest experimental version
npm install --save @opentelemetry/sdk-metrics-base
Usage of the Latest experimental version
The basic setup of the SDK can be seen as followings:
const opentelemetry = require('@opentelemetry/api-metrics');
const { MeterProvider } = require('@opentelemetry/sdk-metrics-base');
opentelemetry.setGlobalMeterProvider(new MeterProvider());
const counter = opentelemetry.getMeter('default').createCounter('foo');
counter.add(1, { attributeKey: 'attribute-value' });
In conditions, we may need to setup an async instrument to observe costly events:
const observableCounter = opentelemetry.getMeter('default')
.createObservableCounter('observable-counter');
observableCounter.addCallback(async (observableResult) => {
observableResult.observe(1, { attributeKey: 'attribute-value' });
});
opentelemetry.getMeter('default')
.addBatchObservableCallback(batchObservableCallback, [ observableCounter ]);
async function batchObservableCallback(batchObservableResult) {
batchObservableResult.observe(observableCounter, 1, { attributeKey: 'attribute-value' });
batchObservableResult.observe(otherObservable, 2);
}
Useful links
License
Apache 2.0 - See LICENSE for more information.