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

Commit fe80268

Browse files
fabiogomessilvakjin
authored andcommitted
refactor: address instrumentation-http TODOs (#36)
* refactor: address instrumentation-http TODOs * refactor: changes to addres review comments
1 parent d6f2fe3 commit fe80268

3 files changed

Lines changed: 24 additions & 25 deletions

File tree

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

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import * as url from 'url';
2424
import * as uuid from 'uuid';
2525

2626

27-
// TODO: maybe we should have a setup as a Client or as Server.
2827

2928
export type HttpModule = typeof httpModule;
3029
export type RequestFunction = typeof httpModule.request;
@@ -53,15 +52,14 @@ export class HttpPlugin extends classes.BasePlugin {
5352
}
5453

5554

56-
// TODO: moduleExports should use type HttpModule instead of any
5755
/**
5856
* Patches HTTP incoming and outcoming request functions.
5957
* @param moduleExports The http module exports
6058
* @param tracer A tracer instance to create spans on.
6159
* @param version The package version.
6260
*/
6361
// tslint:disable-next-line:no-any
64-
applyPatch(moduleExports: any, tracer: types.Tracer, version: string) {
62+
applyPatch(moduleExports: HttpModule, tracer: types.Tracer, version: string) {
6563
this.setPluginContext(moduleExports, tracer, version);
6664
this.logger = tracer.logger || logger.logger('debug');
6765

@@ -80,7 +78,7 @@ export class HttpPlugin extends classes.BasePlugin {
8078
shimmer.wrap(
8179
moduleExports && moduleExports.Server &&
8280
moduleExports.Server.prototype,
83-
'emit', this.patchIncomingRequest());
81+
'emit' as never, this.patchIncomingRequest());
8482
} else {
8583
this.logger.error(
8684
'Could not apply patch to %s.emit. Interface is not as expected.',
@@ -110,10 +108,8 @@ export class HttpPlugin extends classes.BasePlugin {
110108
/**
111109
* Creates spans for incoming requests, restoring spans' context if applied.
112110
*/
113-
patchIncomingRequest() {
114-
// TODO: evaluate if this function should return RequestFunction
115-
return (original: RequestFunction):
116-
types.Func<httpModule.ClientRequest> => {
111+
protected patchIncomingRequest() {
112+
return (original: RequestFunction) => {
117113
const plugin = this;
118114
return function incomingRequest(
119115
event: string, request: httpModule.IncomingMessage,
@@ -198,7 +194,7 @@ export class HttpPlugin extends classes.BasePlugin {
198194
* Creates spans for outgoing requests, sending spans' context for distributed
199195
* tracing.
200196
*/
201-
patchOutgoingRequest() {
197+
protected patchOutgoingRequest() {
202198
return (original: types.Func<httpModule.ClientRequest>):
203199
types.Func<httpModule.ClientRequest> => {
204200
const plugin = this;
@@ -252,16 +248,15 @@ export class HttpPlugin extends classes.BasePlugin {
252248
};
253249
}
254250

255-
// TODO: type of options shold be better define
256251
/**
257252
* Injects span's context to header for distributed tracing and finshes the
258253
* span when the response is finished.
259254
* @param original The original patched function.
260255
* @param options The arguments to the original function.
261256
*/
262-
makeRequestTrace(
257+
private makeRequestTrace(
263258
// tslint:disable-next-line:no-any
264-
request: httpModule.ClientRequest, options: any,
259+
request: httpModule.ClientRequest, options: httpModule.RequestOptions,
265260
plugin: HttpPlugin): types.Func<httpModule.ClientRequest> {
266261
return (span: types.Span): httpModule.ClientRequest => {
267262
plugin.logger.debug('makeRequestTrace');
@@ -290,15 +285,17 @@ export class HttpPlugin extends classes.BasePlugin {
290285
response.on('end', () => {
291286
plugin.logger.debug('outgoingRequest on end()');
292287
const method = response.method ? response.method : 'GET';
293-
const reqUrl = url.parse(options);
294288
const userAgent =
295289
headers ? (headers['user-agent'] || headers['User-Agent']) : null;
296290

297-
span.addAttribute(HttpPlugin.ATTRIBUTE_HTTP_HOST, reqUrl.hostname);
291+
span.addAttribute(HttpPlugin.ATTRIBUTE_HTTP_HOST, options.hostname);
298292
span.addAttribute(HttpPlugin.ATTRIBUTE_HTTP_METHOD, method);
299-
span.addAttribute(HttpPlugin.ATTRIBUTE_HTTP_PATH, reqUrl.pathname);
300-
span.addAttribute(HttpPlugin.ATTRIBUTE_HTTP_ROUTE, reqUrl.path);
301-
span.addAttribute(HttpPlugin.ATTRIBUTE_HTTP_USER_AGENT, userAgent);
293+
span.addAttribute(HttpPlugin.ATTRIBUTE_HTTP_PATH, options.path);
294+
span.addAttribute(HttpPlugin.ATTRIBUTE_HTTP_ROUTE, options.path);
295+
if (userAgent) {
296+
span.addAttribute(
297+
HttpPlugin.ATTRIBUTE_HTTP_USER_AGENT, userAgent.toString());
298+
}
302299
span.addAttribute(
303300
HttpPlugin.ATTRIBUTE_HTTP_STATUS_CODE,
304301
response.statusCode.toString());

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ describe('HttpPlugin', () => {
138138
assert.strictEqual(rootSpanVerifier.endedRootSpans.length, 1);
139139

140140
const span = rootSpanVerifier.endedRootSpans[0];
141-
assertSpanAttributes(span, 200, 'GET', hostName, testPath, null);
141+
assertSpanAttributes(span, 200, 'GET', hostName, testPath, undefined);
142142
});
143143
});
144144

@@ -161,7 +161,7 @@ describe('HttpPlugin', () => {
161161
assert.strictEqual(rootSpanVerifier.endedRootSpans.length, 1);
162162
const span = rootSpanVerifier.endedRootSpans[0];
163163
assertSpanAttributes(
164-
span, httpErrorCodes[i], 'GET', hostName, testPath, null);
164+
span, httpErrorCodes[i], 'GET', hostName, testPath, undefined);
165165
});
166166
});
167167
}
@@ -178,7 +178,7 @@ describe('HttpPlugin', () => {
178178
assert.ok(root.spans[0].name.indexOf(testPath) >= 0);
179179
assert.strictEqual(root.traceId, root.spans[0].traceId);
180180
const span = root.spans[0];
181-
assertSpanAttributes(span, 200, 'GET', hostName, testPath, null);
181+
assertSpanAttributes(span, 200, 'GET', hostName, testPath, undefined);
182182
});
183183
});
184184
});
@@ -201,7 +201,8 @@ describe('HttpPlugin', () => {
201201

202202
const span = root.spans[0];
203203
assertSpanAttributes(
204-
span, httpErrorCodes[i], 'GET', hostName, testPath, null);
204+
span, httpErrorCodes[i], 'GET', hostName, testPath,
205+
undefined);
205206
});
206207
});
207208
});

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ describe('HttpsPlugin', () => {
145145
assert.strictEqual(rootSpanVerifier.endedRootSpans.length, 1);
146146

147147
const span = rootSpanVerifier.endedRootSpans[0];
148-
assertSpanAttributes(span, 200, 'GET', hostName, testPath, null);
148+
assertSpanAttributes(span, 200, 'GET', hostName, testPath, undefined);
149149
});
150150
});
151151

@@ -168,7 +168,7 @@ describe('HttpsPlugin', () => {
168168
assert.strictEqual(rootSpanVerifier.endedRootSpans.length, 1);
169169
const span = rootSpanVerifier.endedRootSpans[0];
170170
assertSpanAttributes(
171-
span, httpErrorCodes[i], 'GET', hostName, testPath, null);
171+
span, httpErrorCodes[i], 'GET', hostName, testPath, undefined);
172172
});
173173
});
174174
}
@@ -185,7 +185,7 @@ describe('HttpsPlugin', () => {
185185
assert.ok(root.spans[0].name.indexOf(testPath) >= 0);
186186
assert.strictEqual(root.traceId, root.spans[0].traceId);
187187
const span = root.spans[0];
188-
assertSpanAttributes(span, 200, 'GET', hostName, testPath, null);
188+
assertSpanAttributes(span, 200, 'GET', hostName, testPath, undefined);
189189
});
190190
});
191191
});
@@ -208,7 +208,8 @@ describe('HttpsPlugin', () => {
208208

209209
const span = root.spans[0];
210210
assertSpanAttributes(
211-
span, httpErrorCodes[i], 'GET', hostName, testPath, null);
211+
span, httpErrorCodes[i], 'GET', hostName, testPath,
212+
undefined);
212213
});
213214
});
214215
});

0 commit comments

Comments
 (0)