feature: add endpoint for logging instrumentation
This commit is contained in:
parent
0cca2a96bf
commit
737620e2ca
|
@ -8,9 +8,9 @@ class FirebaseTransport extends Transport {
|
||||||
this._db = options.db
|
this._db = options.db
|
||||||
}
|
}
|
||||||
|
|
||||||
log(info, callback) {
|
async log(info, callback) {
|
||||||
try {
|
try {
|
||||||
this._db.ref(`log-${new Date().getTime()}`).set(info)
|
await this._db.ref(`log-${new Date().getTime()}`).set(info)
|
||||||
callback(null, info)
|
callback(null, info)
|
||||||
this.emit('logged', info)
|
this.emit('logged', info)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|
|
@ -10,6 +10,7 @@ const {
|
||||||
notFound,
|
notFound,
|
||||||
download,
|
download,
|
||||||
downloadHTML,
|
downloadHTML,
|
||||||
|
track,
|
||||||
} = require('../routes')
|
} = require('../routes')
|
||||||
|
|
||||||
const {
|
const {
|
||||||
|
@ -92,6 +93,7 @@ const configureApplication = ({
|
||||||
|
|
||||||
app.post('/download/html', downloadHTML(appLogger || null))
|
app.post('/download/html', downloadHTML(appLogger || null))
|
||||||
app.post('/download', download(appLogger || null))
|
app.post('/download', download(appLogger || null))
|
||||||
|
app.post('/track', track(appLogger || null))
|
||||||
|
|
||||||
|
|
||||||
app.use(genericErrorHandler)
|
app.use(genericErrorHandler)
|
||||||
|
|
|
@ -1,9 +1,11 @@
|
||||||
const { error, notFound } = require('./error')
|
const { error, notFound } = require('./error')
|
||||||
const { download, downloadHTML } = require('./download')
|
const { download, downloadHTML } = require('./download')
|
||||||
|
const { track } = require('./track')
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
error,
|
error,
|
||||||
notFound,
|
notFound,
|
||||||
download,
|
download,
|
||||||
downloadHTML,
|
downloadHTML,
|
||||||
|
track,
|
||||||
}
|
}
|
||||||
|
|
|
@ -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,
|
||||||
|
}
|
|
@ -6,6 +6,23 @@ class Logger {
|
||||||
'color: grey',
|
'color: grey',
|
||||||
'color: black'
|
'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)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue