From 97e5d64b49ed81773d81729390314dfab37150ba Mon Sep 17 00:00:00 2001 From: legendecas Date: Tue, 4 Jan 2022 16:26:04 +0800 Subject: [PATCH 1/5] feat(instrumentation-http): add options to ignore requests with hooks --- .../README.md | 2 + .../src/http.ts | 14 ++++- .../src/types.ts | 14 ++++- .../src/utils.ts | 16 +++--- .../test/functionals/http-enable.test.ts | 53 +++++++++++++++++-- .../test/functionals/https-enable.test.ts | 49 ++++++++++++++++- .../test/functionals/utils.test.ts | 4 +- 7 files changed, 133 insertions(+), 19 deletions(-) diff --git a/experimental/packages/opentelemetry-instrumentation-http/README.md b/experimental/packages/opentelemetry-instrumentation-http/README.md index 340d658b241..85579bcac15 100644 --- a/experimental/packages/opentelemetry-instrumentation-http/README.md +++ b/experimental/packages/opentelemetry-instrumentation-http/README.md @@ -51,7 +51,9 @@ Http instrumentation has few options available to choose from. You can set the f | [`startIncomingSpanHook`](https://github.com/open-telemetry/opentelemetry-js/blob/main/experimental/packages/opentelemetry-instrumentation-http/src/types.ts#L97) | `StartIncomingSpanCustomAttributeFunction` | Function for adding custom attributes before a span is started in incomingRequest | | [`startOutgoingSpanHook`](https://github.com/open-telemetry/opentelemetry-js/blob/main/experimental/packages/opentelemetry-instrumentation-http/src/types.ts#L99) | `StartOutgoingSpanCustomAttributeFunction` | Function for adding custom attributes before a span is started in outgoingRequest | | [`ignoreIncomingPaths`](https://github.com/open-telemetry/opentelemetry-js/blob/main/experimental/packages/opentelemetry-instrumentation-http/src/types.ts#L87) | `IgnoreMatcher[]` | Http instrumentation will not trace all incoming requests that match paths | +| `ignoreIncomingRequestHook` | `IgnoreIncomingRequestFunction[]` | Http instrumentation will not trace all incoming requests that matched with custom function | | [`ignoreOutgoingUrls`](https://github.com/open-telemetry/opentelemetry-js/blob/main/experimental/packages/opentelemetry-instrumentation-http/src/types.ts#L89) | `IgnoreMatcher[]` | Http instrumentation will not trace all outgoing requests that match urls | +| `ignoreOutgoingRequestHook` | `IgnoreOutgoingRequestFunction[]` | Http instrumentation will not trace all outgoing requests that matched with custom function | | [`serverName`](https://github.com/open-telemetry/opentelemetry-js/blob/main/experimental/packages/opentelemetry-instrumentation-http/src/types.ts#L101) | `string` | The primary server name of the matched virtual host. | | [`requireParentforOutgoingSpans`](https://github.com/open-telemetry/opentelemetry-js/blob/main/experimental/packages/opentelemetry-instrumentation-http/src/types.ts#L103) | Boolean | Require that is a parent span to create new span for outgoing requests. | | [`requireParentforIncomingSpans`](https://github.com/open-telemetry/opentelemetry-js/blob/main/experimental/packages/opentelemetry-instrumentation-http/src/types.ts#L105) | Boolean | Require that is a parent span to create new span for incoming requests. | diff --git a/experimental/packages/opentelemetry-instrumentation-http/src/http.ts b/experimental/packages/opentelemetry-instrumentation-http/src/http.ts index e183d5e5e65..23477b5340e 100644 --- a/experimental/packages/opentelemetry-instrumentation-http/src/http.ts +++ b/experimental/packages/opentelemetry-instrumentation-http/src/http.ts @@ -382,7 +382,12 @@ export class HttpInstrumentation extends InstrumentationBase { utils.isIgnored( pathname, instrumentation._getConfig().ignoreIncomingPaths, - (e: Error) => instrumentation._diag.error('caught ignoreIncomingPaths error: ', e) + (e: unknown) => instrumentation._diag.error('caught ignoreIncomingPaths error: ', e) + ) || + safeExecuteInTheMiddle( + () => instrumentation._getConfig().ignoreIncomingRequestHook?.(request), + () => {}, + true ) ) { return context.with(suppressTracing(context.active()), () => { @@ -534,7 +539,12 @@ export class HttpInstrumentation extends InstrumentationBase { utils.isIgnored( origin + pathname, instrumentation._getConfig().ignoreOutgoingUrls, - (e: Error) => instrumentation._diag.error('caught ignoreOutgoingUrls error: ', e) + (e: unknown) => instrumentation._diag.error('caught ignoreOutgoingUrls error: ', e) + ) || + safeExecuteInTheMiddle( + () => instrumentation._getConfig().ignoreOutgoingRequestHook?.(optionsParsed), + () => {}, + true ) ) { return original.apply(this, [optionsParsed, ...args]); diff --git a/experimental/packages/opentelemetry-instrumentation-http/src/types.ts b/experimental/packages/opentelemetry-instrumentation-http/src/types.ts index 79f4e844cd2..fdb26147fce 100644 --- a/experimental/packages/opentelemetry-instrumentation-http/src/types.ts +++ b/experimental/packages/opentelemetry-instrumentation-http/src/types.ts @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import { +import { Span, SpanAttributes, } from '@opentelemetry/api'; @@ -63,6 +63,14 @@ export interface HttpCustomAttributeFunction { ): void; } +export interface IgnoreIncomingRequestFunction { + (request: IncomingMessage ): boolean; +} + +export interface IgnoreOutgoingRequestFunction { + (request: RequestOptions ): boolean; +} + export interface HttpRequestCustomAttributeFunction { (span: Span, request: ClientRequest | IncomingMessage): void; } @@ -85,8 +93,12 @@ export interface StartOutgoingSpanCustomAttributeFunction { export interface HttpInstrumentationConfig extends InstrumentationConfig { /** Not trace all incoming requests that match paths */ ignoreIncomingPaths?: IgnoreMatcher[]; + /** Not trace all incoming requests that matched with custom function */ + ignoreIncomingRequestHook?: IgnoreIncomingRequestFunction; /** Not trace all outgoing requests that match urls */ ignoreOutgoingUrls?: IgnoreMatcher[]; + /** Not trace all outgoing requests that matched with custom function */ + ignoreOutgoingRequestHook?: IgnoreOutgoingRequestFunction; /** Function for adding custom attributes after response is handled */ applyCustomAttributesOnSpan?: HttpCustomAttributeFunction; /** Function for adding custom attributes before request is handled */ diff --git a/experimental/packages/opentelemetry-instrumentation-http/src/utils.ts b/experimental/packages/opentelemetry-instrumentation-http/src/utils.ts index 4f8a03c7af8..e1393b242f4 100644 --- a/experimental/packages/opentelemetry-instrumentation-http/src/utils.ts +++ b/experimental/packages/opentelemetry-instrumentation-http/src/utils.ts @@ -129,7 +129,7 @@ export const satisfiesPattern = ( export const isIgnored = ( constant: string, list?: IgnoreMatcher[], - onException?: (error: Error) => void + onException?: (error: unknown) => void ): boolean => { if (!list) { // No ignored urls - trace everything @@ -304,18 +304,18 @@ export const getRequestInfo = ( }`; } - if (hasExpectHeader(optionsParsed)) { - optionsParsed.headers = Object.assign({}, optionsParsed.headers); - } else if (!optionsParsed.headers) { - optionsParsed.headers = {}; - } + const headers = optionsParsed.headers ?? {}; + optionsParsed.headers = Object.keys(headers).reduce((normalizedHeader, key) => { + normalizedHeader[key.toLowerCase()] = headers[key]; + return normalizedHeader; + }, {} as OutgoingHttpHeaders); // some packages return method in lowercase.. // ensure upperCase for consistency const method = optionsParsed.method ? optionsParsed.method.toUpperCase() : 'GET'; - return { origin, pathname, method, optionsParsed }; + return { origin, pathname, method, optionsParsed, }; }; /** @@ -501,7 +501,7 @@ export function headerCapture(type: 'request' | 'response', headers: string[]) { return (span: Span, getHeader: (key: string) => undefined | string | string[] | number) => { for (const [capturedHeader, normalizedHeader] of normalizedHeaders) { const value = getHeader(capturedHeader); - + if (value === undefined) { continue; } diff --git a/experimental/packages/opentelemetry-instrumentation-http/test/functionals/http-enable.test.ts b/experimental/packages/opentelemetry-instrumentation-http/test/functionals/http-enable.test.ts index 72c1ca5a1f7..38c549b7e39 100644 --- a/experimental/packages/opentelemetry-instrumentation-http/test/functionals/http-enable.test.ts +++ b/experimental/packages/opentelemetry-instrumentation-http/test/functionals/http-enable.test.ts @@ -18,7 +18,7 @@ import { context, propagation, Span as ISpan, - SpanKind, + SpanKind, trace, SpanAttributes, } from '@opentelemetry/api'; @@ -142,11 +142,17 @@ describe('HttpInstrumentation', () => { throw new Error('bad ignoreIncomingPaths function'); }, ], + ignoreIncomingRequestHook: _request => { + throw new Error('bad ignoreIncomingRequestHook function'); + }, ignoreOutgoingUrls: [ (url: string) => { throw new Error('bad ignoreOutgoingUrls function'); }, ], + ignoreOutgoingRequestHook: _request => { + throw new Error('bad ignoreOutgoingRequestHook function'); + }, applyCustomAttributesOnSpan: () => { throw new Error(applyCustomAttributesOnSpanErrorMessage); }, @@ -167,7 +173,12 @@ describe('HttpInstrumentation', () => { it('should generate valid spans (client side and server side)', async () => { const result = await httpRequest.get( - `${protocol}://${hostname}:${serverPort}${pathname}` + `${protocol}://${hostname}:${serverPort}${pathname}`, + { + headers: { + 'user-agent': 'tester' + } + } ); const spans = memoryExporter.getFinishedSpans(); const [incomingSpan, outgoingSpan] = spans; @@ -207,11 +218,20 @@ describe('HttpInstrumentation', () => { /\/ignored\/regexp$/i, (url: string) => url.endsWith('/ignored/function'), ], + ignoreIncomingRequestHook: request => { + return request.headers['user-agent']?.match('ignored-string') != null; + }, ignoreOutgoingUrls: [ `${protocol}://${hostname}:${serverPort}/ignored/string`, /\/ignored\/regexp$/i, (url: string) => url.endsWith('/ignored/function'), ], + ignoreOutgoingRequestHook: request => { + if (request.headers?.['user-agent'] != null) { + return `${request.headers['user-agent']}`.match('ignored-string') != null; + } + return false; + }, applyCustomAttributesOnSpan: customAttributeFunction, requestHook: requestHookFunction, responseHook: responseHookFunction, @@ -447,7 +467,7 @@ describe('HttpInstrumentation', () => { }); for (const ignored of ['string', 'function', 'regexp']) { - it(`should not trace ignored requests (client and server side) with type ${ignored}`, async () => { + it(`should not trace ignored requests with paths (client and server side) with type ${ignored}`, async () => { const testPath = `/ignored/${ignored}`; await httpRequest.get( @@ -458,6 +478,31 @@ describe('HttpInstrumentation', () => { }); } + it('should not trace ignored requests with headers (client and server side)', async () => { + const testValue = 'ignored-string'; + + await Promise.all([ + httpRequest.get( + `${protocol}://${hostname}:${serverPort}`, + { + headers: { + 'user-agent': testValue + } + } + ), + httpRequest.get( + `${protocol}://${hostname}:${serverPort}`, + { + headers: { + 'uSeR-aGeNt': testValue + } + } + ) + ]); + const spans = memoryExporter.getFinishedSpans(); + assert.strictEqual(spans.length, 0); + }); + for (const arg of ['string', {}, new Date()]) { it(`should be tracable and not throw exception in ${protocol} instrumentation when passing the following argument ${JSON.stringify( arg @@ -507,7 +552,7 @@ describe('HttpInstrumentation', () => { hostname: 'localhost', pathname: '/', forceStatus: { - code: SpanStatusCode.ERROR, + code: SpanStatusCode.ERROR, message: err.message, }, component: 'http', diff --git a/experimental/packages/opentelemetry-instrumentation-http/test/functionals/https-enable.test.ts b/experimental/packages/opentelemetry-instrumentation-http/test/functionals/https-enable.test.ts index 46bf516e136..2611642b4c0 100644 --- a/experimental/packages/opentelemetry-instrumentation-http/test/functionals/https-enable.test.ts +++ b/experimental/packages/opentelemetry-instrumentation-http/test/functionals/https-enable.test.ts @@ -112,11 +112,17 @@ describe('HttpsInstrumentation', () => { throw new Error('bad ignoreIncomingPaths function'); }, ], + ignoreIncomingRequestHook: _request => { + throw new Error('bad ignoreIncomingRequestHook function'); + }, ignoreOutgoingUrls: [ (url: string) => { throw new Error('bad ignoreOutgoingUrls function'); }, ], + ignoreOutgoingRequestHook: _request => { + throw new Error('bad ignoreOutgoingRequestHook function'); + }, applyCustomAttributesOnSpan: () => { throw new Error(applyCustomAttributesOnSpanErrorMessage); }, @@ -142,7 +148,12 @@ describe('HttpsInstrumentation', () => { it('should generate valid spans (client side and server side)', async () => { const result = await httpsRequest.get( - `${protocol}://${hostname}:${serverPort}${pathname}` + `${protocol}://${hostname}:${serverPort}${pathname}`, + { + headers: { + 'user-agent': 'tester' + } + } ); const spans = memoryExporter.getFinishedSpans(); const [incomingSpan, outgoingSpan] = spans; @@ -181,11 +192,20 @@ describe('HttpsInstrumentation', () => { /\/ignored\/regexp$/i, (url: string) => url.endsWith('/ignored/function'), ], + ignoreIncomingRequestHook: request => { + return request.headers['user-agent']?.match('ignored-string') != null; + }, ignoreOutgoingUrls: [ `${protocol}://${hostname}:${serverPort}/ignored/string`, /\/ignored\/regexp$/i, (url: string) => url.endsWith('/ignored/function'), ], + ignoreOutgoingRequestHook: request => { + if (request.headers?.['user-agent'] != null) { + return `${request.headers['user-agent']}`.match('ignored-string') != null; + } + return false; + }, applyCustomAttributesOnSpan: customAttributeFunction, serverName, }); @@ -412,7 +432,7 @@ describe('HttpsInstrumentation', () => { }); for (const ignored of ['string', 'function', 'regexp']) { - it(`should not trace ignored requests (client and server side) with type ${ignored}`, async () => { + it(`should not trace ignored requests with paths (client and server side) with type ${ignored}`, async () => { const testPath = `/ignored/${ignored}`; await httpsRequest.get( @@ -423,6 +443,31 @@ describe('HttpsInstrumentation', () => { }); } + it('should not trace ignored requests with headers (client and server side)', async () => { + const testValue = 'ignored-string'; + + await Promise.all([ + httpsRequest.get( + `${protocol}://${hostname}:${serverPort}`, + { + headers: { + 'user-agent': testValue + } + } + ), + httpsRequest.get( + `${protocol}://${hostname}:${serverPort}`, + { + headers: { + 'uSeR-aGeNt': testValue + } + } + ) + ]); + const spans = memoryExporter.getFinishedSpans(); + assert.strictEqual(spans.length, 0); + }); + for (const arg of ['string', {}, new Date()]) { it(`should be tracable and not throw exception in ${protocol} instrumentation when passing the following argument ${JSON.stringify( arg diff --git a/experimental/packages/opentelemetry-instrumentation-http/test/functionals/utils.test.ts b/experimental/packages/opentelemetry-instrumentation-http/test/functionals/utils.test.ts index 7b8a2e8d1ef..35e68f4211c 100644 --- a/experimental/packages/opentelemetry-instrumentation-http/test/functionals/utils.test.ts +++ b/experimental/packages/opentelemetry-instrumentation-http/test/functionals/utils.test.ts @@ -170,7 +170,7 @@ describe('Utility', () => { }); it('should not re-throw when function throws an exception', () => { - const onException = (e: Error) => { + const onException = (e: unknown) => { // Do nothing }; for (const callback of [undefined, onException]) { @@ -480,7 +480,7 @@ describe('Utility', () => { assert.strictEqual(attributes[SemanticAttributes.HTTP_ROUTE], undefined) }); }); - + describe('headers to span attributes capture', () => { let span: Span; From d3ef95f30f1e7412363df3e229bae632c0c2c674 Mon Sep 17 00:00:00 2001 From: legendecas Date: Tue, 11 Jan 2022 10:56:40 +0800 Subject: [PATCH 2/5] fixup! --- .../README.md | 4 ++-- .../src/http.ts | 12 ++++++++-- .../src/utils.ts | 13 ----------- .../test/functionals/http-enable.test.ts | 15 ++++++++++++- .../test/functionals/utils.test.ts | 22 ------------------- 5 files changed, 26 insertions(+), 40 deletions(-) diff --git a/experimental/packages/opentelemetry-instrumentation-http/README.md b/experimental/packages/opentelemetry-instrumentation-http/README.md index 85579bcac15..0fe25d3368d 100644 --- a/experimental/packages/opentelemetry-instrumentation-http/README.md +++ b/experimental/packages/opentelemetry-instrumentation-http/README.md @@ -51,9 +51,9 @@ Http instrumentation has few options available to choose from. You can set the f | [`startIncomingSpanHook`](https://github.com/open-telemetry/opentelemetry-js/blob/main/experimental/packages/opentelemetry-instrumentation-http/src/types.ts#L97) | `StartIncomingSpanCustomAttributeFunction` | Function for adding custom attributes before a span is started in incomingRequest | | [`startOutgoingSpanHook`](https://github.com/open-telemetry/opentelemetry-js/blob/main/experimental/packages/opentelemetry-instrumentation-http/src/types.ts#L99) | `StartOutgoingSpanCustomAttributeFunction` | Function for adding custom attributes before a span is started in outgoingRequest | | [`ignoreIncomingPaths`](https://github.com/open-telemetry/opentelemetry-js/blob/main/experimental/packages/opentelemetry-instrumentation-http/src/types.ts#L87) | `IgnoreMatcher[]` | Http instrumentation will not trace all incoming requests that match paths | -| `ignoreIncomingRequestHook` | `IgnoreIncomingRequestFunction[]` | Http instrumentation will not trace all incoming requests that matched with custom function | +| `ignoreIncomingRequestHook` | `IgnoreIncomingRequestFunction` | Http instrumentation will not trace all incoming requests that matched with custom function | | [`ignoreOutgoingUrls`](https://github.com/open-telemetry/opentelemetry-js/blob/main/experimental/packages/opentelemetry-instrumentation-http/src/types.ts#L89) | `IgnoreMatcher[]` | Http instrumentation will not trace all outgoing requests that match urls | -| `ignoreOutgoingRequestHook` | `IgnoreOutgoingRequestFunction[]` | Http instrumentation will not trace all outgoing requests that matched with custom function | +| `ignoreOutgoingRequestHook` | `IgnoreOutgoingRequestFunction` | Http instrumentation will not trace all outgoing requests that matched with custom function | | [`serverName`](https://github.com/open-telemetry/opentelemetry-js/blob/main/experimental/packages/opentelemetry-instrumentation-http/src/types.ts#L101) | `string` | The primary server name of the matched virtual host. | | [`requireParentforOutgoingSpans`](https://github.com/open-telemetry/opentelemetry-js/blob/main/experimental/packages/opentelemetry-instrumentation-http/src/types.ts#L103) | Boolean | Require that is a parent span to create new span for outgoing requests. | | [`requireParentforIncomingSpans`](https://github.com/open-telemetry/opentelemetry-js/blob/main/experimental/packages/opentelemetry-instrumentation-http/src/types.ts#L105) | Boolean | Require that is a parent span to create new span for incoming requests. | diff --git a/experimental/packages/opentelemetry-instrumentation-http/src/http.ts b/experimental/packages/opentelemetry-instrumentation-http/src/http.ts index 23477b5340e..5ca612a8340 100644 --- a/experimental/packages/opentelemetry-instrumentation-http/src/http.ts +++ b/experimental/packages/opentelemetry-instrumentation-http/src/http.ts @@ -386,7 +386,11 @@ export class HttpInstrumentation extends InstrumentationBase { ) || safeExecuteInTheMiddle( () => instrumentation._getConfig().ignoreIncomingRequestHook?.(request), - () => {}, + (e: unknown) => { + if (e != null) { + instrumentation._diag.error('caught ignoreIncomingRequestHook error: ', e) + } + }, true ) ) { @@ -543,7 +547,11 @@ export class HttpInstrumentation extends InstrumentationBase { ) || safeExecuteInTheMiddle( () => instrumentation._getConfig().ignoreOutgoingRequestHook?.(optionsParsed), - () => {}, + (e: unknown) => { + if (e != null) { + instrumentation._diag.error('caught ignoreOutgoingRequestHook error: ', e) + } + }, true ) ) { diff --git a/experimental/packages/opentelemetry-instrumentation-http/src/utils.ts b/experimental/packages/opentelemetry-instrumentation-http/src/utils.ts index e1393b242f4..0a102ae6888 100644 --- a/experimental/packages/opentelemetry-instrumentation-http/src/utils.ts +++ b/experimental/packages/opentelemetry-instrumentation-http/src/utils.ts @@ -85,19 +85,6 @@ export const parseResponseStatus = ( return { code: SpanStatusCode.ERROR }; }; -/** - * Returns whether the Expect header is on the given options object. - * @param options Options for http.request. - */ -export const hasExpectHeader = (options: RequestOptions): boolean => { - if (!options.headers) { - return false; - } - - const keys = Object.keys(options.headers); - return !!keys.find(key => key.toLowerCase() === 'expect'); -}; - /** * Check whether the given obj match pattern * @param constant e.g URL of request diff --git a/experimental/packages/opentelemetry-instrumentation-http/test/functionals/http-enable.test.ts b/experimental/packages/opentelemetry-instrumentation-http/test/functionals/http-enable.test.ts index 38c549b7e39..05a6791e82d 100644 --- a/experimental/packages/opentelemetry-instrumentation-http/test/functionals/http-enable.test.ts +++ b/experimental/packages/opentelemetry-instrumentation-http/test/functionals/http-enable.test.ts @@ -497,12 +497,25 @@ describe('HttpInstrumentation', () => { 'uSeR-aGeNt': testValue } } - ) + ), ]); const spans = memoryExporter.getFinishedSpans(); assert.strictEqual(spans.length, 0); }); + it('should trace not ignored requests with headers (client and server side)', async () => { + await httpRequest.get( + `${protocol}://${hostname}:${serverPort}`, + { + headers: { + 'user-agent': 'test-bot', + } + } + ); + const spans = memoryExporter.getFinishedSpans(); + assert.strictEqual(spans.length, 2); + }); + for (const arg of ['string', {}, new Date()]) { it(`should be tracable and not throw exception in ${protocol} instrumentation when passing the following argument ${JSON.stringify( arg diff --git a/experimental/packages/opentelemetry-instrumentation-http/test/functionals/utils.test.ts b/experimental/packages/opentelemetry-instrumentation-http/test/functionals/utils.test.ts index 35e68f4211c..cea71fd1b4d 100644 --- a/experimental/packages/opentelemetry-instrumentation-http/test/functionals/utils.test.ts +++ b/experimental/packages/opentelemetry-instrumentation-http/test/functionals/utils.test.ts @@ -58,28 +58,6 @@ describe('Utility', () => { } }); }); - describe('hasExpectHeader()', () => { - it('should throw if no option', () => { - try { - utils.hasExpectHeader('' as http.RequestOptions); - assert.fail(); - } catch (ignore) {} - }); - - it('should not throw if no headers', () => { - const result = utils.hasExpectHeader({} as http.RequestOptions); - assert.strictEqual(result, false); - }); - - it('should return true on Expect (no case sensitive)', () => { - for (const headers of [{ Expect: 1 }, { expect: 1 }, { ExPect: 1 }]) { - const result = utils.hasExpectHeader({ - headers, - } as http.RequestOptions); - assert.strictEqual(result, true); - } - }); - }); describe('getRequestInfo()', () => { it('should get options object', () => { From 852c7b54f46edd43a7ccd366b67b81f322128548 Mon Sep 17 00:00:00 2001 From: legendecas Date: Tue, 11 Jan 2022 11:06:01 +0800 Subject: [PATCH 3/5] fixup! --- .../test/functionals/utils.test.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/experimental/packages/opentelemetry-instrumentation-http/test/functionals/utils.test.ts b/experimental/packages/opentelemetry-instrumentation-http/test/functionals/utils.test.ts index cea71fd1b4d..7f07b36ac47 100644 --- a/experimental/packages/opentelemetry-instrumentation-http/test/functionals/utils.test.ts +++ b/experimental/packages/opentelemetry-instrumentation-http/test/functionals/utils.test.ts @@ -24,7 +24,6 @@ import { import { BasicTracerProvider, Span } from '@opentelemetry/sdk-trace-base'; import { SemanticAttributes } from '@opentelemetry/semantic-conventions'; import * as assert from 'assert'; -import * as http from 'http'; import { IncomingMessage, ServerResponse } from 'http'; import { Socket } from 'net'; import * as sinon from 'sinon'; From 5806d691dd4d7aefb35292b552187ad57fc64be7 Mon Sep 17 00:00:00 2001 From: legendecas Date: Mon, 17 Jan 2022 11:02:38 +0800 Subject: [PATCH 4/5] fixup! linter autofix --- .../packages/opentelemetry-instrumentation-http/src/http.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/experimental/packages/opentelemetry-instrumentation-http/src/http.ts b/experimental/packages/opentelemetry-instrumentation-http/src/http.ts index f174d057f9e..b031cb57fc1 100644 --- a/experimental/packages/opentelemetry-instrumentation-http/src/http.ts +++ b/experimental/packages/opentelemetry-instrumentation-http/src/http.ts @@ -388,7 +388,7 @@ export class HttpInstrumentation extends InstrumentationBase { () => instrumentation._getConfig().ignoreIncomingRequestHook?.(request), (e: unknown) => { if (e != null) { - instrumentation._diag.error('caught ignoreIncomingRequestHook error: ', e) + instrumentation._diag.error('caught ignoreIncomingRequestHook error: ', e); } }, true @@ -549,7 +549,7 @@ export class HttpInstrumentation extends InstrumentationBase { () => instrumentation._getConfig().ignoreOutgoingRequestHook?.(optionsParsed), (e: unknown) => { if (e != null) { - instrumentation._diag.error('caught ignoreOutgoingRequestHook error: ', e) + instrumentation._diag.error('caught ignoreOutgoingRequestHook error: ', e); } }, true From d2ffa60e3c8dacdc9315815d0b8395d9fcadd5cd Mon Sep 17 00:00:00 2001 From: legendecas Date: Mon, 17 Jan 2022 11:08:21 +0800 Subject: [PATCH 5/5] fixup! docs: deprecate ignore matchers --- .../opentelemetry-instrumentation-http/README.md | 9 +++++++-- .../opentelemetry-instrumentation-http/src/types.ts | 10 ++++++++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/experimental/packages/opentelemetry-instrumentation-http/README.md b/experimental/packages/opentelemetry-instrumentation-http/README.md index 0fe25d3368d..4684adaa4ce 100644 --- a/experimental/packages/opentelemetry-instrumentation-http/README.md +++ b/experimental/packages/opentelemetry-instrumentation-http/README.md @@ -50,15 +50,20 @@ Http instrumentation has few options available to choose from. You can set the f | [`responseHook`](https://github.com/open-telemetry/opentelemetry-js/blob/main/experimental/packages/opentelemetry-instrumentation-http/src/types.ts#L95) | `HttpResponseCustomAttributeFunction` | Function for adding custom attributes before response is handled | | [`startIncomingSpanHook`](https://github.com/open-telemetry/opentelemetry-js/blob/main/experimental/packages/opentelemetry-instrumentation-http/src/types.ts#L97) | `StartIncomingSpanCustomAttributeFunction` | Function for adding custom attributes before a span is started in incomingRequest | | [`startOutgoingSpanHook`](https://github.com/open-telemetry/opentelemetry-js/blob/main/experimental/packages/opentelemetry-instrumentation-http/src/types.ts#L99) | `StartOutgoingSpanCustomAttributeFunction` | Function for adding custom attributes before a span is started in outgoingRequest | -| [`ignoreIncomingPaths`](https://github.com/open-telemetry/opentelemetry-js/blob/main/experimental/packages/opentelemetry-instrumentation-http/src/types.ts#L87) | `IgnoreMatcher[]` | Http instrumentation will not trace all incoming requests that match paths | | `ignoreIncomingRequestHook` | `IgnoreIncomingRequestFunction` | Http instrumentation will not trace all incoming requests that matched with custom function | -| [`ignoreOutgoingUrls`](https://github.com/open-telemetry/opentelemetry-js/blob/main/experimental/packages/opentelemetry-instrumentation-http/src/types.ts#L89) | `IgnoreMatcher[]` | Http instrumentation will not trace all outgoing requests that match urls | | `ignoreOutgoingRequestHook` | `IgnoreOutgoingRequestFunction` | Http instrumentation will not trace all outgoing requests that matched with custom function | | [`serverName`](https://github.com/open-telemetry/opentelemetry-js/blob/main/experimental/packages/opentelemetry-instrumentation-http/src/types.ts#L101) | `string` | The primary server name of the matched virtual host. | | [`requireParentforOutgoingSpans`](https://github.com/open-telemetry/opentelemetry-js/blob/main/experimental/packages/opentelemetry-instrumentation-http/src/types.ts#L103) | Boolean | Require that is a parent span to create new span for outgoing requests. | | [`requireParentforIncomingSpans`](https://github.com/open-telemetry/opentelemetry-js/blob/main/experimental/packages/opentelemetry-instrumentation-http/src/types.ts#L105) | Boolean | Require that is a parent span to create new span for incoming requests. | | [`headersToSpanAttributes`](https://github.com/open-telemetry/opentelemetry-js/blob/main/experimental/packages/opentelemetry-instrumentation-http/src/types.ts#L107) | `object` | List of case insensitive HTTP headers to convert to span attributes. Client (outgoing requests, incoming responses) and server (incoming requests, outgoing responses) headers will be converted to span attributes in the form of `http.{request\|response}.header.header_name`, e.g. `http.response.header.content_length` | +The following options are deprecated: + +| Options | Type | Description | +| ------- | ---- | ----------- | +| `ignoreIncomingPaths` | `IgnoreMatcher[]` | Http instrumentation will not trace all incoming requests that match paths | +| `ignoreOutgoingUrls` | `IgnoreMatcher[]` | Http instrumentation will not trace all outgoing requests that match urls | + ## Useful links - For more information on OpenTelemetry, visit: diff --git a/experimental/packages/opentelemetry-instrumentation-http/src/types.ts b/experimental/packages/opentelemetry-instrumentation-http/src/types.ts index fdb26147fce..6741d0a042d 100644 --- a/experimental/packages/opentelemetry-instrumentation-http/src/types.ts +++ b/experimental/packages/opentelemetry-instrumentation-http/src/types.ts @@ -91,11 +91,17 @@ export interface StartOutgoingSpanCustomAttributeFunction { * Options available for the HTTP instrumentation (see [documentation](https://github.com/open-telemetry/opentelemetry-js/tree/main/packages/opentelemetry-instrumentation-http#http-instrumentation-options)) */ export interface HttpInstrumentationConfig extends InstrumentationConfig { - /** Not trace all incoming requests that match paths */ + /** + * Not trace all incoming requests that match paths + * @deprecated use `ignoreIncomingRequestHook` instead + */ ignoreIncomingPaths?: IgnoreMatcher[]; /** Not trace all incoming requests that matched with custom function */ ignoreIncomingRequestHook?: IgnoreIncomingRequestFunction; - /** Not trace all outgoing requests that match urls */ + /** + * Not trace all outgoing requests that match urls + * @deprecated use `ignoreOutgoingRequestHook` instead + */ ignoreOutgoingUrls?: IgnoreMatcher[]; /** Not trace all outgoing requests that matched with custom function */ ignoreOutgoingRequestHook?: IgnoreOutgoingRequestFunction;