-
Notifications
You must be signed in to change notification settings - Fork 516
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(instrumentation-express): Use router path in router span names (#…
…2319) This PR adds a new utility (getRouterPath) to recursively search the (possibly nested) parameterised router path when getting the layer metadata. This only affects the span name, not the http.route attribute of the span. http.route is still showing the mount path. (Active discussion on this: Express instrumentation does not properly handle router usage #1993 (comment)) Uses route primarily to assign the requestHandler span name instead of layerPath. layerPath does not represent the full path, only the path of the last matching router when nested routers are used. Co-authored-by: Abhijeet Prasad <[email protected]> Co-authored-by: Jamie Danielson <[email protected]> Co-authored-by: Jamie Danielson <[email protected]>
- Loading branch information
1 parent
aa9710f
commit ee5c584
Showing
7 changed files
with
320 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
...ns/node/opentelemetry-instrumentation-express/test/fixtures/use-express-nested-router.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { promisify } from 'util'; | ||
import { createTestNodeSdk } from '@opentelemetry/contrib-test-utils'; | ||
|
||
import { HttpInstrumentation } from '@opentelemetry/instrumentation-http'; | ||
import { ExpressInstrumentation } from '../../build/src/index.js'; | ||
|
||
const sdk = createTestNodeSdk({ | ||
serviceName: 'use-express-nested', | ||
instrumentations: [ | ||
new HttpInstrumentation(), | ||
new ExpressInstrumentation() | ||
] | ||
}) | ||
|
||
sdk.start(); | ||
|
||
import express from 'express'; | ||
import * as http from 'http'; | ||
|
||
const app = express(); | ||
|
||
app.use(async function simpleMiddleware(req, res, next) { | ||
// Wait a short delay to ensure this "middleware - ..." span clearly starts | ||
// before the "router - ..." span. The test rely on a start-time-based sort | ||
// of the produced spans. If they start in the same millisecond, then tests | ||
// can be flaky. | ||
await promisify(setTimeout)(10); | ||
next(); | ||
}); | ||
|
||
const userRouter = express.Router(); | ||
const postsRouter = express.Router(); | ||
|
||
postsRouter.get('/:postId', (req, res, next) => { | ||
res.json({ hello: 'yes' }); | ||
res.end(); | ||
next(); | ||
}); | ||
|
||
userRouter.get('/api/user/:id', (req, res, next) => { | ||
res.json({ hello: 'yes' }); | ||
res.end(); | ||
next(); | ||
}); | ||
|
||
userRouter.use('/api/user/:id/posts', postsRouter); | ||
|
||
app.use(userRouter); | ||
|
||
const server = http.createServer(app); | ||
await new Promise(resolve => server.listen(0, resolve)); | ||
const port = server.address().port; | ||
|
||
|
||
await new Promise(resolve => { | ||
http.get(`http://localhost:${port}/api/user/123/posts/321`, (res) => { | ||
res.resume(); | ||
res.on('end', data => { | ||
resolve(data); | ||
}); | ||
}) | ||
}); | ||
|
||
await new Promise(resolve => server.close(resolve)); | ||
await sdk.shutdown(); |
72 changes: 72 additions & 0 deletions
72
plugins/node/opentelemetry-instrumentation-express/test/fixtures/use-express-router.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { promisify } from 'util'; | ||
import { createTestNodeSdk } from '@opentelemetry/contrib-test-utils'; | ||
|
||
import { HttpInstrumentation } from '@opentelemetry/instrumentation-http'; | ||
import { ExpressInstrumentation } from '../../build/src/index.js'; | ||
|
||
const sdk = createTestNodeSdk({ | ||
serviceName: 'use-express-nested', | ||
instrumentations: [ | ||
new HttpInstrumentation(), | ||
new ExpressInstrumentation() | ||
] | ||
}) | ||
|
||
sdk.start(); | ||
|
||
import express from 'express'; | ||
import * as http from 'http'; | ||
|
||
const app = express(); | ||
|
||
app.use(async function simpleMiddleware(req, res, next) { | ||
// Wait a short delay to ensure this "middleware - ..." span clearly starts | ||
// before the "router - ..." span. The test rely on a start-time-based sort | ||
// of the produced spans. If they start in the same millisecond, then tests | ||
// can be flaky. | ||
await promisify(setTimeout)(10); | ||
next(); | ||
}); | ||
|
||
const router = express.Router(); | ||
|
||
router.get('/api/user/:id', (req, res, next) => { | ||
res.json({ hello: 'yes' }); | ||
res.end(); | ||
next(); | ||
}); | ||
|
||
app.use(router); | ||
|
||
const server = http.createServer(app); | ||
await new Promise(resolve => server.listen(0, resolve)); | ||
const port = server.address().port; | ||
|
||
|
||
await new Promise(resolve => { | ||
http.get(`http://localhost:${port}/api/user/123`, (res) => { | ||
res.resume(); | ||
res.on('end', data => { | ||
resolve(data); | ||
}); | ||
}) | ||
}); | ||
|
||
await new Promise(resolve => server.close(resolve)); | ||
await sdk.shutdown(); |
Oops, something went wrong.