From 8e83bcad19d8b66691716780362c41da17bc0fff Mon Sep 17 00:00:00 2001 From: Selim Belhaouane Date: Tue, 21 Feb 2023 23:54:21 -0500 Subject: [PATCH 1/2] add failing test --- .../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 +++ 6 files changed, 77 insertions(+), 1 deletion(-) 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-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! +
+ ) +} From 663903833f1e34bf1c051d87e97a2cc83d7a0797 Mon Sep 17 00:00:00 2001 From: Selim Belhaouane Date: Wed, 22 Feb 2023 00:46:35 -0500 Subject: [PATCH 2/2] fix test --- packages/datadog-instrumentations/src/next.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) 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 + }) }) }) }