-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
94 lines (82 loc) · 3.15 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
const { finished } = require('stream')
const { StructuredLogger } = require('./src/StructuredLogger')
const { LogSeverity } = require('./src/severity')
class Logging {
/** @param {import('./').LoggingConfig} opts */
constructor({ projectId, logName, serviceContext, requestUserExtractor, extraLabels, productionTransport }) {
/** @readonly @private */
this._serviceContext = Object.freeze({ ...serviceContext })
/** @readonly @private */
this._extraLabels = Object.assign({}, extraLabels)
/** @readonly @private */
this._extractUser = requestUserExtractor
/** @readonly */
this.logger = new StructuredLogger(projectId, logName, serviceContext, productionTransport, extraLabels)
}
/**
* @private
* @param {import('./src/StructuredLogger').Request} req
*/
_makeRequestLog(req) {
// @ts-ignore
return this.logger._requestChild(req, this._extractUser)
}
/** @returns {import('express-serve-static-core').RequestHandler} */
makeLoggingMiddleware() {
return (req, res, next) => {
Object.defineProperty(req, 'log', { value: this._makeRequestLog(req), enumerable: true, configurable: false })
next()
}
}
/**
* This should be attached after adding the result of `makeLoggingMiddleware`
* @returns {import('express-serve-static-core').ErrorRequestHandler}
*/
makeErrorMiddleware() {
return (err, /** @type {import('express-serve-static-core').Request} */ req, res, next) => {
const self = this
// Log client errors (400) as warnings
const asWarning = (err.statusCode || err.status) < 500
// Report after request finished so the error gets the final status code
const resFinishedCleanup = finished(res, function onResponseFinished() {
resFinishedCleanup()
const log = req.log || self._makeRequestLog(req)
log.reportError(err, asWarning ? LogSeverity.WARNING : undefined)
})
next(err)
}
}
/**
* @param {import('next/server').NextRequest} req
*/
nextJSMiddleware(req) {
Object.defineProperty(req, 'log', { value: this._makeRequestLog(req), enumerable: true, configurable: false })
}
/**
* @param {StructuredLogger} loggingTo
* @returns A function to call to detach from the process
*/
attachToProcess(loggingTo) {
const onUnhandledRejection = reason => {
loggingTo.reportError(reason, LogSeverity.WARNING)
}
const onUncaughtException = (err, origin) => {
err.uncaughtExceptionType = origin
loggingTo.reportError(err)
}
process.on('unhandledRejection', onUnhandledRejection)
process.on('uncaughtExceptionMonitor', onUncaughtException)
return function detachFromProcess() {
process.off('unhandledRejection', onUnhandledRejection)
process.off('uncaughtExceptionMonitor', onUncaughtException)
}
}
}
const { requestToHttpRequest } = require('./src/request-transformers')
const { extractTraceContext } = require('./src/trace-context')
module.exports = {
Logging,
LogSeverity,
requestToHttpRequest,
extractTraceContext,
}