Skip to content

Commit

Permalink
Fix nextjs resource name (#2834)
Browse files Browse the repository at this point in the history
* add failing test

* fix test

---------

Co-authored-by: Selim Belhaouane <selim.belhaouane@garda.com>
  • Loading branch information
2 people authored and juan-fernandez committed Mar 28, 2023
1 parent 1306feb commit 76cfcce
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 4 deletions.
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>
)
}

0 comments on commit 76cfcce

Please sign in to comment.