feature: add endpoint for logging instrumentation

This commit is contained in:
Ondřej Synáček 2020-12-12 13:19:34 +01:00
parent 0cca2a96bf
commit 737620e2ca
5 changed files with 47 additions and 2 deletions

View File

@ -8,9 +8,9 @@ class FirebaseTransport extends Transport {
this._db = options.db
}
log(info, callback) {
async log(info, callback) {
try {
this._db.ref(`log-${new Date().getTime()}`).set(info)
await this._db.ref(`log-${new Date().getTime()}`).set(info)
callback(null, info)
this.emit('logged', info)
} catch (err) {

View File

@ -10,6 +10,7 @@ const {
notFound,
download,
downloadHTML,
track,
} = require('../routes')
const {
@ -92,6 +93,7 @@ const configureApplication = ({
app.post('/download/html', downloadHTML(appLogger || null))
app.post('/download', download(appLogger || null))
app.post('/track', track(appLogger || null))
app.use(genericErrorHandler)

View File

@ -1,9 +1,11 @@
const { error, notFound } = require('./error')
const { download, downloadHTML } = require('./download')
const { track } = require('./track')
module.exports = {
error,
notFound,
download,
downloadHTML,
track,
}

24
lib/routes/track.js Normal file
View File

@ -0,0 +1,24 @@
const track = (logger) => async (req, res) => {
try {
if (!logger) {
return res.status(501)
}
const { message, service, level, env } = req.body
await logger.log({
message,
level,
service,
env: process.env.NODE_APP || env || 'N/A',
})
res.status(200).send({ status: 'ok' })
} catch (err) {
next(err)
}
}
module.exports = {
track,
}

View File

@ -6,6 +6,23 @@ class Logger {
'color: grey',
'color: black'
)
this._log({ message, level, service })
}
_log({ message, level, service }) {
return new Promise((resolve, reject) => {
fetch('/track', {
method: 'POST',
headers: {
'Accept': 'text/html, application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({ message, level, service, env: 'browser' })
})
.then(resolve)
.catch(reject)
})
}
}