diff --git a/packages/opencensus-instrumentation-http2/src/http2.ts b/packages/opencensus-instrumentation-http2/src/http2.ts index f755ce3e2..fe262085e 100644 --- a/packages/opencensus-instrumentation-http2/src/http2.ts +++ b/packages/opencensus-instrumentation-http2/src/http2.ts @@ -14,13 +14,10 @@ * limitations under the License. */ -import {Func, HeaderGetter, HeaderSetter, MessageEventType, Span, SpanKind, TraceOptions, Tracer} from '@opencensus/core'; +import {Func, HeaderGetter, HeaderSetter, MessageEventType, Span, SpanKind, TraceOptions} from '@opencensus/core'; import {HttpPlugin} from '@opencensus/instrumentation-http'; -import * as http from 'http'; import * as http2 from 'http2'; -import * as net from 'net'; import * as shimmer from 'shimmer'; -import * as tls from 'tls'; import * as url from 'url'; import * as uuid from 'uuid'; @@ -138,19 +135,20 @@ export class Http2Plugin extends HttpPlugin { } request.on('response', (responseHeaders: http2.IncomingHttpHeaders) => { + const statusCode = responseHeaders[':status'] || 200; span.addAttribute( - Http2Plugin.ATTRIBUTE_HTTP_STATUS_CODE, - `${responseHeaders[':status']}`); - span.setStatus( - Http2Plugin.parseResponseStatus(+responseHeaders[':status'])); + Http2Plugin.ATTRIBUTE_HTTP_STATUS_CODE, `${statusCode}`); + span.setStatus(Http2Plugin.parseResponseStatus(+statusCode)); }); request.on('end', () => { const userAgent = headers['user-agent'] || headers['User-Agent'] || null; - span.addAttribute( - Http2Plugin.ATTRIBUTE_HTTP_HOST, url.parse(authority).host); + const host = url.parse(authority).host; + if (host) { + span.addAttribute(Http2Plugin.ATTRIBUTE_HTTP_HOST, host); + } span.addAttribute( Http2Plugin.ATTRIBUTE_HTTP_METHOD, `${headers[':method']}`); span.addAttribute( @@ -209,21 +207,26 @@ export class Http2Plugin extends HttpPlugin { } const propagation = plugin.tracer.propagation; - const getter = { + const getter: HeaderGetter = { getHeader(name: string) { return headers[name]; } - } as HeaderGetter; + }; - const traceOptions = { - name: headers[':path'], + const traceOptions: TraceOptions = { + name: headers[':path'] || '', kind: SpanKind.SERVER, - spanContext: propagation ? propagation.extract(getter) : null - } as TraceOptions; + }; + if (propagation) { + const spanContext = propagation.extract(getter); + if (spanContext) { + traceOptions.spanContext = spanContext; + } + } // Respond is called in a stream event. We wrap it to get the sent // status code. - let statusCode: number = null; + let statusCode: number; const originalRespond = stream.respond; stream.respond = function(this: http2.Http2Stream) { // Unwrap it since respond is not allowed to be called more than once diff --git a/packages/opencensus-instrumentation-http2/test/test-http2.ts b/packages/opencensus-instrumentation-http2/test/test-http2.ts index 1cf9eadcf..f9abe2692 100644 --- a/packages/opencensus-instrumentation-http2/test/test-http2.ts +++ b/packages/opencensus-instrumentation-http2/test/test-http2.ts @@ -15,13 +15,10 @@ */ -import {CoreTracer, RootSpan, Span, SpanEventListener, TracerConfig} from '@opencensus/core'; -import {logger} from '@opencensus/core'; +import {CoreTracer, logger, RootSpan, Span, SpanEventListener} from '@opencensus/core'; import * as assert from 'assert'; import * as http2 from 'http2'; -import * as mocha from 'mocha'; import * as semver from 'semver'; -import * as shimmer from 'shimmer'; import {plugin} from '../src/'; import {Http2Plugin} from '../src/'; @@ -39,7 +36,7 @@ class RootSpanVerifier implements SpanEventListener { function assertSpanAttributes( span: Span, httpStatusCode: number, httpMethod: string, hostName: string, - path: string, userAgent: string) { + path: string, userAgent?: string) { assert.strictEqual( span.status.code, Http2Plugin.parseResponseStatus(httpStatusCode)); assert.strictEqual( @@ -48,8 +45,10 @@ function assertSpanAttributes( span.attributes[Http2Plugin.ATTRIBUTE_HTTP_METHOD], httpMethod); assert.strictEqual(span.attributes[Http2Plugin.ATTRIBUTE_HTTP_PATH], path); assert.strictEqual(span.attributes[Http2Plugin.ATTRIBUTE_HTTP_ROUTE], path); - assert.strictEqual( - span.attributes[Http2Plugin.ATTRIBUTE_HTTP_USER_AGENT], userAgent); + if (userAgent) { + assert.strictEqual( + span.attributes[Http2Plugin.ATTRIBUTE_HTTP_USER_AGENT], userAgent); + } assert.strictEqual( span.attributes[Http2Plugin.ATTRIBUTE_HTTP_STATUS_CODE], `${httpStatusCode}`); @@ -100,12 +99,14 @@ describe('Http2Plugin', () => { before(() => { tracer.registerSpanEventListener(rootSpanVerifier); - plugin.enable(http2, tracer, VERSION, {}, null); + plugin.enable(http2, tracer, VERSION, {}, ''); server = http2.createServer(); server.on('stream', (stream, requestHeaders) => { - const statusCode = requestHeaders[':path'].length > 1 ? - +requestHeaders[':path'].slice(1) : - 200; + const path = requestHeaders[':path']; + let statusCode = 200; + if (path && path.length > 1) { + statusCode = isNaN(Number(path.slice(1))) ? 200 : Number(path.slice(1)); + } stream.respond({':status': statusCode, 'content-type': 'text/plain'}); stream.end(`${statusCode}`); }); @@ -139,7 +140,7 @@ describe('Http2Plugin', () => { rootSpanVerifier.endedRootSpans[1].name.indexOf(testPath) >= 0); const span = rootSpanVerifier.endedRootSpans[1]; - assertSpanAttributes(span, 200, 'GET', host, testPath, undefined); + assertSpanAttributes(span, 200, 'GET', host, testPath); }); }); @@ -160,8 +161,7 @@ describe('Http2Plugin', () => { 0); const span = rootSpanVerifier.endedRootSpans[1]; - assertSpanAttributes( - span, errorCode, 'GET', host, testPath, undefined); + assertSpanAttributes(span, errorCode, 'GET', host, testPath); }); }); }); @@ -179,8 +179,7 @@ describe('Http2Plugin', () => { assert.ok(root.spans[0].name.indexOf(testPath) >= 0); assert.strictEqual(root.traceId, root.spans[0].traceId); const span = root.spans[0]; - assertSpanAttributes( - span, statusCode, 'GET', host, testPath, undefined); + assertSpanAttributes(span, statusCode, 'GET', host, testPath); }); }); }); @@ -201,8 +200,7 @@ describe('Http2Plugin', () => { assert.strictEqual(root.traceId, root.spans[0].traceId); const span = root.spans[0]; - assertSpanAttributes( - span, errorCode, 'GET', host, testPath, undefined); + assertSpanAttributes(span, errorCode, 'GET', host, testPath); }); }); }); diff --git a/packages/opencensus-instrumentation-http2/tsconfig.json b/packages/opencensus-instrumentation-http2/tsconfig.json index c080f6be0..4a7edf10f 100644 --- a/packages/opencensus-instrumentation-http2/tsconfig.json +++ b/packages/opencensus-instrumentation-http2/tsconfig.json @@ -6,7 +6,8 @@ "pretty": true, "module": "commonjs", "target": "es6", - "strictNullChecks": false, + "strictNullChecks": true, + "noUnusedLocals": true }, "include": [ "src/**/*.ts", @@ -16,4 +17,3 @@ "node_modules" ] } - diff --git a/packages/opencensus-instrumentation-https/src/https.ts b/packages/opencensus-instrumentation-https/src/https.ts index 39759bca4..4f03c4da9 100644 --- a/packages/opencensus-instrumentation-https/src/https.ts +++ b/packages/opencensus-instrumentation-https/src/https.ts @@ -20,7 +20,6 @@ import * as http from 'http'; import * as https from 'https'; import * as semver from 'semver'; import * as shimmer from 'shimmer'; -import * as url from 'url'; /** Https instrumentation plugin for Opencensus */ export class HttpsPlugin extends HttpPlugin { diff --git a/packages/opencensus-instrumentation-https/test/test-https.ts b/packages/opencensus-instrumentation-https/test/test-https.ts index 04851fb04..fa6dfc4ec 100644 --- a/packages/opencensus-instrumentation-https/test/test-https.ts +++ b/packages/opencensus-instrumentation-https/test/test-https.ts @@ -14,16 +14,12 @@ * limitations under the License. */ -import {CoreTracer, RootSpan, Span, SpanEventListener, TracerConfig} from '@opencensus/core'; -import {logger} from '@opencensus/core'; +import {CoreTracer, logger, RootSpan, Span, SpanEventListener} from '@opencensus/core'; import * as assert from 'assert'; import * as fs from 'fs'; -import {IncomingMessage} from 'http'; import * as https from 'https'; -import * as mocha from 'mocha'; import * as nock from 'nock'; import * as shimmer from 'shimmer'; -import * as url from 'url'; import {plugin} from '../src/'; import {HttpsPlugin} from '../src/'; @@ -83,7 +79,7 @@ const httpsOptions = { function assertSpanAttributes( span: Span, httpStatusCode: number, httpMethod: string, hostName: string, - path: string, userAgent: string) { + path: string, userAgent?: string) { assert.strictEqual( span.status.code, HttpsPlugin.parseResponseStatus(httpStatusCode)); assert.strictEqual( @@ -92,8 +88,10 @@ function assertSpanAttributes( span.attributes[HttpsPlugin.ATTRIBUTE_HTTP_METHOD], httpMethod); assert.strictEqual(span.attributes[HttpsPlugin.ATTRIBUTE_HTTP_PATH], path); assert.strictEqual(span.attributes[HttpsPlugin.ATTRIBUTE_HTTP_ROUTE], path); - assert.strictEqual( - span.attributes[HttpsPlugin.ATTRIBUTE_HTTP_USER_AGENT], userAgent); + if (userAgent) { + assert.strictEqual( + span.attributes[HttpsPlugin.ATTRIBUTE_HTTP_USER_AGENT], userAgent); + } assert.strictEqual( span.attributes[HttpsPlugin.ATTRIBUTE_HTTP_STATUS_CODE], `${httpStatusCode}`); @@ -127,7 +125,7 @@ describe('HttpsPlugin', () => { (url: string) => url === `${urlHost}/ignored/function` ] }, - null); + ''); tracer.registerSpanEventListener(rootSpanVerifier); server = https.createServer(httpsOptions, (request, response) => { response.end('Test Server Response'); @@ -170,8 +168,7 @@ describe('HttpsPlugin', () => { assert.strictEqual(rootSpanVerifier.endedRootSpans.length, 1); const span = rootSpanVerifier.endedRootSpans[0]; - assertSpanAttributes( - span, 200, 'GET', hostName, testPath, undefined); + assertSpanAttributes(span, 200, 'GET', hostName, testPath); }); }); @@ -194,8 +191,7 @@ describe('HttpsPlugin', () => { assert.strictEqual(rootSpanVerifier.endedRootSpans.length, 1); const span = rootSpanVerifier.endedRootSpans[0]; assertSpanAttributes( - span, httpErrorCodes[i], 'GET', hostName, testPath, - undefined); + span, httpErrorCodes[i], 'GET', hostName, testPath); }); }); } @@ -212,8 +208,7 @@ describe('HttpsPlugin', () => { assert.ok(root.spans[0].name.indexOf(testPath) >= 0); assert.strictEqual(root.traceId, root.spans[0].traceId); const span = root.spans[0]; - assertSpanAttributes( - span, 200, 'GET', hostName, testPath, undefined); + assertSpanAttributes(span, 200, 'GET', hostName, testPath); }); }); }); @@ -236,8 +231,7 @@ describe('HttpsPlugin', () => { const span = root.spans[0]; assertSpanAttributes( - span, httpErrorCodes[i], 'GET', hostName, testPath, - undefined); + span, httpErrorCodes[i], 'GET', hostName, testPath); }); }); }); diff --git a/packages/opencensus-instrumentation-https/tsconfig.json b/packages/opencensus-instrumentation-https/tsconfig.json index 217e48eca..4a7edf10f 100644 --- a/packages/opencensus-instrumentation-https/tsconfig.json +++ b/packages/opencensus-instrumentation-https/tsconfig.json @@ -6,7 +6,8 @@ "pretty": true, "module": "commonjs", "target": "es6", - "strictNullChecks": false + "strictNullChecks": true, + "noUnusedLocals": true }, "include": [ "src/**/*.ts", @@ -16,4 +17,3 @@ "node_modules" ] } -