Skip to content

Commit e0f7f37

Browse files
starts logging when server starts 1
1 parent 1c92094 commit e0f7f37

File tree

4 files changed

+31
-31
lines changed

4 files changed

+31
-31
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ This package now includes database logging using Prisma with SQL Server. Follow
3939

4040
Replace username, password, localhost, and your_database_name with your actual database credentials.
4141

42-
2. In your main project's root directory, create a `prisma` folder if it doesn't exist. Inside this folder, create a `schema.prisma` file with the following content:
42+
2. In your main project's root directory, create a `prisma` folder if it doesn't exist. Inside this folder, create a `schema.prisma` file if it doesn't exist and add the following content:
4343

4444
```prisma
4545
datasource db {

src/helpers/gather-os-metrics.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ module.exports = (io, span, config) => {
2626
return;
2727
}
2828

29-
const last = span.responses[span.responses.length - 1];
29+
const last = span.responses[span.responses.length - 1] || defaultResponse;
3030

3131
// Convert from B to MB
3232
stat.memory = stat.memory / 1024 / 1024;
@@ -70,6 +70,8 @@ module.exports = (io, span, config) => {
7070
if (span.os.length >= span.retention) span.os.shift();
7171
if (span.responses[0] && span.responses.length > span.retention) span.responses.shift();
7272

73-
sendMetrics(io, span);
73+
if (io) {
74+
sendMetrics(io, span);
75+
}
7476
});
7577
};

src/helpers/socket-io-init.js

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,27 @@ const addSocketEvents = (socket, config) => {
1212

1313
module.exports = (server, config) => {
1414
if (io === null || io === undefined) {
15-
if (config.websocket !== null) {
16-
io = config.websocket;
17-
} else {
18-
io = socketIo(server);
19-
}
20-
21-
io.on('connection', socket => {
22-
if (config.authorize) {
23-
config
24-
.authorize(socket)
25-
.then(authorized => {
26-
if (!authorized) socket.disconnect('unauthorized');
27-
else addSocketEvents(socket, config);
28-
})
29-
.catch(() => socket.disconnect('unauthorized'));
15+
if (server) {
16+
if (config.websocket !== null) {
17+
io = config.websocket;
3018
} else {
31-
addSocketEvents(socket, config);
19+
io = socketIo(server);
3220
}
33-
});
21+
22+
io.on('connection', socket => {
23+
if (config.authorize) {
24+
config
25+
.authorize(socket)
26+
.then(authorized => {
27+
if (!authorized) socket.disconnect('unauthorized');
28+
else addSocketEvents(socket, config);
29+
})
30+
.catch(() => socket.disconnect('unauthorized'));
31+
} else {
32+
addSocketEvents(socket, config);
33+
}
34+
});
35+
}
3436

3537
config.spans.forEach(span => {
3638
span.os = [];
@@ -40,5 +42,8 @@ module.exports = (server, config) => {
4042
// Don't keep Node.js process up
4143
interval.unref();
4244
});
45+
46+
// Start gathering metrics immediately
47+
gatherOsMetrics(io, config.spans[0], config);
4348
}
4449
};

src/middleware-wrapper.js

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,9 @@ const middlewareWrapper = config => {
6464
}
6565
};
6666

67-
/* Provide two properties, the middleware and HTML page renderer separately
68-
* so that the HTML page can be authenticated while the middleware can be
69-
* earlier in the request handling chain. Use like:
70-
* ```
71-
* const statusMonitor = require('express-status-monitor')(config);
72-
* server.use(statusMonitor);
73-
* server.get('/status', isAuthenticated, statusMonitor.pageRoute);
74-
* ```
75-
* discussion: https://github.com/RafalWilinski/express-status-monitor/issues/63
76-
*/
67+
// Start logging immediately
68+
socketIoInit(null, validatedConfig);
69+
7770
middleware.middleware = middleware;
7871
middleware.pageRoute = (req, res) => {
7972
healthChecker(validatedConfig.healthChecks).then(results => {
@@ -84,4 +77,4 @@ const middlewareWrapper = config => {
8477
return middleware;
8578
};
8679

87-
module.exports = middlewareWrapper;
80+
module.exports = middlewareWrapper;

0 commit comments

Comments
 (0)