Skip to content

Commit

Permalink
feat: support http2 client side (googleapis#616)
Browse files Browse the repository at this point in the history
* feat: support http2 client side

Much of the code is borrowed from http1. It's not easy to factor out the
common code due to differences between http1 and http2.

Server side tracing will be done after this.
  • Loading branch information
jinwoo authored Nov 30, 2017
1 parent b6bd7c2 commit 2e25b4e
Show file tree
Hide file tree
Showing 6 changed files with 567 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ export const defaultConfig = {
'grpc': path.join(pluginDirectory, 'plugin-grpc.js'),
'hapi': path.join(pluginDirectory, 'plugin-hapi.js'),
'http': path.join(pluginDirectory, 'plugin-http.js'),
'http2': path.join(pluginDirectory, 'plugin-http2.js'),
'https': path.join(pluginDirectory, 'plugin-https.js'),
'knex': path.join(pluginDirectory, 'plugin-knex.js'),
'koa': path.join(pluginDirectory, 'plugin-koa.js'),
Expand Down
187 changes: 187 additions & 0 deletions src/plugins/plugin-http2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
/**
* Copyright 2017 Google Inc. All Rights Reserved.
*
* 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
*
* http://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 {EventEmitter} from 'events';
// This is imported only for types. Generated .js file should NOT load 'http2'.
// `http2` must be used only in type annotations, not in expressions.
import * as http2 from 'http2';
import * as shimmer from 'shimmer';
import {URL} from 'url';

import {TraceAgent} from '../plugin-types';

// type of ClientHttp2Session#request()
type Http2SessionRequestFunction =
(this: http2.ClientHttp2Session, headers?: http2.OutgoingHttpHeaders,
options?: http2.ClientSessionRequestOptions) => http2.ClientHttp2Stream;

function getSpanName(authority: string|URL): string {
if (typeof authority === 'string') {
authority = new URL(authority);
}
return authority.hostname;
}

function extractMethodName(headers?: http2.OutgoingHttpHeaders): string {
if (headers && headers[':method']) {
return headers[':method'] as string;
}
return 'GET';
}

function extractPath(headers?: http2.OutgoingHttpHeaders): string {
if (headers && headers[':path']) {
return headers[':path'] as string;
}
return '/';
}

function extractUrl(
authority: string|URL, headers?: http2.OutgoingHttpHeaders): string {
if (typeof authority === 'string') {
authority = new URL(authority);
}
return `${authority.origin}${extractPath(headers)}`;
}

function isTraceAgentRequest(
headers: http2.OutgoingHttpHeaders|undefined, api: TraceAgent): boolean {
return !!headers && !!headers[api.constants.TRACE_AGENT_REQUEST_HEADER];
}

function makeRequestTrace(
request: Http2SessionRequestFunction, authority: string|URL,
api: TraceAgent): Http2SessionRequestFunction {
return function(
this: http2.Http2Session,
headers?: http2.OutgoingHttpHeaders): http2.ClientHttp2Stream {
// Create new headers so that the object passed in by the client is not
// modified.
const newHeaders: http2.OutgoingHttpHeaders =
Object.assign({}, headers || {});

// Don't trace ourselves lest we get into infinite loops.
// Note: this would not be a problem if we guarantee buffering of trace api
// calls. If there is no buffering then each trace is an http call which
// will get a trace which will be an http call.
//
// TraceWriter uses http1 so this check is not needed at the moment. But
// add the check anyway for the potential migration to http2 in the
// future.
if (isTraceAgentRequest(newHeaders, api)) {
return request.apply(this, arguments);
}

const requestLifecycleSpan =
api.createChildSpan({name: getSpanName(authority)});
if (!requestLifecycleSpan) {
return request.apply(this, arguments);
}
// Node sets the :method pseudo-header to GET if not set by client.
requestLifecycleSpan.addLabel(
api.labels.HTTP_METHOD_LABEL_KEY, extractMethodName(newHeaders));
requestLifecycleSpan.addLabel(
api.labels.HTTP_URL_LABEL_KEY, extractUrl(authority, newHeaders));
newHeaders[api.constants.TRACE_CONTEXT_HEADER_NAME] =
requestLifecycleSpan.getTraceContext();
const stream: http2.ClientHttp2Stream = request.call(
this, newHeaders, ...Array.prototype.slice.call(arguments, 1));
api.wrapEmitter(stream);

let numBytes = 0;
let listenerAttached = false;
stream
.on('response',
(headers) => {
requestLifecycleSpan.addLabel(
api.labels.HTTP_RESPONSE_CODE_LABEL_KEY, headers[':status']);
})
.on('end',
() => {
requestLifecycleSpan.addLabel(
api.labels.HTTP_RESPONSE_SIZE_LABEL_KEY, numBytes);
requestLifecycleSpan.endSpan();
})
.on('error', (err: Error) => {
if (err) {
requestLifecycleSpan.addLabel(
api.labels.ERROR_DETAILS_NAME, err.name);
requestLifecycleSpan.addLabel(
api.labels.ERROR_DETAILS_MESSAGE, err.message);
}
requestLifecycleSpan.endSpan();
});
// Streams returned by Http2Session#request are yielded in paused mode.
// Attaching a 'data' listener to the stream will switch it to flowing
// mode which could cause the stream to drain before the calling
// framework has a chance to attach their own listeners. To avoid this,
// we attach our listener lazily. This approach to tracking data size
// will not observe data read by explicitly calling `read` on the
// request. We expect this to be very uncommon as it is not mentioned in
// any of the official documentation.
shimmer.wrap(
stream, 'on',
function(
this: http2.ClientHttp2Stream,
on: (this: EventEmitter, eventName: {}, listener: Function) =>
EventEmitter) {
return function(
this: http2.ClientHttp2Stream, eventName: {}, cb: Function) {
if (eventName === 'data' && !listenerAttached) {
listenerAttached = true;
on.call(this, 'data', (chunk: Buffer|string) => {
numBytes += chunk.length;
});
}
return on.apply(this, arguments);
};
});
return stream;
};
}

function patchHttp2Session(
session: http2.Http2Session, authority: string|URL, api: TraceAgent): void {
api.wrapEmitter(session);
shimmer.wrap(
session, 'request',
(request: Http2SessionRequestFunction) =>
makeRequestTrace(request, authority, api));
}

function patchHttp2(h2: NodeJS.Module, api: TraceAgent): void {
shimmer.wrap(
h2, 'connect',
(connect: typeof http2.connect): typeof http2.connect => function(
this: NodeJS.Module, authority: string|URL) {
const session: http2.ClientHttp2Session =
connect.apply(this, arguments);
patchHttp2Session(session, authority, api);
return session;
});
}

function unpatchHttp2(h2: NodeJS.Module) {
shimmer.unwrap(h2, 'connect');
}

module.exports = [
{
file: 'http2',
patch: patchHttp2,
unpatch: unpatchHttp2,
},
];
5 changes: 3 additions & 2 deletions test/non-interference/http-e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ if (!testCommonPath) {
}
cp.execFileSync('sed', ['-i.bak', 's/exports.globalCheck = true/' +
'exports.globalCheck = false/g', testCommonPath]);
var test_glob = path.join(node_dir, 'test', 'parallel', 'test-http-*.js');
// Test files for http, https, and http2.
var test_glob = path.join(node_dir, 'test', 'parallel', 'test-http?(s|2)-*.js');

// Run tests
console.log('Running tests');
Expand Down Expand Up @@ -81,7 +82,7 @@ glob(test_glob, function(err, files) {
const matches = contents.match(/^\/\/ Flags: (.*)$/m);
const flags = matches ? matches[1] : '';

// The use of the -i flag as '-i.bak' to specify a backup extension of
// The use of the -i flag as '-i.bak' to specify a backup extension of
// '.bak' is needed to ensure that the command works on both Linux and OS X
cp.execFileSync('sed', ['-i.bak', 's#\'use strict\';#' +
'\'use strict\';' + gcloud_require + '#g', files[testCount]]);
Expand Down
Loading

0 comments on commit 2e25b4e

Please sign in to comment.