Skip to content
This repository was archived by the owner on Jan 21, 2026. It is now read-only.

fix: inject context http headers early if expect header exists#766

Merged
kjin merged 3 commits intogoogleapis:masterfrom
kjin:http-expect
Jun 2, 2018
Merged

fix: inject context http headers early if expect header exists#766
kjin merged 3 commits intogoogleapis:masterfrom
kjin:http-expect

Conversation

@kjin
Copy link
Copy Markdown
Contributor

@kjin kjin commented Jun 1, 2018

Fixes #763

This PR fixes a bug where our HTTP instrumentation fails if Expect is added as an outgoing header. This is because we normally inject a trace context header for a request as part of our HTTP instrumentation, but Node has a special case where it prohibits additional headers if the Expect header is detected. See: https://github.com/nodejs/node/blob/v7.10.1/lib/_http_outgoing.js#L323

To fix this, I brought back the old "slower" way of injecting trace context headers that was removed in #643, but only if the Expect header is detected.

I also added a test that fails without the source change.

@kjin kjin requested review from DominicKramer, jinwoo and ofrobots June 1, 2018 22:20
@googlebot googlebot added the cla: yes This human has signed the Contributor License Agreement. label Jun 1, 2018
Copy link
Copy Markdown
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.

Do you know why Node throws an error in that case?

Comment thread src/plugins/plugin-http.ts Outdated
* @param options Options for http.request.
*/
function hasExpectHeader(options: ClientRequestArgs|
url.URL): options is ClientRequestArgs {

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

Comment thread src/plugins/plugin-http.ts Outdated
function hasExpectHeader(options: ClientRequestArgs|
url.URL): options is ClientRequestArgs {
// tslint:disable-next-line:no-any
return !!((options as any).headers && (options as any).headers.Expect);

This comment was marked as spam.

This comment was marked as spam.

Comment thread src/plugins/plugin-http.ts Outdated
traceHeaderPreinjected = true;
// "Clone" the options object -- but don't deep-clone anything except for
// headers.
options = Object.assign({}, options);

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

@kjin
Copy link
Copy Markdown
Contributor Author

kjin commented Jun 1, 2018

@jinwoo I'm not sure. It could be unintentional -- I think I need to understand more about the Expect header first.

Comment thread src/plugins/plugin-http.ts Outdated
@@ -49,7 +49,9 @@ function getSpanName(options: ClientRequestArgs|url.URL) {
function hasExpectHeader(options: ClientRequestArgs|
url.URL): options is ClientRequestArgs {
// tslint:disable-next-line:no-any

This comment was marked as spam.

@kjin
Copy link
Copy Markdown
Contributor Author

kjin commented Jun 1, 2018

@jinwoo Seems like the behavior for a long time (since Node 0.3) has been that Node eagerly sends headers when Expect exists. https://github.com/nodejs/node/blob/d59512f6f406ceae4940fd9c83e8255f0c03173b/lib/http.js#L463

@jinwoo
Copy link
Copy Markdown
Contributor

jinwoo commented Jun 1, 2018

That makes sense because Expect means that the client is expecting a 100 response before sending the request body.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

cla: yes This human has signed the Contributor License Agreement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

NetworkingError: Can't set headers after they are sent.

3 participants