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

Commit ab7f722

Browse files
silva-fabiokjin
authored andcommitted
refactor: refactor B3Format to implement core propagation interface
1 parent 20c44f8 commit ab7f722

File tree

22 files changed

+1887
-2677
lines changed

22 files changed

+1887
-2677
lines changed
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
1-
Google Inc.
1+
# This is the list of OpenCensus authors for copyright purposes.
2+
#
3+
# This does not necessarily list everyone who has contributed code, since in
4+
# some cases, their employer may be the copyright holder. To see the full list
5+
# of contributors, see the revision history in source control.
6+
7+
Google LLC
8+
CESAR Team (www.cesar.org.br)

packages/opencensus-instrumentation-http/package-lock.json

Lines changed: 107 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/opencensus-instrumentation-http/package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "@opencensus/opencensus-instrumentation-http",
2+
"name": "@opencensus/instrumentation-http",
33
"version": "0.0.1",
44
"description": "OpenCensus is a toolkit for collecting application performance and behavior data.",
55
"main": "build/src/index.js",
@@ -40,20 +40,22 @@
4040
"devDependencies": {
4141
"@types/end-of-stream": "^1.4.0",
4242
"@types/mocha": "^2.2.48",
43+
"@types/nock": "^9.1.3",
4344
"@types/node": "^9.4.7",
4445
"@types/semver": "^5.5.0",
4546
"@types/shimmer": "^1.0.1",
4647
"@types/uuid": "^3.4.3",
4748
"gts": "^0.5.1",
4849
"mocha": "^5.0.4",
4950
"ncp": "^2.0.0",
51+
"nock": "^9.2.6",
5052
"nyc": "^11.7.1",
5153
"ts-node": "^4.0.0",
5254
"typescript": "^2.7.2"
5355
},
5456
"dependencies": {
5557
"@opencensus/opencensus-core": "^0.0.1",
56-
"@opencensus/opencensus-propagation-b3": "^0.0.1",
58+
"@opencensus/propagation-b3": "^0.0.1",
5759
"end-of-stream": "^1.4.1",
5860
"semver": "^5.5.0",
5961
"shimmer": "^1.2.0",

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

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import {types} from '@opencensus/opencensus-core';
1818
import {classes} from '@opencensus/opencensus-core';
1919
import {logger} from '@opencensus/opencensus-core';
20-
import {B3Format} from '@opencensus/opencensus-propagation-b3';
2120
import * as semver from 'semver';
2221
import * as shimmer from 'shimmer';
2322
import * as url from 'url';
@@ -45,6 +44,8 @@ export class HttpPlugin extends classes.BasePlugin {
4544
this.setPluginContext(moduleExporters, tracer, version);
4645
this.logger = tracer.logger || logger.logger('debug');
4746

47+
this.logger.debug('applying pacth to %s@%s', this.moduleName, this.version);
48+
4849
shimmer.wrap(moduleExporters, 'request', this.patchOutgoingRequest());
4950

5051
// In Node 8, http.get calls a private request method, therefore we patch it
@@ -64,7 +65,9 @@ export class HttpPlugin extends classes.BasePlugin {
6465
/** Unpatches all HTTP patched function. */
6566
applyUnpatch(): void {
6667
shimmer.unwrap(this.moduleExporters, 'request');
67-
shimmer.unwrap(this.moduleExporters, 'get');
68+
if (semver.satisfies(this.version, '>=8.0.0')) {
69+
shimmer.unwrap(this.moduleExporters, 'get');
70+
}
6871
shimmer.unwrap(
6972
this.moduleExporters && this.moduleExporters.Server &&
7073
this.moduleExporters.Server.prototype,
@@ -86,10 +89,18 @@ export class HttpPlugin extends classes.BasePlugin {
8689
return original.apply(this, arguments);
8790
}
8891

92+
plugin.logger.debug('%s plugin incomingRequest', plugin.moduleName);
93+
const propagation = plugin.tracer.propagation;
94+
const headers = arguments[1].headers;
95+
const getter = {
96+
getHeader(name: string) {
97+
return headers[name];
98+
}
99+
} as types.HeaderGetter;
89100
const traceOptions = {
90101
name: arguments[1].url,
91102
type: 'SERVER',
92-
spanContext: B3Format.extractFromHeader(arguments[1].headers)
103+
spanContext: propagation ? propagation.extract(getter) : null
93104
};
94105

95106
return plugin.tracer.startRootSpan(traceOptions, rootSpan => {
@@ -151,27 +162,38 @@ export class HttpPlugin extends classes.BasePlugin {
151162
if (arguments[0].headers &&
152163
arguments[0].headers['x-opencensus-outgoing-request']) {
153164
// tslint:disable:no-any
154-
return original.apply(this as any, arguments);
165+
plugin.logger.debug(
166+
'header with "x-opencensus-outgoing-request" - do not trace');
167+
return original.apply(this, arguments);
155168
}
156169

170+
const request = original.apply(this, arguments);
171+
172+
plugin.tracer.wrapEmitter(request);
173+
174+
plugin.logger.debug('%s plugin outgoingRequest', plugin.moduleName);
157175
const traceOptions = {
158-
name: arguments[0].pathname,
176+
name: `${request.method ? request.method : 'GET'} ${
177+
arguments[0].pathname}`,
159178
type: 'CLIENT',
160179
};
161180

181+
162182
// Checks if this outgoing request is part of an operation by checking
163183
// if there is a current root span, if so, we create a child span. In
164184
// case there is no root span, this means that the outgoing request is
165185
// the first operation, therefore we create a root span.
166186
if (!plugin.tracer.currentRootSpan) {
187+
plugin.logger.debug('outgoingRequest starting a root span');
167188
return plugin.tracer.startRootSpan(
168189
traceOptions,
169-
plugin.makeRequestTrace(original, arguments, plugin));
190+
plugin.makeRequestTrace(request, arguments, plugin));
170191
} else {
192+
plugin.logger.debug('outgoingRequest starting a child span');
171193
const span: types.Span = plugin.tracer.startChildSpan(
172194
traceOptions.name, traceOptions.type);
173195
return (span: types.Span) =>
174-
plugin.makeRequestTrace(original, arguments, plugin);
196+
plugin.makeRequestTrace(request, arguments, plugin);
175197
}
176198
};
177199
};
@@ -184,19 +206,34 @@ export class HttpPlugin extends classes.BasePlugin {
184206
* @param args The arguments to the original function.
185207
*/
186208
// tslint:disable:no-any
187-
makeRequestTrace(original: Function, args: any, plugin: HttpPlugin): any {
209+
makeRequestTrace(request: any, args: any, plugin: HttpPlugin): any {
188210
return (span: types.Span) => {
189-
args[0].headers = B3Format.injectToHeader(args[0].headers, span);
211+
plugin.logger.debug('makeRequestTrace');
190212

191-
const request = original.apply(this, args);
213+
const headers = args[0].headers;
214+
const setter = {
215+
setHeader(name: string, value: string) {
216+
headers[name] = value;
217+
}
218+
};
219+
220+
const propagation = plugin.tracer.propagation;
221+
if (propagation) {
222+
propagation.inject(setter, span.spanContext);
223+
}
192224

193225
if (!span) {
226+
plugin.logger.debug('makeRequestTrace span is null');
194227
return request;
195228
}
196229

197230
// tslint:disable:no-any
198-
request.on('response', (response: any) => {
231+
const req = request.on('response', (response: any) => {
232+
plugin.tracer.wrapEmitter(response);
233+
plugin.logger.debug('outgoingRequest on response()');
234+
199235
response.on('end', () => {
236+
plugin.logger.debug('outgoingRequest on end()');
200237
span.addAttribute('http.host', args[0].host);
201238
span.addAttribute('http.method', request.method);
202239
span.addAttribute('http.path', args[0].pathname);
@@ -211,11 +248,12 @@ export class HttpPlugin extends classes.BasePlugin {
211248
span.addMessageEvent(
212249
'MessageEventTypeSent', uuid.v4().split('-').join(''));
213250

214-
// debug('Ended span: ', span.name, span.id, span.traceId);
215251
span.end();
216252
});
217253
});
254+
plugin.tracer.wrapEmitter(req);
218255

256+
plugin.logger.debug('makeRequestTrace retun request');
219257
return request;
220258
};
221259
}

0 commit comments

Comments
 (0)