You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by ke...@apache.org on 2022/10/14 08:17:12 UTC

[skywalking-nodejs] branch master updated: fix: entry span url in endponts using Express middleware/router objects (#96)

This is an automated email from the ASF dual-hosted git repository.

kezhenxu94 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/skywalking-nodejs.git


The following commit(s) were added to refs/heads/master by this push:
     new 2b9300a  fix: entry span url in endponts using Express middleware/router objects (#96)
2b9300a is described below

commit 2b9300a1b791d369e1464db8ca2560e6d2f38d20
Author: Brandon Fergerson <bf...@apache.org>
AuthorDate: Fri Oct 14 12:17:07 2022 +0400

    fix: entry span url in endponts using Express middleware/router objects (#96)
---
 src/plugins/ExpressPlugin.ts             | 5 +++--
 tests/plugins/express/client.ts          | 5 ++++-
 tests/plugins/express/expected.data.yaml | 4 ++--
 tests/plugins/express/test.ts            | 2 +-
 4 files changed, 10 insertions(+), 6 deletions(-)

diff --git a/src/plugins/ExpressPlugin.ts b/src/plugins/ExpressPlugin.ts
index 8382f2f..3e55e72 100644
--- a/src/plugins/ExpressPlugin.ts
+++ b/src/plugins/ExpressPlugin.ts
@@ -27,6 +27,7 @@ import DummySpan from '../trace/span/DummySpan';
 import { ignoreHttpMethodCheck } from '../config/AgentConfig';
 import PluginInstaller from '../core/PluginInstaller';
 import HttpPlugin from './HttpPlugin';
+import { Request } from 'express';
 
 class ExpressPlugin implements SwPlugin {
   readonly module = 'express';
@@ -40,9 +41,9 @@ class ExpressPlugin implements SwPlugin {
     const router = installer.require('express/lib/router');
     const _handle = router.handle;
 
-    router.handle = function (req: IncomingMessage, res: ServerResponse, next: any) {
+    router.handle = function (req: Request, res: ServerResponse, next: any) {
       const carrier = ContextCarrier.from((req as any).headers || {});
-      const operation = (req.url || '/').replace(/\?.*/g, '');
+      const operation = (req.originalUrl || req.url || '/').replace(/\?.*/g, '');
       const span = ignoreHttpMethodCheck(req.method ?? 'GET')
         ? DummySpan.create()
         : ContextManager.current.newEntrySpan(operation, carrier, [Component.HTTP_SERVER, Component.EXPRESS]);
diff --git a/tests/plugins/express/client.ts b/tests/plugins/express/client.ts
index 20e0487..ccc1bb4 100644
--- a/tests/plugins/express/client.ts
+++ b/tests/plugins/express/client.ts
@@ -28,7 +28,10 @@ agent.start({
 
 const app = express();
 
-app.get('/express', (req, res) => {
+const testRouter = express.Router();
+app.use('/test', testRouter);
+
+testRouter.get('/express', (req, res) => {
   http
   .request(`http://${process.env.SERVER || 'localhost:5000'}${req.url}`, (r) => {
     let data = '';
diff --git a/tests/plugins/express/expected.data.yaml b/tests/plugins/express/expected.data.yaml
index 6924540..b53cbf9 100644
--- a/tests/plugins/express/expected.data.yaml
+++ b/tests/plugins/express/expected.data.yaml
@@ -77,7 +77,7 @@ segmentItems:
     segments:
       - segmentId: not null
         spans:
-          - operationName: /express
+          - operationName: /test/express
             operationId: 0
             parentSpanId: -1
             spanId: 0
@@ -86,7 +86,7 @@ segmentItems:
               - key: coldStart
                 value: 'true'
               - key: http.url
-                value: http://localhost:5001/express
+                value: http://localhost:5001/test/express
               - key: http.method
                 value: GET
               - key: http.status.code
diff --git a/tests/plugins/express/test.ts b/tests/plugins/express/test.ts
index b046f34..04a8f65 100644
--- a/tests/plugins/express/test.ts
+++ b/tests/plugins/express/test.ts
@@ -39,7 +39,7 @@ describe('plugin tests', () => {
   });
 
   it(__filename, async () => {
-    await waitForExpect(async () => expect((await axios.get('http://localhost:5001/express')).status).toBe(200));
+    await waitForExpect(async () => expect((await axios.get('http://localhost:5001/test/express')).status).toBe(200));
 
     const expectedData = await fs.readFile(path.join(rootDir, 'expected.data.yaml'), 'utf8');