generated from leventebalogh/playground-nodejs-prometheus-grafana
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
42 lines (37 loc) · 1.11 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
const { createLogger, transports } = require("winston");
const LokiTransport = require("winston-loki");
const express = require("express");
const path = require("path");
const app = express();
const port = 8080;
// Home page
app.get("/", (req, res) => {
res.sendFile(path.resolve(path.join(__dirname, 'index.html')));
});
// Logger
const logger = createLogger({
transports: [
new LokiTransport({
host: "http://localhost:3100",
// Only for development purposes
interval: 5,
labels: {
job: 'nodejs'
}
})
]
})
// Send random logs repeatedly
setInterval(() => {
const level = getRandomArrayElement(['debug', 'info', 'warn', 'error']);
const labels = getRandomArrayElement([{ env: 'dev' }, { env: 'prod' }]);
const message = getRandomArrayElement(['This is just some log message...', 'Oh snap! Something went wrong.']);
logger[level]({ message, labels })
}, 2000);
// Start the webserver
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`);
});
function getRandomArrayElement(arr) {
return arr[Math.floor(Math.random() * arr.length)];
}