I just started learning opentelemetry-js.
I have a node.js application.
for it I wrote a test file for opentelemetry-js.
instrumentation.js
const { NodeSDK } = require('@opentelemetry/sdk-node');
const { ConsoleSpanExporter } = require('@opentelemetry/sdk-trace-node');
const { getNodeAutoInstrumentations } = require('@opentelemetry/auto-instrumentations-node');
const { PeriodicExportingMetricReader, ConsoleMetricExporter, MeterProvider } = require('@opentelemetry/sdk-metrics');
const { PrometheusExporter } = require('@opentelemetry/exporter-prometheus');
const options = {port: 9464};
const exporter = new PrometheusExporter(options);
const consoleExporter = new ConsoleSpanExporter()
const sdk = new NodeSDK({
traceExporter: consoleExporter,
metricReader: new PeriodicExportingMetricReader({
exporter: new ConsoleMetricExporter()
}),
instrumentations: [getNodeAutoInstrumentations()]
});
sdk.start()
Data on the server comes to the console.
{
traceId: '2b366787acd87deea0e1de4e38f73469',
parentId: undefined,
traceState: undefined,
name: 'fs statSync',
id: '9807414f70b27f7d',
kind: 0,
timestamp: 1689777151629000,
duration: 12,
attributes: {},
status: { code: 0 },
events: [],
links: []
}
{
traceId: 'b74140f2610eb31277a4874317971602',
parentId: '91529e125f5e27ed',
traceState: undefined,
name: 'middleware - query',
id: '5e34627c252e58d9',
kind: 0,
timestamp: 1689777154416000,
duration: 1141,
attributes: {
'http.route': '/',
'express.name': 'query',
'express.type': 'middleware'
},
status: { code: 0 },
events: [],
links: []
}
I installed a module for prometeus
const { PrometheusExporter } = require('@opentelemetry/exporter-prometheus');
The example for this module provides an example of creating metrics through the MeterProvider
const meterProvider = new MeterProvider();
meterProvider.addMetricReader(exporter);
I can get the metrics that I add using the meterProvider methods.
But I can't replace the output to the data console for the express application.
I tried to include an exporter for traceExporter instead of consoleExporter but it didn't work.
Here is my not working code
const { NodeSDK } = require('@opentelemetry/sdk-node');
const { ConsoleSpanExporter } = require('@opentelemetry/sdk-trace-node');
const { getNodeAutoInstrumentations } = require('@opentelemetry/auto-instrumentations-node');
const { PeriodicExportingMetricReader, ConsoleMetricExporter, MeterProvider } = require('@opentelemetry/sdk-metrics');
const { PrometheusExporter } = require('@opentelemetry/exporter-prometheus');
const options = {port: 9464};
const exporter = new PrometheusExporter(options);
const meterProvider = new MeterProvider();
meterProvider.addMetricReader(exporter);
const consoleExporter = new ConsoleSpanExporter()
const sdk = new NodeSDK({
traceExporter: exporter,
metricReader: new PeriodicExportingMetricReader({
exporter: new ConsoleMetricExporter()
}),
instrumentations: [getNodeAutoInstrumentations()]
});
sdk.start()
how can i send the data that comes in the console to the server for prometeus ?
Or what connection sequence should be in order to pass data to meterProvider ?
Or how can I connect a callback to traceExporter to add the data I need to meterProvider ?
Because I can use it to create metrics and send them. But I can't get data from the console.
const meter = meterProvider.getMeter('example-prometheus');
const counter = meter.createCounter('metric_name', {
description: 'Example of a counter'
});
counter.add(10, { pid: process.pid });
Is this not the case to use the metricReader as the exporter?
// Add your port and startServer to the Prometheus options
const options = { port: 8111 };
const exporter = new PrometheusExporter(options);
const sdk = new NodeSDK({
traceExporter: new ConsoleSpanExporter(),
metricReader: exporter,
instrumentations: [getNodeAutoInstrumentations()],
});
sdk.start();