2022-06-08 09:09:34 +00:00
|
|
|
import { RouterContext } from '@koa/router'
|
2023-09-25 14:01:42 +00:00
|
|
|
import Sentry from '@sentry/node'
|
|
|
|
import { stripUrlQueryAndFragment } from '@sentry/utils'
|
2022-06-08 09:09:34 +00:00
|
|
|
import { Context, Next } from 'koa'
|
|
|
|
|
2022-08-30 15:35:01 +00:00
|
|
|
const release =
|
|
|
|
(process.env.APP ?? '') +
|
|
|
|
'-' +
|
|
|
|
(process.env.CONTAINER_VERSION ?? '').substring(0, 7)
|
|
|
|
|
2022-06-08 09:09:34 +00:00
|
|
|
Sentry.init({
|
|
|
|
dsn: 'https://21ddaba2424b46b4b14185dba51b1288@sentry.incubateur.net/42',
|
|
|
|
|
|
|
|
// Set tracesSampleRate to 1.0 to capture 100%
|
|
|
|
// of transactions for performance monitoring.
|
|
|
|
// We recommend adjusting this value in production
|
|
|
|
tracesSampleRate: 0.5,
|
2022-08-30 15:35:01 +00:00
|
|
|
release,
|
|
|
|
environment: release.includes('api-pr') ? 'test' : 'production',
|
2023-09-25 14:01:42 +00:00
|
|
|
|
|
|
|
integrations: [
|
|
|
|
// Automatically instrument Node.js libraries and frameworks
|
|
|
|
...Sentry.autoDiscoverNodePerformanceMonitoringIntegrations(),
|
|
|
|
],
|
2022-06-08 09:09:34 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
export default Sentry
|
|
|
|
|
2023-09-25 14:01:42 +00:00
|
|
|
export const requestHandler = async (ctx: Context, next: Next) =>
|
|
|
|
Sentry.runWithAsyncContext(() => {
|
|
|
|
const hub = Sentry.getCurrentHub()
|
|
|
|
hub.configureScope((scope) =>
|
|
|
|
scope.addEventProcessor((event) =>
|
|
|
|
Sentry.addRequestDataToEvent(event, ctx.request, {
|
|
|
|
include: { user: false },
|
|
|
|
})
|
2022-06-08 09:09:34 +00:00
|
|
|
)
|
2023-09-25 14:01:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
return next()
|
2022-06-08 09:09:34 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// this tracing middleware creates a transaction per request
|
|
|
|
export const tracingMiddleWare = async (ctx: RouterContext, next: Next) => {
|
|
|
|
const reqMethod = (ctx.method || '').toUpperCase()
|
2023-09-25 14:01:42 +00:00
|
|
|
const reqUrl = ctx.url && stripUrlQueryAndFragment(ctx.url)
|
2022-06-08 09:09:34 +00:00
|
|
|
|
|
|
|
// connect to trace of upstream app
|
|
|
|
let traceparentData
|
|
|
|
if (ctx.request.get('sentry-trace')) {
|
2023-09-25 14:01:42 +00:00
|
|
|
traceparentData = Sentry.extractTraceparentData(
|
2022-06-08 09:09:34 +00:00
|
|
|
ctx.request.get('sentry-trace')
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const transaction = Sentry.startTransaction({
|
|
|
|
name: `${reqMethod} ${reqUrl}`,
|
|
|
|
op: 'http.server',
|
|
|
|
...traceparentData,
|
|
|
|
})
|
|
|
|
|
|
|
|
ctx.__sentry_transaction = transaction
|
|
|
|
|
|
|
|
// We put the transaction on the scope so users can attach children to it
|
|
|
|
Sentry.getCurrentHub().configureScope((scope) => {
|
|
|
|
scope.setSpan(transaction)
|
|
|
|
})
|
|
|
|
|
|
|
|
ctx.res.on('finish', () => {
|
|
|
|
// Push `transaction.finish` to the next event loop so open spans have a chance to finish before the transaction closes
|
|
|
|
setImmediate(() => {
|
|
|
|
// if using koa router, a nicer way to capture transaction using the matched route
|
|
|
|
if (ctx._matchedRoute) {
|
|
|
|
const mountPath = (ctx.mountPath as undefined | string) || ''
|
2023-09-25 14:01:42 +00:00
|
|
|
transaction.setName(`${reqMethod} ${mountPath}${ctx._matchedRoute}`)
|
2022-06-08 09:09:34 +00:00
|
|
|
}
|
|
|
|
transaction.setHttpStatus(ctx.status)
|
|
|
|
transaction.finish()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
await next()
|
|
|
|
}
|