Logz.io nodejs metrics sdk
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
Note This project works best with logzio as metrics backend, but its compatible with all backends that support prometheuesrmotewrite
format
Quick start
Install the package:
npm install logzio-nodejs-metrics-sdk@0.3.0
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. For example - https://listener.logz.io:8053 |
token | Your Logz.io Prometheus Metrics account token. |
const MeterProvider = require('@opentelemetry/sdk-metrics-base');
const sdk = require('logzio-nodejs-metrics-sdk');
const collectorOptions = {
url: '<<url>>',
headers: {
Authorization: 'Bearer <<token>>',
},
};
const metricExporter = new sdk.RemoteWriteExporter(collectorOptions);
const meter = new MeterProvider.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. |
Histogram | Metric values captured by the histogram.Record(context,value,labels) function, calculated per request. |
More examples
First Initialize the exporter and meter provider:
const MeterProvider = require('@opentelemetry/sdk-metrics-base');
const sdk = require('logzio-nodejs-metrics-sdk');
const collectorOptions = {
url: '<<url>>',
headers: {
Authorization: 'Bearer <<token>>',
},
};
const metricExporter = new sdk.RemoteWriteExporter(collectorOptions);
const meter = new MeterProvider.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);
Histogram:
const histogram = meter.createHistogram('test_histogram', {
description: 'Example of a histogram',
});
const labels = { environment: 'prod' };
histogram.bind(labels);
histogram.record(30);
histogram.record(20);
Update log
0.3.0
- Add github action for auto publish to npm
- Add option to update TimeUnixNano in metrics from Exporter
0.2.0
- Update otel dependencies and naming conventions
- Update docs
- Fix exporting modules names
0.1.0