Skip to content
This repository has been archived by the owner on Jul 31, 2024. It is now read-only.

Commit

Permalink
Merge pull request #131 from jmartin4563/replace-render-html
Browse files Browse the repository at this point in the history
fix: update instrumentation to wrap getServerSideProps from renderToResponseWithComponents
  • Loading branch information
jmartin4563 committed Jun 23, 2023
2 parents 0d32297 + 5e1e933 commit e785831
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 20 deletions.
66 changes: 47 additions & 19 deletions lib/next-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const SPAN_PREFIX = 'Nodejs/Nextjs'
// Version middleware is stable
// See: https://nextjs.org/docs/advanced-features/middleware
const MIN_MW_SUPPORTED_VERSION = '12.2.0'
const GET_SERVER_SIDE_PROP_VERSION = '13.4.5'

module.exports = function initialize(shim, nextServer) {
const nextVersion = shim.require('./package.json').version
Expand All @@ -25,7 +26,32 @@ module.exports = function initialize(shim, nextServer) {
return function wrappedRenderToResponseWithComponents() {
const { pathname } = arguments[0]
// this is not query params but instead url params for dynamic routes
const { query } = arguments[1]
const { query, components } = arguments[1]

if (
semver.gte(nextVersion, GET_SERVER_SIDE_PROP_VERSION) &&
components.getServerSideProps
) {
shim.record(
components,
'getServerSideProps',
function recordGetServerSideProps(shim, orig, name, [{ req, res }]) {
return {
inContext(segment) {
segment.addSpanAttributes({ 'next.page': pathname })
assignCLMAttrs(config, segment, {
'code.function': 'getServerSideProps',
'code.filepath': `pages${pathname}`
})
},
req,
res,
promise: true,
name: `${SPAN_PREFIX}/getServerSideProps/${pathname}`
}
}
)
}

shim.setTransactionUri(pathname)

Expand All @@ -52,25 +78,27 @@ module.exports = function initialize(shim, nextServer) {
}
})

shim.record(
Server.prototype,
'renderHTML',
function renderHTMLRecorder(shim, renderToHTML, name, [req, res, page]) {
return {
inContext(segment) {
segment.addSpanAttributes({ 'next.page': page })
assignCLMAttrs(config, segment, {
'code.function': 'getServerSideProps',
'code.filepath': `pages${page}`
})
},
req,
res,
promise: true,
name: `${SPAN_PREFIX}/getServerSideProps/${page}`
if (semver.lt(nextVersion, GET_SERVER_SIDE_PROP_VERSION)) {
shim.record(
Server.prototype,
'renderHTML',
function renderHTMLRecorder(shim, renderToHTML, name, [req, res, page]) {
return {
inContext(segment) {
segment.addSpanAttributes({ 'next.page': page })
assignCLMAttrs(config, segment, {
'code.function': 'getServerSideProps',
'code.filepath': `pages${page}`
})
},
req,
res,
promise: true,
name: `${SPAN_PREFIX}/getServerSideProps/${page}`
}
}
}
)
)
}

if (semver.lt(nextVersion, MIN_MW_SUPPORTED_VERSION)) {
shim.logger.warn(
Expand Down
2 changes: 1 addition & 1 deletion tests/versioned/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"node": ">=16"
},
"dependencies": {
"next": ">=13.0.0 <13.4.5",
"next": ">=13.0.0",
"react": "18.2.0",
"react-dom": "18.2.0"
},
Expand Down
6 changes: 6 additions & 0 deletions tests/versioned/segments.tap.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ function getChildSegments(uri) {
})
}

if (semver.gte(nextPkg.version, '13.4.5')) {
segments.push({
name: 'timers.setTimeout'
})
}

return segments
}

Expand Down

0 comments on commit e785831

Please sign in to comment.