OpenTelemetry Node.js Configuration
Ship traces from Node.js to OpenSearch with OpenTelemetry
Use OpenTelemetry to easily send Node.js traces to your Logit.io stack.
This sample app was created and tested with
Node Package Manager 9.6.7
Install Integration
Install
Create a new folder for the project called opentelemetry_nodejs_example
and
then open the command prompt or terminal window and navigate to the folder.
Paste in the following command:
npm init -y
This will create a package.json
file with default settings.
This guide uses the Express framework to simplify handling HTTP requests. Next we need to paste the following command into the command prompt or terminal window:
npm install express
You will now have a node_modules
folder in addition to
the package.json
and a package-lock.json
file.
We now need to install the necessary OpenTelemetry packages using npm (node package manager), the packages that we will be using are as follows:
opentelemetry-api
: Core API for OpenTelemetryopentelemetry-sdk
: SDK implementationopentelemetry-exporter-trace-otlp-grpc
: Exporter to send data to the OpenTelemetry Collector or other OTLP-compatible backends using grpcopentelemetry-exporter-trace-otlp-http
: Exporter to send data to the OpenTelemetry Collector or other OTLP-compatible backends using httpsopentelemetry-instrumentation-node
: Automatically instrument your node applications for tracing with OpenTelemetry
We install them with the following command.
npm install @opentelemetry/api @opentelemetry/sdk-node @opentelemetry/auto-instrumentations-node @opentelemetry/sdk-trace-base @opentelemetry/exporter-trace-otlp-http @opentelemetry/exporter-trace-otlp-grpc
Create three new files and name them app.js
, tracing.js
and config.js
.
Open the tracing.js
file with your choice of text editor, copy and paste the following code into the file and then save.
const config = require('./config');
const { NodeSDK } = require('@opentelemetry/sdk-node');
const { getNodeAutoInstrumentations } = require('@opentelemetry/auto-instrumentations-node');
const { Resource } = require('@opentelemetry/resources');
const { SemanticResourceAttributes } = require('@opentelemetry/semantic-conventions');
const { OTLPTraceExporter } = require(`@opentelemetry/exporter-trace-otlp-${config.protocol}`);
let sdk = new NodeSDK();
let channelCredentials = null;
let traceExporter = new OTLPTraceExporter();
// Define the service name
const serviceName = 'LogitNodeJSTestApp';
// Encode the username and password for Basic Authentication
const auth = Buffer.from(`${config.username}:${config.password}`).toString('base64');
if (config.protocol === "grpc") {
const OTLP_ENDPOINT = `grpc://${config.endpoint}:${config.port}`;
const grpc = require('@grpc/grpc-js');
const { credentials } = grpc;
const metadata = new grpc.Metadata();
metadata.set('authorization', `Basic ${auth}`);
// Create gRPC credentials with Basic Auth
channelCredentials = credentials.combineChannelCredentials(
credentials.createSsl(), // Use SSL credentials if your endpoint requires it
credentials.createFromMetadataGenerator((_params, callback) => {
callback(null, metadata);
})
);
// Configure the OTLP trace exporter with gRPC and Basic Auth
traceExporter = new OTLPTraceExporter({
url: OTLP_ENDPOINT,
credentials: channelCredentials,
});
// Create a new Node SDK instance with automatic instrumentation
sdk = new NodeSDK({
traceExporter,
instrumentations: [getNodeAutoInstrumentations()],
resource: new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: serviceName,
}),
});
} else {
const OTLP_ENDPOINT = `https://${config.endpoint}:${config.port}/v1/traces`;
// Create HTTPS credentials with Basic Auth
traceExporter = new OTLPTraceExporter({
url: OTLP_ENDPOINT,
headers: {
'Authorization': `Basic ${auth}`
}
});
// Create a new Node SDK instance with automatic instrumentation
sdk = new NodeSDK({
traceExporter,
instrumentations: [getNodeAutoInstrumentations()],
resource: new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: serviceName,
}),
});
}
// Initialize the SDK and start the tracing
sdk.start();
Next open the app.js
file with your text editor, copy and paste the following code into the file and then save.
// Import and initialize OpenTelemetry
require('./tracing');
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('Hello, World!');
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
Configuring the App
Open the config.js
file using your text editor, copy and paste the
following into the file and then save. Use the tab to choose between
sending via HTTPS or gRPC.
// config.js
module.exports = {
endpoint: "@opentelemetry.endpointAddress",
port: "@port",
protocol: "@protocol",
username: '@opentelemetry.username',
password: '@opentelemetry.password',
};
Run the NodeJs App
Run the NodeJS app with the following command in Terminal or the Command Prompt window.
node app.js
You will see feedback from the app so that you know that is running, the message should say "Server is running on http://localhost:3000 (opens in a new tab)".
The action of launching the NodeJS app will have sent traces to your stack which can now be viewed in Jaeger.
Launch Logit.io to view your traces
Launch APMIf you open your browser and enter http://localhost:3000 (opens in a new tab) you will see that action of browsing to the page served by the NodeJS app will send further traces to your stack.
How to diagnose no data in stack
If you don't see data appearing in your stack after following this integration, take a look at the troubleshooting guide for steps to diagnose and resolve the problem or contact our support team and we'll be happy to assist.