From 76cfcce4c59d360cc16874d1988c9897082e8df1 Mon Sep 17 00:00:00 2001 From: Selim Belhaouane Date: Thu, 16 Mar 2023 17:29:17 -0400 Subject: [PATCH] Fix nextjs resource name (#2834) * add failing test * fix test --------- Co-authored-by: Selim Belhaouane --- packages/datadog-instrumentations/src/next.js | 8 ++-- .../datadog-plugin-next/test/index.spec.js | 44 +++++++++++++++++++ .../test/pages/api/hello/index.js | 9 ++++ .../test/pages/api/hello/other.js | 9 ++++ .../test/pages/hello/[name].js | 2 +- .../test/pages/hello/index.js | 7 +++ .../test/pages/hello/other.js | 7 +++ 7 files changed, 82 insertions(+), 4 deletions(-) create mode 100644 packages/datadog-plugin-next/test/pages/api/hello/index.js create mode 100644 packages/datadog-plugin-next/test/pages/api/hello/other.js create mode 100644 packages/datadog-plugin-next/test/pages/hello/index.js create mode 100644 packages/datadog-plugin-next/test/pages/hello/other.js diff --git a/packages/datadog-instrumentations/src/next.js b/packages/datadog-instrumentations/src/next.js index 93e38a00f45..4056aa90585 100644 --- a/packages/datadog-instrumentations/src/next.js +++ b/packages/datadog-instrumentations/src/next.js @@ -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 + }) }) }) } diff --git a/packages/datadog-plugin-next/test/index.spec.js b/packages/datadog-plugin-next/test/index.spec.js index 3cb8014a3c8..3450a411acf 100644 --- a/packages/datadog-plugin-next/test/index.spec.js +++ b/packages/datadog-plugin-next/test/index.spec.js @@ -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`) @@ -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 => { diff --git a/packages/datadog-plugin-next/test/pages/api/hello/index.js b/packages/datadog-plugin-next/test/pages/api/hello/index.js new file mode 100644 index 00000000000..729cdfdb0a7 --- /dev/null +++ b/packages/datadog-plugin-next/test/pages/api/hello/index.js @@ -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 }) +} diff --git a/packages/datadog-plugin-next/test/pages/api/hello/other.js b/packages/datadog-plugin-next/test/pages/api/hello/other.js new file mode 100644 index 00000000000..729cdfdb0a7 --- /dev/null +++ b/packages/datadog-plugin-next/test/pages/api/hello/other.js @@ -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 }) +} diff --git a/packages/datadog-plugin-next/test/pages/hello/[name].js b/packages/datadog-plugin-next/test/pages/hello/[name].js index 9330c5e74e4..be5179e4284 100644 --- a/packages/datadog-plugin-next/test/pages/hello/[name].js +++ b/packages/datadog-plugin-next/test/pages/hello/[name].js @@ -1,7 +1,7 @@ export default function Home () { return (
- Hello World! + Hello [name]!
) } diff --git a/packages/datadog-plugin-next/test/pages/hello/index.js b/packages/datadog-plugin-next/test/pages/hello/index.js new file mode 100644 index 00000000000..c6632cc75f5 --- /dev/null +++ b/packages/datadog-plugin-next/test/pages/hello/index.js @@ -0,0 +1,7 @@ +export default function Home () { + return ( +
+ Hello index! +
+ ) +} diff --git a/packages/datadog-plugin-next/test/pages/hello/other.js b/packages/datadog-plugin-next/test/pages/hello/other.js new file mode 100644 index 00000000000..18e6e27a8ae --- /dev/null +++ b/packages/datadog-plugin-next/test/pages/hello/other.js @@ -0,0 +1,7 @@ +export default function Home () { + return ( +
+ Hello other! +
+ ) +}