Skip to content
This repository was archived by the owner on Oct 3, 2023. It is now read-only.

Commit ceb3327

Browse files
StoneDotmayurkale22
authored andcommitted
Fix the problem caused by multiple http2.connect calls (#650)
1 parent 379711a commit ceb3327

File tree

2 files changed

+63
-7
lines changed

2 files changed

+63
-7
lines changed

packages/opencensus-instrumentation-http2/src/http2.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,6 @@ export class Http2Plugin extends HttpPlugin {
8585
plugin.getPatchRequestFunction()(original, authority)
8686
);
8787

88-
shimmer.unwrap(plugin.moduleExports, 'connect');
89-
9088
return client;
9189
};
9290
};

packages/opencensus-instrumentation-http2/test/test-http2.ts

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ import * as assert from 'assert';
2525
import * as http2 from 'http2';
2626
import * as semver from 'semver';
2727

28-
import { plugin } from '../src/';
29-
import { Http2Plugin } from '../src/';
28+
import { Http2Plugin, plugin } from '../src/';
29+
import { IncomingHttpHeaders, ServerHttp2Stream } from 'http2';
3030

3131
const VERSION = process.versions.node;
3232

@@ -97,13 +97,35 @@ describe('Http2Plugin', () => {
9797
});
9898
});
9999
},
100+
getAndAssertRootSpanExistence: (
101+
client: http2.ClientHttp2Session,
102+
options: {}
103+
) => {
104+
return new Promise((resolve, reject) => {
105+
const req = client.request(options);
106+
req.on('response', headers => {
107+
assert.ok(tracer.currentRootSpan);
108+
});
109+
let data = '';
110+
req.on('data', chunk => {
111+
data += chunk;
112+
});
113+
req.on('end', () => resolve(data));
114+
req.on('error', err => reject(err));
115+
});
116+
},
100117
};
101118

102119
let server: http2.Http2Server;
120+
let server2: http2.Http2Server;
103121
let client: http2.ClientHttp2Session;
122+
let client2: http2.ClientHttp2Session;
104123
const serverPort = 8080;
124+
const serverPort2 = 8081;
105125
const host = `localhost:${serverPort}`;
126+
const host2 = `localhost:${serverPort2}`;
106127
const authority = `http://${host}`;
128+
const authority2 = `http://${host2}`;
107129

108130
const log = logger.logger();
109131
const tracer = new CoreTracer();
@@ -118,19 +140,28 @@ describe('Http2Plugin', () => {
118140
tracer.registerSpanEventListener(spanVerifier);
119141

120142
plugin.enable(http2, tracer, VERSION, {}, '');
121-
server = http2.createServer();
122-
server.on('stream', (stream, requestHeaders) => {
143+
const streamHandler = (
144+
stream: ServerHttp2Stream,
145+
requestHeaders: IncomingHttpHeaders
146+
) => {
123147
const path = requestHeaders[':path'];
124148
let statusCode = 200;
125149
if (path && path.length > 1) {
126150
statusCode = isNaN(Number(path.slice(1))) ? 200 : Number(path.slice(1));
127151
}
128152
stream.respond({ ':status': statusCode, 'content-type': 'text/plain' });
129153
stream.end(`${statusCode}`);
130-
});
154+
};
155+
server = http2.createServer();
156+
server.on('stream', streamHandler);
131157
server.listen(serverPort);
132158

159+
server2 = http2.createServer();
160+
server2.on('stream', streamHandler);
161+
server2.listen(serverPort2);
162+
133163
client = http2.connect(authority);
164+
client2 = http2.connect(authority2);
134165
});
135166

136167
beforeEach(() => {
@@ -139,7 +170,9 @@ describe('Http2Plugin', () => {
139170

140171
after(() => {
141172
server.close();
173+
server2.close();
142174
client.destroy();
175+
client2.destroy();
143176
});
144177

145178
/** Should intercept outgoing requests */
@@ -263,6 +296,31 @@ describe('Http2Plugin', () => {
263296
assert.strictEqual(spanVerifier.endedSpans.length, 1);
264297
});
265298
});
299+
300+
it('should work correctly even when multiple clients are used', async () => {
301+
const statusCode = 200;
302+
const testPath = `/${statusCode}`;
303+
const requestOptions = { ':method': 'GET', ':path': testPath };
304+
const options = { name: 'TestRootSpan' };
305+
306+
assert.strictEqual(spanVerifier.endedSpans.length, 0);
307+
await tracer.startRootSpan(options, async () => {
308+
assert.ok(tracer.currentRootSpan);
309+
await http2Request.getAndAssertRootSpanExistence(
310+
client,
311+
requestOptions
312+
);
313+
});
314+
315+
assert.strictEqual(spanVerifier.endedSpans.length, 2);
316+
return tracer.startRootSpan(options, async () => {
317+
assert.ok(tracer.currentRootSpan);
318+
await http2Request.getAndAssertRootSpanExistence(
319+
client2,
320+
requestOptions
321+
);
322+
});
323+
});
266324
});
267325

268326
/** Should intercept incoming requests */

0 commit comments

Comments
 (0)