52 lines
2.0 KiB
JavaScript
52 lines
2.0 KiB
JavaScript
// Sendet alle 500ms eine zufällige Log-Zeile an den Collector
|
|
// Usage: node send-test-logs.js [stream-name] [collector-url]
|
|
|
|
const http = require('http');
|
|
|
|
const STREAM = process.argv[2] || 'demo-app';
|
|
const BASE = process.argv[3] || 'http://localhost:8081';
|
|
const url = new URL(`/collect/${STREAM}`, BASE);
|
|
|
|
const levels = ['DEBUG', 'INFO', 'INFO', 'INFO', 'WARN', 'ERROR', 'FATAL'];
|
|
const components = ['auth', 'db', 'http', 'cache', 'queue', 'api'];
|
|
const messages = {
|
|
DEBUG: ['Cache hit for key user:42', 'Query took 12ms', 'GC pause 8ms', 'Pool stats: active=3 idle=7'],
|
|
INFO: ['Request GET /api/users 200 (45ms)', 'User login: admin@test.com', 'Job completed: sync-data', 'Health check OK'],
|
|
WARN: ['Slow query: 2340ms', 'Memory at 82%', 'Rate limit 90%', 'Retry attempt 2/3', 'Cert expires in 7 days'],
|
|
ERROR: ['Connection timeout', 'NullPointerException at line 42', '500 POST /api/orders', 'Disk write failed'],
|
|
FATAL: ['Database connection lost!', 'Out of memory!', 'Process crash - restarting'],
|
|
};
|
|
|
|
let count = 0;
|
|
|
|
function sendLog() {
|
|
const level = levels[Math.floor(Math.random() * levels.length)];
|
|
const comp = components[Math.floor(Math.random() * components.length)];
|
|
const msgs = messages[level] || messages.INFO;
|
|
const msg = msgs[Math.floor(Math.random() * msgs.length)];
|
|
const ts = new Date().toISOString();
|
|
const line = `${ts} ${level.padEnd(5)} [${comp}] ${msg}`;
|
|
|
|
const data = line;
|
|
const req = http.request({
|
|
hostname: url.hostname,
|
|
port: url.port,
|
|
path: url.pathname,
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'text/plain' },
|
|
}, (res) => {
|
|
count++;
|
|
if (count % 10 === 0) process.stdout.write(`\r Sent: ${count} logs to stream "${STREAM}"`);
|
|
});
|
|
req.on('error', (e) => console.error(`\n Error: ${e.message}`));
|
|
req.write(data);
|
|
req.end();
|
|
}
|
|
|
|
console.log(`\n 📡 Sending dummy logs to ${BASE}/collect/${STREAM}`);
|
|
console.log(` Press Ctrl+C to stop\n`);
|
|
|
|
// Send one immediately, then every 500ms
|
|
sendLog();
|
|
setInterval(sendLog, 500);
|