diff --git a/packages/opentelemetry-instrumentation-fetch/src/fetch.ts b/packages/opentelemetry-instrumentation-fetch/src/fetch.ts index b263c110152..5a87d4b6c0e 100644 --- a/packages/opentelemetry-instrumentation-fetch/src/fetch.ts +++ b/packages/opentelemetry-instrumentation-fetch/src/fetch.ts @@ -145,6 +145,11 @@ export class FetchInstrumentation extends InstrumentationBase< this._getConfig().propagateTraceHeaderCorsUrls ) ) { + const headers: Partial> = {}; + api.propagation.inject(api.context.active(), headers); + if (Object.keys(headers).length > 0) { + api.diag.debug('headers inject skipped due to CORS policy'); + } return; } @@ -292,8 +297,8 @@ export class FetchInstrumentation extends InstrumentationBase< ): Promise { const url = input instanceof Request ? input.url : input; const options = input instanceof Request ? input : init || {}; - const span = plugin._createSpan(url, options); - if (!span) { + const createdSpan = plugin._createSpan(url, options); + if (!createdSpan) { return original.apply(this, [url, options]); } const spanData = plugin._prepareSpanData(url); @@ -338,15 +343,15 @@ export class FetchInstrumentation extends InstrumentationBase< return new Promise((resolve, reject) => { return api.context.with( - api.setSpan(api.context.active(), span), + api.setSpan(api.context.active(), createdSpan), () => { plugin._addHeaders(options, url); plugin._tasksCount++; return original .apply(this, [url, options]) .then( - (onSuccess as any).bind(this, span, resolve), - onError.bind(this, span, reject) + (onSuccess as any).bind(this, createdSpan, resolve), + onError.bind(this, createdSpan, reject) ); } ); diff --git a/packages/opentelemetry-instrumentation-fetch/test/fetch.test.ts b/packages/opentelemetry-instrumentation-fetch/test/fetch.test.ts index 9212774ca10..b6cff23c49a 100644 --- a/packages/opentelemetry-instrumentation-fetch/test/fetch.test.ts +++ b/packages/opentelemetry-instrumentation-fetch/test/fetch.test.ts @@ -537,10 +537,19 @@ describe('fetch', () => { }); describe('when propagateTraceHeaderCorsUrls does NOT MATCH', () => { + let spyDebug: sinon.SinonSpy; beforeEach(done => { + const diagLogger = new api.DiagConsoleLogger(); + spyDebug = sinon.spy(); + diagLogger.debug = spyDebug; + api.diag.setLogger(diagLogger, api.DiagLogLevel.ALL); clearData(); prepareData(done, url, {}); }); + afterEach(() => { + sinon.restore(); + }); + it('should NOT set trace headers', () => { assert.strictEqual( lastResponse.headers[X_B3_TRACE_ID], @@ -558,6 +567,12 @@ describe('fetch', () => { `trace header '${X_B3_SAMPLED}' should not be set` ); }); + it('should debug info that injecting headers was skipped', () => { + assert.strictEqual( + spyDebug.lastCall.args[0], + 'headers inject skipped due to CORS policy' + ); + }); }); }); diff --git a/packages/opentelemetry-instrumentation-xml-http-request/src/xhr.ts b/packages/opentelemetry-instrumentation-xml-http-request/src/xhr.ts index 84c0b18c83f..b65afdc6949 100644 --- a/packages/opentelemetry-instrumentation-xml-http-request/src/xhr.ts +++ b/packages/opentelemetry-instrumentation-xml-http-request/src/xhr.ts @@ -106,6 +106,11 @@ export class XMLHttpRequestInstrumentation extends InstrumentationBase> = {}; + api.propagation.inject(api.context.active(), headers); + if (Object.keys(headers).length > 0) { + api.diag.debug('headers inject skipped due to CORS policy'); + } return; } const headers: { [key: string]: unknown } = {}; diff --git a/packages/opentelemetry-instrumentation-xml-http-request/test/xhr.test.ts b/packages/opentelemetry-instrumentation-xml-http-request/test/xhr.test.ts index 689e299aa8a..8537fe8c3ad 100644 --- a/packages/opentelemetry-instrumentation-xml-http-request/test/xhr.test.ts +++ b/packages/opentelemetry-instrumentation-xml-http-request/test/xhr.test.ts @@ -549,7 +549,12 @@ describe('xhr', () => { 'AND origin does NOT match window.location And does NOT match' + ' with propagateTraceHeaderCorsUrls', () => { + let spyDebug: sinon.SinonSpy; beforeEach(done => { + const diagLogger = new api.DiagConsoleLogger(); + spyDebug = sinon.spy(); + diagLogger.debug = spyDebug; + api.diag.setLogger(diagLogger, api.DiagLogLevel.ALL); clearData(); prepareData( done, @@ -573,6 +578,13 @@ describe('xhr', () => { `trace header '${X_B3_SAMPLED}' should not be set` ); }); + + it('should debug info that injecting headers was skipped', () => { + assert.strictEqual( + spyDebug.lastCall.args[0], + 'headers inject skipped due to CORS policy' + ); + }); } );