forked from RafalWilinski/express-status-monitor
-
Notifications
You must be signed in to change notification settings - Fork 16
/
index.js
167 lines (147 loc) · 3.95 KB
/
index.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
'use strict'
const fs = require('mz/fs')
const path = require('path')
const os = require('os')
const pidusage = require('pidusage')
const handlebars = require('handlebars')
let io
let appName
try {
appName = require('../../package.json').name
} catch (err) {}
const defaultConfig = {
path: '/status',
title: appName,
spans: [
{
interval: 1,
retention: 60
},
{
interval: 5,
retention: 60
},
{
interval: 15,
retention: 60
}
]
}
const last = function(arr) {
return arr[arr.length - 1]
}
const gatherOsMetrics = (io, span) => {
const defaultResponse = {
'2': 0,
'3': 0,
'4': 0,
'5': 0,
count: 0,
mean: 0,
timestamp: Date.now()
}
const sendMetrics = (span) => {
io.emit('stats', {
os: span.os[span.os.length - 2],
responses: span.responses[span.responses.length - 2],
interval: span.interval,
retention: span.retention
})
}
pidusage(process.pid, (err, stat) => {
if (err) {
console.error(err)
return
}
stat.memory = stat.memory / 1024 / 1024 // Convert from B to MB
stat.load = os.loadavg()
stat.timestamp = Date.now()
span.os.push(stat)
if (
!span.responses[0] ||
last(span.responses).timestamp + span.interval * 1000 < Date.now()
)
span.responses.push(defaultResponse)
if (span.os.length >= span.retention) span.os.shift()
if (span.responses[0] && span.responses.length > span.retention)
span.responses.shift()
sendMetrics(span)
})
}
const encoding = { encoding: 'utf8' }
const middlewareWrapper = (app, config) => {
if (!app.listen) {
throw new Error('First parameter must be an http server')
}
io = require('socket.io')(app)
Object.assign(defaultConfig, config)
config = defaultConfig
const htmlFilePath = path.join(__dirname, 'index.html')
const indexHtml = fs.readFileSync(htmlFilePath, encoding)
const template = handlebars.compile(indexHtml)
io.on('connection', (socket) => {
socket.emit('start', config.spans)
socket.on('change', function() {
socket.emit('start', config.spans)
})
})
config.spans.forEach((span) => {
span.os = []
span.responses = []
const interval = setInterval(
() => gatherOsMetrics(io, span),
span.interval * 1000
)
interval.unref()
})
// console.log(config)
return async (ctx, next) => {
const startTime = process.hrtime()
if (ctx.path === config.path) {
ctx.body = template(config)
} else if (ctx.url === `${config.path}/koa-monitor-frontend.js`) {
const pathToJs = path.join(__dirname, 'koa-monitor-frontend.js')
ctx.body = await fs.readFile(pathToJs, encoding)
} else {
let timer
if (config.requestTimeout) {
timer = setTimeout(() => {
record.call(ctx, true)
}, config.requestTimeout)
}
await next
timer && clearTimeout(timer)
record.call(ctx)
}
function record(timeout) {
const diff = process.hrtime(startTime)
const responseTime = diff[0] * 1e3 + diff[1] * 1e-6
// if timeout, set response code to 5xx.
const category = timeout ? 5 : Math.floor(ctx.statusCode / 100)
config.spans.forEach((span) => {
const lastResponse = last(span.responses)
if (
lastResponse &&
lastResponse.timestamp / 1000 + span.interval > Date.now() / 1000
) {
lastResponse[category]++
lastResponse.count++
lastResponse.mean =
lastResponse.mean +
(responseTime - lastResponse.mean) / lastResponse.count
} else {
span.responses.push({
'2': category === 2 ? 1 : 0,
'3': category === 3 ? 1 : 0,
'4': category === 4 ? 1 : 0,
'5': category === 5 ? 1 : 0,
count: 1,
mean: responseTime,
timestamp: Date.now()
})
}
})
}
}
}
module.exports = middlewareWrapper