diff --git a/packages/opentelemetry-plugin-http/test/functionals/http-disable.test.ts b/packages/opentelemetry-plugin-http/test/functionals/http-disable.test.ts index 78c24de3cbf..e25260fbc99 100644 --- a/packages/opentelemetry-plugin-http/test/functionals/http-disable.test.ts +++ b/packages/opentelemetry-plugin-http/test/functionals/http-disable.test.ts @@ -42,6 +42,8 @@ describe('HttpPlugin', () => { nock.enableNetConnect(); plugin.enable(http, tracer, tracer.logger); + // Ensure that http module is patched. + assert.strictEqual(http.Server.prototype.emit.__wrapped, true); server = http.createServer((request, response) => { response.end('Test Server Response'); }); @@ -76,6 +78,8 @@ describe('HttpPlugin', () => { (tracer.startSpan as sinon.SinonSpy).called, false ); + + assert.strictEqual(http.Server.prototype.emit.__wrapped, undefined); assert.strictEqual((tracer.withSpan as sinon.SinonSpy).called, false); }); }); diff --git a/packages/opentelemetry-plugin-http/test/functionals/http-enable.test.ts b/packages/opentelemetry-plugin-http/test/functionals/http-enable.test.ts index 7a6d0879039..fa435843bfc 100644 --- a/packages/opentelemetry-plugin-http/test/functionals/http-enable.test.ts +++ b/packages/opentelemetry-plugin-http/test/functionals/http-enable.test.ts @@ -28,8 +28,8 @@ import { InMemorySpanExporter, SimpleSpanProcessor, } from '@opentelemetry/basic-tracer'; -import { HttpPluginConfig } from '../../src'; import { Utils } from '../../src/utils'; +import { HttpPluginConfig, Http } from '../../src/types'; let server: http.Server; const serverPort = 22345; @@ -82,18 +82,20 @@ describe('HttpPlugin', () => { }); before(() => { - const ignoreConfig = [ - `http://${hostname}/ignored/string`, - /\/ignored\/regexp$/i, - (url: string) => url.endsWith(`/ignored/function`), - ]; const config: HttpPluginConfig = { - ignoreIncomingPaths: ignoreConfig, - ignoreOutgoingUrls: ignoreConfig, + ignoreIncomingPaths: [ + `/ignored/string`, + /\/ignored\/regexp$/i, + (url: string) => url.endsWith(`/ignored/function`), + ], + ignoreOutgoingUrls: [ + `http://${hostname}:${serverPort}/ignored/string`, + /\/ignored\/regexp$/i, + (url: string) => url.endsWith(`/ignored/function`), + ], applyCustomAttributesOnSpan: customAttributeFunction, }; plugin.enable(http, tracer, tracer.logger, config); - server = http.createServer((request, response) => { response.end('Test Server Response'); }); @@ -106,6 +108,18 @@ describe('HttpPlugin', () => { plugin.disable(); }); + it('http module should be patched', () => { + assert.strictEqual(http.Server.prototype.emit.__wrapped, true); + }); + + it("should not patch if it's not a http module", () => { + const httpNotPatched = new HttpPlugin( + HttpPlugin.component, + process.versions.node + ).enable({} as Http, tracer, tracer.logger, {}); + assert.strictEqual(Object.keys(httpNotPatched).length, 0); + }); + it('should generate valid spans (client side and server side)', async () => { const result = await httpRequest.get( `http://${hostname}:${serverPort}${pathname}` @@ -142,7 +156,7 @@ describe('HttpPlugin', () => { assert.strictEqual(spans.length, 0); }); - const httpErrorCodes = [400, 401, 403, 404, 429, 501, 503, 504, 500]; + const httpErrorCodes = [400, 401, 403, 404, 429, 501, 503, 504, 500, 505]; for (let i = 0; i < httpErrorCodes.length; i++) { it(`should test span for GET requests with http error ${httpErrorCodes[i]}`, async () => { @@ -283,14 +297,14 @@ describe('HttpPlugin', () => { }); for (const ignored of ['string', 'function', 'regexp']) { - it(`should not trace ignored requests with type ${ignored}`, async () => { + it(`should not trace ignored requests (client and server side) with type ${ignored}`, async () => { const testPath = `/ignored/${ignored}`; - doNock(hostname, testPath, 200, 'Ok'); + await httpRequest.get( + `${protocol}://${hostname}:${serverPort}${testPath}` + ); const spans = memoryExporter.getFinishedSpans(); assert.strictEqual(spans.length, 0); - await httpRequest.get(`${protocol}://${hostname}${testPath}`); - assert.strictEqual(spans.length, 0); }); } diff --git a/packages/opentelemetry-plugin-http/test/functionals/utils.test.ts b/packages/opentelemetry-plugin-http/test/functionals/utils.test.ts index 5a9972c37d7..1ac1b79735a 100644 --- a/packages/opentelemetry-plugin-http/test/functionals/utils.test.ts +++ b/packages/opentelemetry-plugin-http/test/functionals/utils.test.ts @@ -73,11 +73,19 @@ describe('Utils', () => { describe('getRequestInfo()', () => { it('should get options object', () => { - const result = Utils.getRequestInfo('http://google.fr/'); - assert.strictEqual(result.optionsParsed.hostname, 'google.fr'); - assert.strictEqual(result.optionsParsed.protocol, 'http:'); - assert.strictEqual(result.optionsParsed.path, '/'); - assert.strictEqual(result.pathname, '/'); + const webUrl = 'http://google.fr/'; + const urlParsed = url.parse(webUrl); + const urlParsedWithoutPathname = { + ...urlParsed, + pathname: undefined, + }; + for (const param of [webUrl, urlParsed, urlParsedWithoutPathname]) { + const result = Utils.getRequestInfo(param); + assert.strictEqual(result.optionsParsed.hostname, 'google.fr'); + assert.strictEqual(result.optionsParsed.protocol, 'http:'); + assert.strictEqual(result.optionsParsed.path, '/'); + assert.strictEqual(result.pathname, '/'); + } }); }); @@ -212,22 +220,25 @@ describe('Utils', () => { describe('setSpanWithError()', () => { it('should have error attributes', () => { - const span = new Span( - new BasicTracer({ - scopeManager: new NoopScopeManager(), - }), - 'test', - { spanId: '', traceId: '' }, - SpanKind.INTERNAL - ); const errorMessage = 'test error'; - Utils.setSpanWithError(span, new Error(errorMessage)); - const attributes = span.toReadableSpan().attributes; - assert.strictEqual( - attributes[AttributeNames.HTTP_ERROR_MESSAGE], - errorMessage - ); - assert.ok(attributes[AttributeNames.HTTP_ERROR_NAME]); + for (const obj of [undefined, { statusCode: 400 }]) { + const span = new Span( + new BasicTracer({ + scopeManager: new NoopScopeManager(), + }), + 'test', + { spanId: '', traceId: '' }, + SpanKind.INTERNAL + ); + /* tslint:disable-next-line:no-any */ + Utils.setSpanWithError(span, new Error(errorMessage), obj as any); + const attributes = span.toReadableSpan().attributes; + assert.strictEqual( + attributes[AttributeNames.HTTP_ERROR_MESSAGE], + errorMessage + ); + assert.ok(attributes[AttributeNames.HTTP_ERROR_NAME]); + } }); }); describe('isOpenTelemetryRequest()', () => {