Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add express-session instrumentation #5060

Draft
wants to merge 10 commits into
base: automatic_userid_blocking
Choose a base branch
from
52 changes: 52 additions & 0 deletions packages/datadog-instrumentations/src/express-session.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
'use strict'

const { addHook } = require('./helpers/instrument')
const shimmer = require('../../datadog-shimmer')
const { channel } = require('./helpers/instrument')

const sessionMiddlewareFinishCh = channel('datadog:express-session:middleware:finish')

function wrapSessionMiddleware (sessionMiddleware) {
return function wrappedSessionMiddleware (req, res, next) {
shimmer.wrap(arguments, 2, function wrapNext (next) {
return function wrappedNext () {
if (sessionMiddlewareFinishCh.hasSubscribers) {
const abortController = new AbortController()

sessionMiddlewareFinishCh.publish({ req, res, sessionId: req.sessionID, abortController })
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
sessionMiddlewareFinishCh.publish({ req, res, sessionId: req.sessionID, abortController })
// what if session IDs are using rolling sessions or always changing or something idk ?
sessionMiddlewareFinishCh.publish({ req, res, sessionId: req.sessionID, abortController })


if (abortController.signal.aborted) return
}

return next.apply(this, arguments)
}
})

return sessionMiddleware.apply(this, arguments)
}
}

function wrapSession (session) {
return function wrappedSession () {
const sessionMiddleware = session.apply(this, arguments)

return shimmer.wrapFunction(sessionMiddleware, wrapSessionMiddleware)
}
}

addHook({
name: 'express-session',
versions: ['>=0.3.0'] // TODO
}, session => {
return shimmer.wrapFunction(session, wrapSession)
})


return shimmer.wrapFunction(session, function wrapSession {
const queryMiddleware = query.apply(this, arguments)

return shimmer.wrapFunction(queryMiddleware, queryMiddleware => function (req, res, next) {
arguments[2] = publishQueryParsedAndNext(req, res, next)
return queryMiddleware.apply(this, arguments)
})
})
Loading