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

refactor: use setHeader to set trace context header #643

Merged
merged 5 commits into from Jan 12, 2018

Conversation

kjin
Copy link
Contributor

@kjin kjin commented Jan 11, 2018

Fixes #639

The HTTP tracing plugin should not be trying to clone the HTTP request options object if it's not even clear if the request is being traced. This change moves the clone to after this check has been made removes the merge call in favor of using setHeader to mutate headers after http.request has been called.

@googlebot googlebot added the cla: yes This human has signed the Contributor License Agreement. label Jan 11, 2018
@codecov
Copy link

codecov bot commented Jan 11, 2018

Codecov Report

Merging #643 into master will increase coverage by 0.07%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #643      +/-   ##
==========================================
+ Coverage   96.03%   96.11%   +0.07%     
==========================================
  Files          31       31              
  Lines        1615     1621       +6     
  Branches      290      289       -1     
==========================================
+ Hits         1551     1558       +7     
+ Misses         64       63       -1
Impacted Files Coverage Δ
root/project/build/src/plugins/plugin-http2.js 95.23% <0%> (ø) ⬆️
root/project/build/src/plugins/plugin-http.js 100% <0%> (+1.23%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 202d4cb...6f7a591. Read the comment docs.

@jinwoo
Copy link
Contributor

jinwoo commented Jan 11, 2018

This change improves the performance only when the client code is not tracing. The performance degradation still happens if the code is traced, right? I'm not sure if #639 is only for the former, not for the latter. Is there a way to improve for both cases?

I'd imagine most production services would want to enable tracing all the time (at least for some of its traffic), and they wouldn't want the performance to be impacted severely.

@kjin
Copy link
Contributor Author

kjin commented Jan 11, 2018

@jinwoo As long we're claiming a guarantee that the HTTP request options object is not being mutated (this could be up for discussion in the spirit of improving performance), I'm not sure there's a way around cloning the headers object. I'm not sure what the traffic patterns are like in #639 but it might very well be possible that the number of outgoing HTTP requests indeed far exceeds the number of traced requests (we by default limit it to 10 traces/second).

@evanlucas
Copy link

@kjin would yall actually consider an option for mutating the request object? That seems like it would be the smallest performance hit. #639 is a socket.io server that makes outbound http requests on every socket pong that is received, so yes, the outbound requests greatly exceed the inbound requests for most cases.

I also agree with @jinwoo. It would be ideal to tune for the production if we can :]

@jinwoo
Copy link
Contributor

jinwoo commented Jan 11, 2018

@kjin Do you mean that this new code will degrade the performance only for 10 requests/second at maximum, while the old code will degrade for all the requests? If that is the case, I think this is in a much better state, if not ideal.

@kjin
Copy link
Contributor Author

kjin commented Jan 11, 2018

Looking through the docs I'm reminded that http.setHeader exists. As the implementation of http.request internally copies the headers object already (at least in v4/6/8/9) we might be able to have our cake and eat it too :)

@kjin kjin changed the title refactor: clone http options after root span check refactor: use setHeader to set trace context header Jan 11, 2018
Copy link
Contributor

@jinwoo jinwoo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CI is failing.

@@ -65,21 +61,27 @@ function makeRequestTrace(request, api) {
return request.apply(this, arguments);
}

options = isString(options) ? url.parse(options) : merge({}, options);
options.headers = options.headers || {};
var uri;

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

@ofrobots
Copy link
Contributor

@evanlucas

@kjin would yall actually consider an option for mutating the request object?

This was a bug. We were cloning the request options for all requests rather than the sampled requests. I expect that this change will fix the performance issue even without the change to avoid mutation.

@kjin kjin force-pushed the http-options-lazy-clone branch 3 times, most recently from c0adcc1 to 5b5d696 Compare January 11, 2018 19:52
@kjin
Copy link
Contributor Author

kjin commented Jan 11, 2018

@GoogleCloudPlatform/node-team @evanlucas PTAL

Copy link
Contributor

@jinwoo jinwoo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

// The former is already true (at least in supported Node versions up to
// v9), so we simply follow the latter.
// Ref: https://nodejs.org/dist/latest/docs/api/http.html#http_http_get_options_callback
return function makeGetTrace() {

This comment was marked as spam.

This comment was marked as spam.

@kjin
Copy link
Contributor Author

kjin commented Jan 12, 2018

CircleCI is backed up... a previous Node 6 build passed (differences are not specific to Node 6) https://circleci.com/gh/GoogleCloudPlatform/cloud-trace-nodejs/1289

Landing.

@kjin kjin merged commit 182c0cb into googleapis:master Jan 12, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes This human has signed the Contributor License Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants