This topic includes instructions on how to send custom metrics to Logz.io from your Node.js application.
The included example uses the OpenTelemetry JS SDK and its based on OpenTelemetry exporter collector proto.
Before you begin, you'll need:
Node 8 or higher
Quick start
Set the variables in the following code snippet:
Environment variable | Description |
---|
url | The Logz.io Listener URL for for your region, configured to use port 8052 for http traffic, or port 8053 for https traffic. |
token | Your Logz.io Prometheus Metrics account token. |
const { MeterProvider } = require('@opentelemetry/metrics');
const { RemoteWriteMetricExporter } = require('yotam-js-metrics-test');
const collectorOptions = {
url: '<<url>>',
token: '<<token>>',
};
const metricExporter = new RemoteWriteMetricExporter(collectorOptions);
const meter = new MeterProvider({
exporter: metricExporter,
interval: 15000,
}).getMeter('example-exporter');
const requestCounter = meter.createCounter('Counter', {
description: 'Example of a Counter',
});
const labels = { environment: 'prod' };
requestCounter.bind(labels).add(1);
Types of metric instruments
For more information, see the OpenTelemetry documentation.
Name | Behavior |
---|
Counter | Metric value can only go up or be reset to 0, calculated per counter.Add(context,value,labels) request. |
UpDownCounter | Metric value can arbitrarily increment or decrement, calculated per updowncounter.Add(context,value,labels) request. |
ValueRecorder | Metric values captured by the valuerecorder.Record(context,value,labels) function, calculated per request. |
SumObserver | Metric value can only go up or be reset to 0, calculated per push interval. |
UpDownSumObserver | Metric value can arbitrarily increment or decrement, calculated per push interval. |
ValueObserver | Metric values captured by the callback function, calculated per push interval. |
More examples
First Initialize the exporter and meter provider:
const { MeterProvider } = require('@opentelemetry/metrics');
const { RemoteWriteMetricExporter } = require('yotam-js-metrics-test');
const collectorOptions = {
url: '<<url>>',
token: '<<token>>',
};
const metricExporter = new RemoteWriteMetricExporter(collectorOptions);
const meter = new MeterProvider({
exporter: metricExporter,
interval: 15000,
}).getMeter('example-exporter');
Then create different types of metrics
UpDownCounter:
const upDownCounter = meter.createUpDownCounter('UpDownCounter', {
description: 'Example of a UpDownCounter',
});
const labels = { environment: 'prod' };
upDownCounter.bind(labels);
upDownCounter.add(5);
upDownCounter.add(-1);
ValueRecorder:
const recorder = meter.createValueRecorder('test_value_recorder', {
description: 'Example of a ValueRecorder',
});
const labels = { environment: 'prod' };
recorder.bind(labels);
recorder.record(30);
recorder.record(20);
SumObserver:
meter.createSumObserver('SumObserver', {
description: 'Example of a sync sum observer with callback',
}, (observerResult) => {
const value = getMetrics();
const labels = { environment: 'prod' };
observerResult.observe(value, labels);
});
UpDownSumObserver:
function getAsyncValue() {
return new Promise((resolve) => {
setTimeout(()=> {
resolve(Math.random());
}, 100);
});
}
meter.createUpDownSumObserver('UpDownSumObserver', {
description: 'Example of an async observer with callback',
}, async (observerResult) => {
const value = await getAsyncValue();
const labels = { environment: 'prod' };
observerResult.observe(value, labels);
});
ValueObserver:
function getSomeAsyncMetrics() {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve({
value1: Math.random(),
value2: Math.random(),
});
}, 100)
});
}
const valueObserver = meter.createValueObserver('value_observer', {
description: 'Example of a value observer metric',
});
const anotherValueObserver = meter.createValueObserver('another_value_observer', {
description: 'Example of a value observer metric',
});
meter.createBatchObserver((observerBatchResult) => {
getSomeAsyncMetrics().then(metrics => {
const labels = { environment: 'prod' };
observerBatchResult.observe(labels, [
valueObserver.observation(metrics.value1),
anotherValueObserver.observation(metrics.value2),
]);
});
});