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

Fix nextjs resource name #2834

Merged
merged 2 commits into from
Mar 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions packages/datadog-instrumentations/src/next.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ function wrapHandleApiRequest (handleApiRequest) {
return promise.then(handled => {
if (!handled) return handled

const page = getPageFromPath(pathname, this.dynamicRoutes)
return this.hasPage(pathname).then(pageFound => {
const page = pageFound ? pathname : getPageFromPath(pathname, this.dynamicRoutes)

pageLoadChannel.publish({ page })
pageLoadChannel.publish({ page })

return handled
return handled
})
})
})
}
Expand Down
44 changes: 44 additions & 0 deletions packages/datadog-plugin-next/test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,28 @@ describe('Plugin', function () {
.catch(done)
})

const pathTests = [
['/api/hello', '/api/hello'],
['/api/hello/world', '/api/hello/[name]'],
['/api/hello/other', '/api/hello/other']
]
pathTests.forEach(([url, expectedPath]) => {
it(`should infer the corrrect resource path (${expectedPath})`, done => {
agent
.use(traces => {
const spans = traces[0]

expect(spans[0]).to.have.property('resource', `GET ${expectedPath}`)
})
.then(done)
.catch(done)

axios
.get(`http://localhost:${port}${url}`)
.catch(done)
})
})

it('should propagate context', done => {
axios
.get(`http://localhost:${port}/api/hello/world`)
Expand Down Expand Up @@ -187,6 +209,28 @@ describe('Plugin', function () {
.catch(done)
})

const pathTests = [
['/hello', '/hello'],
['/hello/world', '/hello/[name]'],
['/hello/other', '/hello/other']
]
pathTests.forEach(([url, expectedPath]) => {
it(`should infer the corrrect resource (${expectedPath})`, done => {
agent
.use(traces => {
const spans = traces[0]

expect(spans[0]).to.have.property('resource', `GET ${expectedPath}`)
})
.then(done)
.catch(done)

axios
.get(`http://localhost:${port}${url}`)
.catch(done)
})
})

it('should handle pages not found', done => {
agent
.use(traces => {
Expand Down
9 changes: 9 additions & 0 deletions packages/datadog-plugin-next/test/pages/api/hello/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction

export default (req, res) => {
const tracer = require('../../../../../dd-trace')
const span = tracer.scope().active()
const name = span && span.context()._name

res.status(200).json({ name })
}
9 changes: 9 additions & 0 deletions packages/datadog-plugin-next/test/pages/api/hello/other.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction

export default (req, res) => {
const tracer = require('../../../../../dd-trace')
const span = tracer.scope().active()
const name = span && span.context()._name

res.status(200).json({ name })
}
2 changes: 1 addition & 1 deletion packages/datadog-plugin-next/test/pages/hello/[name].js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export default function Home () {
return (
<div>
Hello World!
Hello [name]!
</div>
)
}
7 changes: 7 additions & 0 deletions packages/datadog-plugin-next/test/pages/hello/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function Home () {
return (
<div>
Hello index!
</div>
)
}
7 changes: 7 additions & 0 deletions packages/datadog-plugin-next/test/pages/hello/other.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function Home () {
return (
<div>
Hello other!
</div>
)
}