The easiest way to add OpenTelemetry Logs & Traces to NodeJS app.
Powered by Otelic.com magic ✨🧙♂️
Get free API Key at https://app.otelic.com/ at Workspace Settings
These 3 env variables config Otelic Simple OpenTelemetry package:
Only required is OTELIC_API_KEY
OTELIC_API_KEY = "YOUR_FREE_API_KEY";
OTELIC_APP_NAME = "My app";
OTELIC_SERVICE_NAME = "My service";
e.g. in your package.json
{
"scripts": {
"run": "export OTELIC_API_KEY=\"YOUR_FREE_API_KEY\" && export OTELIC_APP_NAME=\"My App\" && export OTELIC_SERVICE_NAME=\"My service\" && node index.js"
}
}
You have to load these env variables before importing
Make sure it happens before anything else, while it will auto-instrument your console, SQL, Mongo, 3rd-party services code to create traces, spans.
import "simple-opentelemetry";
That's it :)
Make sure to have this as first thing in your app.
Once done, go to https://app.otelic.com/ and enjoy your free searchable Logs, Traces & Alerts.
Enjoy 🎉
process.env.OTELIC_API_KEY = "MY_FREE_API_KEY";
import "simple-opentelemetry";
Your imported file is executed immediately before env variables were actually available. Check how to define env variables below:
Looks a bit messy, but it's super easy, works every time and is scoped to this process only.
You can have multiple scripts defined for local, production, staging env.
{
"scripts": {
"run": "export OTELIC_API_KEY=\"YOUR_FREE_API_KEY\" && export OTELIC_APP_NAME=\"My App\" && export OTELIC_SERVICE_NAME=\"My service\" && node index.js"
}
}
process.env.OTELIC_API_KEY = "YOUR_FREE_API_KEY";
process.env.OTELIC_APP_NAME = "My app";
process.env.OTELIC_SERVICE_NAME = "My service";
import "app/index.ts";
Inside app/index.ts Your first line would be
import "simple-opentelemetry";
In your server look for file /etc/environment in new line you can add.
OTELIC_API_KEY="YOUR_FREE_API_KEY";
OTELIC_APP_NAME="My app";
OTELIC_SERVICE_NAME="My service";
Keep in mind this will be set for all processes in the machine.
pm2 process can be run with ecosystem.config.js like this:
pm2 start ecosystem.config.js
This file has many more options related to RAM usage, restart policy and else. Search for pm2 ecosystem file official docs to learn more.
ecosystem.config.js sample file:
module.exports = {
apps: [
{
name: "My app",
script: "./app.js", // Path to your application's entry file
env: {
OTELIC_API_KEY: "YOUR_FREE_API_KEY",
OTELIC_APP_NAME: "My app",
OTELIC_SERVICE_NAME: "My service",
},
},
],
};