Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(instr-express): normalize paths with double slashes #1995

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,9 @@ export class ExpressInstrumentation extends InstrumentationBase<
storeLayerPath(req, layerPath);
const route = (req[_LAYERS_STORE_PROPERTY] as string[])
.filter(path => path !== '/' && path !== '/*')
.join('');
.join('')
// remove duplicate slashes to normalize route
.replace(/\/{2,}/g, '/');

const attributes: Attributes = {
[SemanticAttributes.HTTP_ROUTE]: route.length > 0 ? route : '/',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,39 @@ describe('ExpressInstrumentation', () => {
}
);
});

it('should ignore double slashes in routes', async () => {
JamieDanielson marked this conversation as resolved.
Show resolved Hide resolved
const rootSpan = tracer.startSpan('rootSpan');
let rpcMetadata: RPCMetadata | undefined;
const httpServer = await serverWithMiddleware(tracer, rootSpan, app => {
app.use(express.json());
app.use((req, res, next) => {
rpcMetadata = getRPCMetadata(context.active());
next();
});
});
server = httpServer.server;
port = httpServer.port;
assert.strictEqual(memoryExporter.getFinishedSpans().length, 0);
await context.with(
trace.setSpan(context.active(), rootSpan),
async () => {
const response = await httpRequest.get(
`http://localhost:${port}/double-slashes/foo`
);
assert.strictEqual(response, 'foo');
rootSpan.end();
const requestHandlerSpan = memoryExporter
.getFinishedSpans()
.find(span => span.name.includes('request handler'));
assert.strictEqual(
requestHandlerSpan?.attributes[SemanticAttributes.HTTP_ROUTE],
'/double-slashes/:id'
);
assert.strictEqual(rpcMetadata?.route, '/double-slashes/:id');
}
);
});
});

describe('Disabling plugin', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export async function serverWithMiddleware(

const router = express.Router();
app.use('/toto', router);
app.use('/double-slashes/', router);
router.get('/:id', (req, res) => {
setImmediate(() => {
res.status(200).end(req.params.id);
Expand Down
Loading