Node.js Monitoring in Production (Step-by-Step with PM2 & Custom Metrics)

🚀 Why Node.js Monitoring Matters in Production
Node.js is powerful, but also sensitive to issues like:
- Memory leaks
- Blocking the event loop
- Slow database queries
- High error rates
Without proper monitoring, these issues silently degrade performance. Real-time monitoring helps you:
✅ Detect anomalies early
✅ Improve response times
✅ Scale your app confidently
📊 Step 1: Define What You Need to Monitor
Before installing any tool, identify the critical metrics you want to track:
- CPU & Memory Usage – Detect leaks or bottlenecks
- Event Loop Lag – Ensure asynchronous operations stay fast
- API Latency & Throughput – Monitor user experience
- Error Rates – Discover hidden bugs
- Database Performance – Identify slow queries
- Custom Business Metrics – Track logins, payments, or failures
⚙️ Step 2: Install a Node.js Monitoring Agent
Use the Watchlog agent to begin collecting real-time metrics from your server:
sudo apiKey="your-api-key" server="your-server" bash -c "$(curl -L https://watchlog.io/ubuntu/watchlog-script.sh)"
This lightweight agent starts collecting CPU, memory, latency, and more—without affecting your app’s performance.
📦 Step 3: Monitor PM2 Processes (If You Use PM2)
PM2 is a popular process manager. With Watchlog’s PM2 integration, you can track:
- Live status of each app
- CPU & memory usage per process
- Automatic metric forwarding to Watchlog
How to set it up:
git clone [email protected]:watchlogserver/watchlog-pm2.git cd watchlog-pm2 npm install pm2 start watchlog-pm2
Then log into your Watchlog panel and verify the metrics are displayed.
📐 Step 4: Track Custom Application Events
Want to track business-level metrics like "failed logins" or "checkout clicks"? Use watchlog-metric
:
npm install watchlog-metric
Example:
const watchlog = require("watchlog-metric"); watchlog.increment("api_requests"); watchlog.increment("failed_logins", 3);
🚨 Step 5: Set Up Real-Time Alerts
Avoid surprises with proactive alerts. With Watchlog you can:
- ✅ Get Slack or Email alerts
- ✅ Integrate Webhooks for auto-responses
- ✅ Define thresholds for custom business rules
📁 Step 6: Analyze Logs for Root Cause
Centralized logging makes debugging easier. Use tools like Winston or Watchlog’s built-in logging.
Example using Winston:
const winston = require("winston"); const logger = winston.createLogger({ level: "info", transports: [ new winston.transports.Console(), new winston.transports.File({ filename: "error.log", level: "error" }) ] });
📉 Step 7: Visualize Performance in Dashboards
Numbers mean nothing if you can’t see them. Watchlog’s dashboard helps you:
- Track resource usage in real-time
- Analyze API performance and bottlenecks
- View error and anomaly trends
- Compare historical metrics over time
🔄 Step 8: Automate Performance Optimization
Once you collect enough data, start optimizing:
- 🔍 Identify slow queries
- 🧠 Add smart caching
- 🚦 Scale services with traffic
- ✂️ Refactor performance bottlenecks
✅ Final Thoughts: Start Monitoring Node.js Today
Production issues are inevitable—but they don’t have to be painful. With proper Node.js monitoring, you gain full control over your application’s health and performance.
💡 Start using Watchlog today and gain real-time insights into your Node.js app.
🧩 Related Articles
- Why Node.js Monitoring is Essential for Performance Optimization
- Ultimate Guide to Preventing Brute Force Attacks in Node.js
- Ultimate Guide to Node.js Monitoring Dashboard with Watchlog – Real-Time Performance Tracking
- Best Node.js Monitoring Tools Compared (2025 Update)