Skip to content

Commit

Permalink
fix: entry span url in endponts using Express middleware/router objec…
Browse files Browse the repository at this point in the history
…ts (#96)
  • Loading branch information
BFergerson authored Oct 14, 2022
1 parent f3a5621 commit 2b9300a
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 6 deletions.
5 changes: 3 additions & 2 deletions src/plugins/ExpressPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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]);
Expand Down
5 changes: 4 additions & 1 deletion tests/plugins/express/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '';
Expand Down
4 changes: 2 additions & 2 deletions tests/plugins/express/expected.data.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ segmentItems:
segments:
- segmentId: not null
spans:
- operationName: /express
- operationName: /test/express
operationId: 0
parentSpanId: -1
spanId: 0
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/plugins/express/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down

0 comments on commit 2b9300a

Please sign in to comment.