Skip to content

Commit 4a468fc

Browse files
obecnyvmarchaud
andauthored
chore: adding info to debug whenever headers are being skipped due to cors policy (#2061)
Co-authored-by: Valentin Marchaud <[email protected]>
1 parent a51bbbd commit 4a468fc

File tree

4 files changed

+42
-5
lines changed

4 files changed

+42
-5
lines changed

packages/opentelemetry-instrumentation-fetch/src/fetch.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,11 @@ export class FetchInstrumentation extends InstrumentationBase<
145145
this._getConfig().propagateTraceHeaderCorsUrls
146146
)
147147
) {
148+
const headers: Partial<Record<string, unknown>> = {};
149+
api.propagation.inject(api.context.active(), headers);
150+
if (Object.keys(headers).length > 0) {
151+
api.diag.debug('headers inject skipped due to CORS policy');
152+
}
148153
return;
149154
}
150155

@@ -292,8 +297,8 @@ export class FetchInstrumentation extends InstrumentationBase<
292297
): Promise<Response> {
293298
const url = input instanceof Request ? input.url : input;
294299
const options = input instanceof Request ? input : init || {};
295-
const span = plugin._createSpan(url, options);
296-
if (!span) {
300+
const createdSpan = plugin._createSpan(url, options);
301+
if (!createdSpan) {
297302
return original.apply(this, [url, options]);
298303
}
299304
const spanData = plugin._prepareSpanData(url);
@@ -338,15 +343,15 @@ export class FetchInstrumentation extends InstrumentationBase<
338343

339344
return new Promise((resolve, reject) => {
340345
return api.context.with(
341-
api.setSpan(api.context.active(), span),
346+
api.setSpan(api.context.active(), createdSpan),
342347
() => {
343348
plugin._addHeaders(options, url);
344349
plugin._tasksCount++;
345350
return original
346351
.apply(this, [url, options])
347352
.then(
348-
(onSuccess as any).bind(this, span, resolve),
349-
onError.bind(this, span, reject)
353+
(onSuccess as any).bind(this, createdSpan, resolve),
354+
onError.bind(this, createdSpan, reject)
350355
);
351356
}
352357
);

packages/opentelemetry-instrumentation-fetch/test/fetch.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,10 +537,19 @@ describe('fetch', () => {
537537
});
538538

539539
describe('when propagateTraceHeaderCorsUrls does NOT MATCH', () => {
540+
let spyDebug: sinon.SinonSpy;
540541
beforeEach(done => {
542+
const diagLogger = new api.DiagConsoleLogger();
543+
spyDebug = sinon.spy();
544+
diagLogger.debug = spyDebug;
545+
api.diag.setLogger(diagLogger, api.DiagLogLevel.ALL);
541546
clearData();
542547
prepareData(done, url, {});
543548
});
549+
afterEach(() => {
550+
sinon.restore();
551+
});
552+
544553
it('should NOT set trace headers', () => {
545554
assert.strictEqual(
546555
lastResponse.headers[X_B3_TRACE_ID],
@@ -558,6 +567,12 @@ describe('fetch', () => {
558567
`trace header '${X_B3_SAMPLED}' should not be set`
559568
);
560569
});
570+
it('should debug info that injecting headers was skipped', () => {
571+
assert.strictEqual(
572+
spyDebug.lastCall.args[0],
573+
'headers inject skipped due to CORS policy'
574+
);
575+
});
561576
});
562577
});
563578

packages/opentelemetry-instrumentation-xml-http-request/src/xhr.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@ export class XMLHttpRequestInstrumentation extends InstrumentationBase<XMLHttpRe
106106
this._getConfig().propagateTraceHeaderCorsUrls
107107
)
108108
) {
109+
const headers: Partial<Record<string, unknown>> = {};
110+
api.propagation.inject(api.context.active(), headers);
111+
if (Object.keys(headers).length > 0) {
112+
api.diag.debug('headers inject skipped due to CORS policy');
113+
}
109114
return;
110115
}
111116
const headers: { [key: string]: unknown } = {};

packages/opentelemetry-instrumentation-xml-http-request/test/xhr.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,12 @@ describe('xhr', () => {
549549
'AND origin does NOT match window.location And does NOT match' +
550550
' with propagateTraceHeaderCorsUrls',
551551
() => {
552+
let spyDebug: sinon.SinonSpy;
552553
beforeEach(done => {
554+
const diagLogger = new api.DiagConsoleLogger();
555+
spyDebug = sinon.spy();
556+
diagLogger.debug = spyDebug;
557+
api.diag.setLogger(diagLogger, api.DiagLogLevel.ALL);
553558
clearData();
554559
prepareData(
555560
done,
@@ -573,6 +578,13 @@ describe('xhr', () => {
573578
`trace header '${X_B3_SAMPLED}' should not be set`
574579
);
575580
});
581+
582+
it('should debug info that injecting headers was skipped', () => {
583+
assert.strictEqual(
584+
spyDebug.lastCall.args[0],
585+
'headers inject skipped due to CORS policy'
586+
);
587+
});
576588
}
577589
);
578590

0 commit comments

Comments
 (0)