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

Add base URL in Express 4 routers #173

Closed
wants to merge 2 commits into from

Conversation

mjackson
Copy link

This patch adds the base URL to paths by default in Express 4.

See https://discuss.newrelic.com/t/node-js-agent-doesnt-put-full-express-4-router-paths-in-by-default/1776

@wraithan
Copy link
Contributor

Would you be able to add a test for this? Somewhere in here: https://github.com/newrelic/node-newrelic/tree/master/test/versioned/express4-ejs

We use tests to make sure features continue working going forward. If you don't have time to, it may take us a bit longer to be able to write the tests and make sure this works.

@mjackson
Copy link
Author

@wraithan Thanks for taking a look. I added a test, but I'm not 100% sure if it passes or not because I can't get all the way through the integration tests on my machine without it breaking. I setup all the deps using Docker, but maybe you have a machine that's all ready to go that you could run a quick test on?

Thanks!

@wraithan
Copy link
Contributor

We will be looking at this for merge in the near future. We may need to rewrite that test since it doesn't appear to actually be mounting the sub router (the IIFE doesn't return anything). We may need to be more defensive for grabbing the baseUrl for older versions of express we still support.

Agent code is about being defensive as possible while still grabbing data. Thanks for this and we'll base continued work on your patch.

@mjackson
Copy link
Author

@wraithan Thanks! I'm happy to make any changes you think are needed.

@hayes
Copy link
Contributor

hayes commented Sep 30, 2014

After looking at this a little more closely, the issue is slightly more complicated than it initially appears. With this fix using req.baseUrl is not a sufficient solution. the agent aggregates transactions using the route rather than the url eg /items/:id rather than /item/123. when using app.use, or router.use, you may specify a dynamic route rather than a static path. To properly solve this issue we will need to accumulate the routes of each router and app a request passes through to generate the finial transaction name.

This fix will take some additional work to get into a releasable state.

@mjackson
Copy link
Author

mjackson commented Oct 2, 2014

@hayes Can you provide a quick code example of a routes configuration that is not handled by this patch?

@hayes
Copy link
Contributor

hayes commented Oct 2, 2014

@mjackson

var nr = require('newrelic');
var express = require('express');
var app = express();

var users = express.Router();

app.use('/users/:id', users);

users.get('/profile', function(req, res) {
  res.end('user profile');
});

var server = app.listen(3000, function() {
  console.log('Listening on port %d', server.address().port);
});

In this example for a requests to /users/5/profile the transaction name using the provided path would be /users/5/profile, the correct behavior would be fore the transaction name to be /users/:id/profile

@hayes
Copy link
Contributor

hayes commented Oct 10, 2014

@mjackson just wanted to give you an update, we have a fix we that should go out in the next release.
if your curious about the changes, I uploaded the patch here: https://gist.github.com/hayes/cd60421a68d1c23fdf4a we have not had time to test this thoroughly so I do not recommend using it in production until it is released next week.

@mjackson
Copy link
Author

@hayes Thank you for keeping on top of this! I'm looking forward to seeing your patch land. Please feel free to close this PR when it does :)

@hayes
Copy link
Contributor

hayes commented Oct 16, 2014

@hayes hayes closed this Oct 16, 2014
cmcadams-newrelic pushed a commit to cmcadams-newrelic/node-newrelic that referenced this pull request Jan 29, 2024
jsumners-nr pushed a commit to jsumners-nr/node-newrelic that referenced this pull request Apr 16, 2024
…/follow-redirects-1.15.6

build(deps-dev): bump follow-redirects from 1.15.5 to 1.15.6
bizob2828 added a commit to bizob2828/node-newrelic that referenced this pull request Apr 19, 2024
bizob2828 added a commit to bizob2828/node-newrelic that referenced this pull request Apr 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants