-
Notifications
You must be signed in to change notification settings - Fork 318
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
Use req.host
instead of req.headers['host']
on express
#1080
Comments
If anyone has the same issue, here's the workaround I'm using: import tracer from 'dd-trace';
// from https://github.com/DataDog/dd-trace-js/blob/86c57e0eaba9c83f6067752f83af3d25ec578e3b/packages/dd-trace/src/plugins/util/web.js#L408
// we changed `req.headers['host']` with `req.host`
function extractURL(req) {
const headers = req.headers;
if (req.stream) {
return `${headers[HTTP2_HEADER_SCHEME]}://${headers[HTTP2_HEADER_AUTHORITY]}${headers[HTTP2_HEADER_PATH]}`;
} else {
const protocol = req.connection.encrypted ? 'https' : 'http';
return `${protocol}://${req.host}${req.originalUrl || req.url}`;
}
}
tracer.init();
tracer.use('express', {
hooks: {
request: (span, req, res) => {
const url = extractURL(req);
span.setTag('http.url', url.split('?')[0]);
},
},
});
export default tracer; |
The reasoning was that |
That makes total sense. But I wonder about replicating what express does, as it is something that depends on a configuration. What about using req.host if it exists and falling back on the headers if not? Not sure what other framework do 🤔 |
Do you think it would make sense to add a configuration on the tracer as well? It definitely makes sense to use |
I'm checking also other frameworks: fastifyuses koafrom the docs:
hapiI wasn't really able to find much, but it seems they do things a bit differently: https://stackoverflow.com/questions/31840286/how-to-get-the-full-url-for-a-request-in-hapi so maybe we should pass a function to fetch the host/url to the web util, so we can customise the behaviour per framework? |
Right, it really looks like each one has their own config and their own way to support the header. Then I agree that the best approach would be by framework since it would mean we can support the configuration out of the box. |
I can attempt a PR for express :) Not sure where I could do this though, maybe I can pass a function in normalize config, here:
this function would be used to get the correct host. |
I would say the easiest is probably to add a |
@rochdev it is probably faster to ask you, where would I put the host? I think I need to call setHost before instrumenting the request, so I can't do |
@patrick91 Can you clarify why you can't use |
if I understood correctly https://github.com/DataDog/dd-trace-js/pull/1082/files#diff-0b84b4276780e2d2c5b9e1c34dc86b78R57 I guess I could create _datadog when needed, but doesn't feel right |
It's possible to call |
@rochdev thanks! I'm having some troubles with |
Hi, I was trying to improve our tracing to show the correct hostname, we run lambdas under API Gateway with a Cloudfront proxy in front of it, so the host in the header is not the right one, since the right one is in the
X-Forwarded-Host
header.You can allow express to trust the
X-Forwarded-Host
header, see: https://expressjs.com/en/guide/behind-proxies.htmlThis will set the correct host on
req.host
, but looks like this is not being used by dd-trace-js[1]. Is there any reason for that?[1] https://github.com/DataDog/dd-trace-js/blob/master/packages/dd-trace/src/plugins/util/web.js#L415
The text was updated successfully, but these errors were encountered: