Skip to content

Commit 7edbbcd

Browse files
authored
fix: handle fatal metrics error (#150)
If the metrics promise rejects the process will crash. Instead, handle the rejection, log the error and return a 500 to prometheus.
1 parent 2fe378b commit 7edbbcd

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/index.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,15 @@ async function main (): Promise<void> {
183183

184184
const metricsServer = createServer((req, res) => {
185185
if (req.url === argMetricsPath && req.method === 'GET') {
186-
res.writeHead(200, { 'Content-Type': 'text/plain' })
187-
void register.metrics().then((metrics) => res.end(metrics))
186+
register.metrics()
187+
.then((metrics) => {
188+
res.writeHead(200, { 'Content-Type': 'text/plain' })
189+
res.end(metrics)
190+
}, (err) => {
191+
console.error('could not read metrics', err)
192+
res.writeHead(500, { 'Content-Type': 'text/plain' })
193+
res.end('Internal Server Error')
194+
})
188195
} else {
189196
res.writeHead(404, { 'Content-Type': 'text/plain' })
190197
res.end('Not Found')

0 commit comments

Comments
 (0)