pms_md 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +93 -0
- package/node-monitor/ARCHITECTURE.md +341 -0
- package/node-monitor/CHANGELOG.md +105 -0
- package/node-monitor/CONTRIBUTING.md +96 -0
- package/node-monitor/DESIGN_IMPROVEMENTS.md +286 -0
- package/node-monitor/FILTER_BUTTONS_FIX.md +303 -0
- package/node-monitor/GETTING_STARTED.md +416 -0
- package/node-monitor/INSTALLATION.md +470 -0
- package/node-monitor/LICENSE +22 -0
- package/node-monitor/PUBLISHING_GUIDE.md +331 -0
- package/node-monitor/QUICK_REFERENCE.md +252 -0
- package/node-monitor/README.md +458 -0
- package/node-monitor/READY_TO_PUBLISH.md +272 -0
- package/node-monitor/SETUP_GUIDE.md +479 -0
- package/node-monitor/examples/EMAIL_SETUP_GUIDE.md +282 -0
- package/node-monitor/examples/ERROR_LOGGING_GUIDE.md +405 -0
- package/node-monitor/examples/GET_APP_PASSWORD.md +145 -0
- package/node-monitor/examples/LOG_FILES_REFERENCE.md +336 -0
- package/node-monitor/examples/QUICK_START_EMAIL.md +126 -0
- package/node-monitor/examples/express-app.js +499 -0
- package/node-monitor/examples/package-lock.json +1295 -0
- package/node-monitor/examples/package.json +18 -0
- package/node-monitor/examples/public/css/style.css +718 -0
- package/node-monitor/examples/public/js/dashboard.js +207 -0
- package/node-monitor/examples/public/js/health.js +114 -0
- package/node-monitor/examples/public/js/main.js +89 -0
- package/node-monitor/examples/public/js/metrics.js +225 -0
- package/node-monitor/examples/public/js/theme.js +138 -0
- package/node-monitor/examples/views/dashboard.ejs +20 -0
- package/node-monitor/examples/views/error-logs.ejs +1129 -0
- package/node-monitor/examples/views/health.ejs +21 -0
- package/node-monitor/examples/views/home.ejs +341 -0
- package/node-monitor/examples/views/layout.ejs +50 -0
- package/node-monitor/examples/views/metrics.ejs +16 -0
- package/node-monitor/examples/views/partials/footer.ejs +16 -0
- package/node-monitor/examples/views/partials/header.ejs +35 -0
- package/node-monitor/examples/views/partials/nav.ejs +23 -0
- package/node-monitor/examples/views/status.ejs +390 -0
- package/node-monitor/package-lock.json +4300 -0
- package/node-monitor/package.json +76 -0
- package/node-monitor/pre-publish-check.js +200 -0
- package/node-monitor/src/config/monitoringConfig.js +255 -0
- package/node-monitor/src/index.js +300 -0
- package/node-monitor/src/logger/errorLogger.js +297 -0
- package/node-monitor/src/monitors/apiErrorMonitor.js +156 -0
- package/node-monitor/src/monitors/dbConnectionMonitor.js +389 -0
- package/node-monitor/src/monitors/serverHealthMonitor.js +320 -0
- package/node-monitor/src/monitors/systemResourceMonitor.js +357 -0
- package/node-monitor/src/notifiers/emailNotifier.js +248 -0
- package/node-monitor/src/notifiers/notificationManager.js +96 -0
- package/node-monitor/src/notifiers/slackNotifier.js +209 -0
- package/node-monitor/src/views/dashboard.html +530 -0
- package/node-monitor/src/views/health.html +399 -0
- package/node-monitor/src/views/metrics.html +406 -0
- package/package.json +22 -0
|
@@ -0,0 +1,416 @@
|
|
|
1
|
+
# 🚀 Getting Started with Node Monitor
|
|
2
|
+
|
|
3
|
+
Welcome! This guide will help you get Node Monitor up and running in your application in just a few minutes.
|
|
4
|
+
|
|
5
|
+
## Prerequisites
|
|
6
|
+
|
|
7
|
+
- Node.js 14.0.0 or higher
|
|
8
|
+
- An existing Express.js application (or willingness to create one)
|
|
9
|
+
- Basic knowledge of Node.js and npm
|
|
10
|
+
|
|
11
|
+
## Step-by-Step Guide
|
|
12
|
+
|
|
13
|
+
### Step 1: Install Node Monitor
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install @projectmd/node-monitor
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
If you're using Express (recommended):
|
|
20
|
+
```bash
|
|
21
|
+
npm install express
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
### Step 2: Create Your First Monitored App
|
|
25
|
+
|
|
26
|
+
Create a file called `server.js`:
|
|
27
|
+
|
|
28
|
+
```javascript
|
|
29
|
+
const express = require('express');
|
|
30
|
+
const NodeMonitor = require('@projectmd/node-monitor');
|
|
31
|
+
|
|
32
|
+
// Initialize the monitor
|
|
33
|
+
const monitor = new NodeMonitor({
|
|
34
|
+
app: {
|
|
35
|
+
name: 'My First Monitored App',
|
|
36
|
+
version: '1.0.0'
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
// Create Express app
|
|
41
|
+
const app = express();
|
|
42
|
+
app.use(express.json());
|
|
43
|
+
|
|
44
|
+
// Add monitoring middleware
|
|
45
|
+
app.use(monitor.requestLogger());
|
|
46
|
+
|
|
47
|
+
// Add a health check endpoint
|
|
48
|
+
app.get('/health', monitor.healthCheckEndpoint());
|
|
49
|
+
|
|
50
|
+
// Your application routes
|
|
51
|
+
app.get('/', (req, res) => {
|
|
52
|
+
res.json({ message: 'Hello, World!' });
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
// Test error endpoint
|
|
56
|
+
app.get('/error', (req, res, next) => {
|
|
57
|
+
const error = new Error('This is a test error');
|
|
58
|
+
error.statusCode = 500;
|
|
59
|
+
next(error);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
// Error handling (MUST be last)
|
|
63
|
+
app.use(monitor.notFoundHandler());
|
|
64
|
+
app.use(monitor.errorMiddleware());
|
|
65
|
+
|
|
66
|
+
// Start the server
|
|
67
|
+
const server = app.listen(3000, () => {
|
|
68
|
+
console.log('Server running on http://localhost:3000');
|
|
69
|
+
|
|
70
|
+
// Start monitoring
|
|
71
|
+
monitor.start();
|
|
72
|
+
|
|
73
|
+
// Setup graceful shutdown
|
|
74
|
+
monitor.setupGracefulShutdown(server);
|
|
75
|
+
});
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### Step 3: Run Your App
|
|
79
|
+
|
|
80
|
+
```bash
|
|
81
|
+
node server.js
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
### Step 4: Test the Monitoring
|
|
85
|
+
|
|
86
|
+
Open your browser or use curl:
|
|
87
|
+
|
|
88
|
+
```bash
|
|
89
|
+
# Check health
|
|
90
|
+
curl http://localhost:3000/health
|
|
91
|
+
|
|
92
|
+
# Test your app
|
|
93
|
+
curl http://localhost:3000/
|
|
94
|
+
|
|
95
|
+
# Trigger an error
|
|
96
|
+
curl http://localhost:3000/error
|
|
97
|
+
|
|
98
|
+
# Try a non-existent route (404)
|
|
99
|
+
curl http://localhost:3000/nonexistent
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
### Step 5: Check the Logs
|
|
103
|
+
|
|
104
|
+
Look in the `logs/` directory that was automatically created:
|
|
105
|
+
|
|
106
|
+
```bash
|
|
107
|
+
# View application logs
|
|
108
|
+
cat logs/application-*.log
|
|
109
|
+
|
|
110
|
+
# View error logs
|
|
111
|
+
cat logs/error-*.log
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
## 🎉 Congratulations!
|
|
115
|
+
|
|
116
|
+
You now have a monitored Node.js application! The monitor is:
|
|
117
|
+
- ✅ Tracking all errors
|
|
118
|
+
- ✅ Logging requests
|
|
119
|
+
- ✅ Monitoring system resources
|
|
120
|
+
- ✅ Providing health check endpoints
|
|
121
|
+
|
|
122
|
+
## Next Steps
|
|
123
|
+
|
|
124
|
+
### Add Email Notifications
|
|
125
|
+
|
|
126
|
+
1. **Get Email Credentials** (Gmail example):
|
|
127
|
+
- Enable 2FA on your Gmail account
|
|
128
|
+
- Generate an App Password
|
|
129
|
+
- Copy the 16-character password
|
|
130
|
+
|
|
131
|
+
2. **Update your code**:
|
|
132
|
+
|
|
133
|
+
```javascript
|
|
134
|
+
const monitor = new NodeMonitor({
|
|
135
|
+
app: {
|
|
136
|
+
name: 'My App',
|
|
137
|
+
version: '1.0.0'
|
|
138
|
+
},
|
|
139
|
+
notifications: {
|
|
140
|
+
email: {
|
|
141
|
+
enabled: true,
|
|
142
|
+
host: 'smtp.gmail.com',
|
|
143
|
+
port: 587,
|
|
144
|
+
auth: {
|
|
145
|
+
user: 'your-email@gmail.com',
|
|
146
|
+
pass: 'your-app-password'
|
|
147
|
+
},
|
|
148
|
+
from: 'your-email@gmail.com',
|
|
149
|
+
recipients: ['admin@example.com']
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
});
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
3. **Test it**:
|
|
156
|
+
|
|
157
|
+
```javascript
|
|
158
|
+
// Add this route to test notifications
|
|
159
|
+
app.post('/test-notification', async (req, res) => {
|
|
160
|
+
await monitor.notify('info', 'Test Alert', 'This is a test notification');
|
|
161
|
+
res.json({ message: 'Notification sent!' });
|
|
162
|
+
});
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
### Add Slack Notifications
|
|
166
|
+
|
|
167
|
+
1. **Create Slack Webhook**:
|
|
168
|
+
- Go to https://api.slack.com/apps
|
|
169
|
+
- Create a new app
|
|
170
|
+
- Enable Incoming Webhooks
|
|
171
|
+
- Add webhook to your workspace
|
|
172
|
+
- Copy the webhook URL
|
|
173
|
+
|
|
174
|
+
2. **Update your code**:
|
|
175
|
+
|
|
176
|
+
```javascript
|
|
177
|
+
const monitor = new NodeMonitor({
|
|
178
|
+
notifications: {
|
|
179
|
+
slack: {
|
|
180
|
+
enabled: true,
|
|
181
|
+
webhook: 'https://hooks.slack.com/services/YOUR/WEBHOOK/URL',
|
|
182
|
+
channel: '#monitoring'
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
});
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
### Monitor Your Database
|
|
189
|
+
|
|
190
|
+
**MongoDB Example**:
|
|
191
|
+
|
|
192
|
+
```javascript
|
|
193
|
+
const mongoose = require('mongoose');
|
|
194
|
+
|
|
195
|
+
// Connect to MongoDB
|
|
196
|
+
await mongoose.connect('mongodb://localhost:27017/myapp');
|
|
197
|
+
|
|
198
|
+
// Register for monitoring
|
|
199
|
+
monitor.registerDatabase('mongodb', 'mongoose', mongoose.connection);
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
**PostgreSQL Example**:
|
|
203
|
+
|
|
204
|
+
```javascript
|
|
205
|
+
const { Pool } = require('pg');
|
|
206
|
+
|
|
207
|
+
const pool = new Pool({
|
|
208
|
+
connectionString: 'postgresql://user:password@localhost:5432/myapp'
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
monitor.registerDatabase('postgres', 'postgresql', pool);
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
### Add Custom Health Checks
|
|
215
|
+
|
|
216
|
+
```javascript
|
|
217
|
+
// Check if external API is available
|
|
218
|
+
monitor.registerHealthCheck('external-api', async () => {
|
|
219
|
+
try {
|
|
220
|
+
const response = await fetch('https://api.example.com/health');
|
|
221
|
+
return response.ok;
|
|
222
|
+
} catch {
|
|
223
|
+
return false;
|
|
224
|
+
}
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
// Check if cache is working
|
|
228
|
+
monitor.registerHealthCheck('cache', async () => {
|
|
229
|
+
try {
|
|
230
|
+
await redis.ping();
|
|
231
|
+
return true;
|
|
232
|
+
} catch {
|
|
233
|
+
return false;
|
|
234
|
+
}
|
|
235
|
+
});
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
### Use Environment Variables
|
|
239
|
+
|
|
240
|
+
1. **Create `.env` file**:
|
|
241
|
+
|
|
242
|
+
```env
|
|
243
|
+
NODE_ENV=development
|
|
244
|
+
MONITOR_APP_NAME=my-app
|
|
245
|
+
|
|
246
|
+
# Email
|
|
247
|
+
MONITOR_EMAIL_ENABLED=true
|
|
248
|
+
MONITOR_EMAIL_USER=your-email@gmail.com
|
|
249
|
+
MONITOR_EMAIL_PASS=your-app-password
|
|
250
|
+
MONITOR_EMAIL_RECIPIENTS=admin@example.com
|
|
251
|
+
|
|
252
|
+
# Slack
|
|
253
|
+
MONITOR_SLACK_ENABLED=true
|
|
254
|
+
MONITOR_SLACK_WEBHOOK=https://hooks.slack.com/services/YOUR/WEBHOOK
|
|
255
|
+
|
|
256
|
+
# Thresholds
|
|
257
|
+
MONITOR_CPU_THRESHOLD=80
|
|
258
|
+
MONITOR_MEMORY_THRESHOLD=90
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
2. **Load environment variables**:
|
|
262
|
+
|
|
263
|
+
```javascript
|
|
264
|
+
require('dotenv').config();
|
|
265
|
+
|
|
266
|
+
const monitor = new NodeMonitor();
|
|
267
|
+
// Config is automatically loaded from environment variables
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
## Common Patterns
|
|
271
|
+
|
|
272
|
+
### Pattern 1: Async Route Handlers
|
|
273
|
+
|
|
274
|
+
```javascript
|
|
275
|
+
app.get('/users', monitor.asyncHandler(async (req, res) => {
|
|
276
|
+
const users = await User.find();
|
|
277
|
+
res.json(users);
|
|
278
|
+
}));
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
### Pattern 2: Custom Error Creation
|
|
282
|
+
|
|
283
|
+
```javascript
|
|
284
|
+
app.get('/user/:id', async (req, res, next) => {
|
|
285
|
+
const user = await User.findById(req.params.id);
|
|
286
|
+
|
|
287
|
+
if (!user) {
|
|
288
|
+
const error = NodeMonitor.createError('User not found', 404);
|
|
289
|
+
return next(error);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
res.json(user);
|
|
293
|
+
});
|
|
294
|
+
```
|
|
295
|
+
|
|
296
|
+
### Pattern 3: Manual Logging
|
|
297
|
+
|
|
298
|
+
```javascript
|
|
299
|
+
app.post('/payment', async (req, res) => {
|
|
300
|
+
try {
|
|
301
|
+
const result = await processPayment(req.body);
|
|
302
|
+
|
|
303
|
+
monitor.logInfo('Payment processed successfully', {
|
|
304
|
+
amount: req.body.amount,
|
|
305
|
+
userId: req.user.id
|
|
306
|
+
});
|
|
307
|
+
|
|
308
|
+
res.json(result);
|
|
309
|
+
} catch (error) {
|
|
310
|
+
monitor.logError('payment_failed', error.message, {
|
|
311
|
+
amount: req.body.amount,
|
|
312
|
+
userId: req.user.id
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
throw error;
|
|
316
|
+
}
|
|
317
|
+
});
|
|
318
|
+
```
|
|
319
|
+
|
|
320
|
+
### Pattern 4: Custom Notifications
|
|
321
|
+
|
|
322
|
+
```javascript
|
|
323
|
+
app.post('/critical-action', async (req, res) => {
|
|
324
|
+
// Perform critical action
|
|
325
|
+
await performCriticalAction();
|
|
326
|
+
|
|
327
|
+
// Send notification
|
|
328
|
+
await monitor.notify('critical',
|
|
329
|
+
'Critical Action Performed',
|
|
330
|
+
'A critical action was just performed',
|
|
331
|
+
{
|
|
332
|
+
user: req.user.email,
|
|
333
|
+
action: 'critical-action',
|
|
334
|
+
timestamp: new Date().toISOString()
|
|
335
|
+
}
|
|
336
|
+
);
|
|
337
|
+
|
|
338
|
+
res.json({ success: true });
|
|
339
|
+
});
|
|
340
|
+
```
|
|
341
|
+
|
|
342
|
+
## Monitoring Dashboard
|
|
343
|
+
|
|
344
|
+
Access monitoring data via endpoints:
|
|
345
|
+
|
|
346
|
+
```javascript
|
|
347
|
+
// Get current status
|
|
348
|
+
app.get('/admin/monitor', (req, res) => {
|
|
349
|
+
const status = monitor.getStatus();
|
|
350
|
+
res.json(status);
|
|
351
|
+
});
|
|
352
|
+
|
|
353
|
+
// Get system info
|
|
354
|
+
app.get('/admin/system', (req, res) => {
|
|
355
|
+
const info = monitor.getSystemInfo();
|
|
356
|
+
res.json(info);
|
|
357
|
+
});
|
|
358
|
+
|
|
359
|
+
// Get metrics history
|
|
360
|
+
app.get('/admin/metrics', (req, res) => {
|
|
361
|
+
const metrics = monitor.getMetricsHistory();
|
|
362
|
+
res.json(metrics);
|
|
363
|
+
});
|
|
364
|
+
```
|
|
365
|
+
|
|
366
|
+
## Troubleshooting
|
|
367
|
+
|
|
368
|
+
### Logs not appearing?
|
|
369
|
+
|
|
370
|
+
Check that the logs directory exists and is writable:
|
|
371
|
+
```bash
|
|
372
|
+
mkdir -p logs
|
|
373
|
+
chmod 755 logs
|
|
374
|
+
```
|
|
375
|
+
|
|
376
|
+
### Emails not sending?
|
|
377
|
+
|
|
378
|
+
1. Check your SMTP credentials
|
|
379
|
+
2. Verify firewall allows outbound connections on port 587
|
|
380
|
+
3. Check spam folder
|
|
381
|
+
4. Look at error logs: `cat logs/error-*.log`
|
|
382
|
+
|
|
383
|
+
### High CPU alerts?
|
|
384
|
+
|
|
385
|
+
This is normal during startup. If it persists:
|
|
386
|
+
1. Increase the threshold
|
|
387
|
+
2. Increase the consecutive failures count
|
|
388
|
+
3. Check your application for performance issues
|
|
389
|
+
|
|
390
|
+
## Best Practices
|
|
391
|
+
|
|
392
|
+
1. **Start Simple**: Begin with basic error tracking, add features gradually
|
|
393
|
+
2. **Test Notifications**: Send test alerts before going to production
|
|
394
|
+
3. **Adjust Thresholds**: Fine-tune based on your app's normal behavior
|
|
395
|
+
4. **Use Environment Variables**: Keep credentials out of code
|
|
396
|
+
5. **Monitor the Monitor**: Check logs to ensure monitoring is working
|
|
397
|
+
6. **Set Appropriate Cooldowns**: Prevent alert fatigue
|
|
398
|
+
|
|
399
|
+
## Need Help?
|
|
400
|
+
|
|
401
|
+
- 📖 **Full Documentation**: See `README.md`
|
|
402
|
+
- 🔧 **Setup Guide**: See `SETUP_GUIDE.md`
|
|
403
|
+
- ⚡ **Quick Reference**: See `QUICK_REFERENCE.md`
|
|
404
|
+
- 🏗️ **Architecture**: See `ARCHITECTURE.md`
|
|
405
|
+
- 💡 **Examples**: Check `examples/express-app.js`
|
|
406
|
+
|
|
407
|
+
## What's Next?
|
|
408
|
+
|
|
409
|
+
- Explore all configuration options
|
|
410
|
+
- Set up production deployment
|
|
411
|
+
- Add custom health checks
|
|
412
|
+
- Integrate with your CI/CD pipeline
|
|
413
|
+
- Create custom dashboards
|
|
414
|
+
|
|
415
|
+
Happy monitoring! 🎉
|
|
416
|
+
|