Skip to content

Commit

Permalink
chore: adding info to debug whenever headers are being skipped due to…
Browse files Browse the repository at this point in the history
… cors policy (#2061)

Co-authored-by: Valentin Marchaud <contact@vmarchaud.fr>
  • Loading branch information
obecny and vmarchaud authored Apr 9, 2021
1 parent a51bbbd commit 4a468fc
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
15 changes: 10 additions & 5 deletions packages/opentelemetry-instrumentation-fetch/src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ export class FetchInstrumentation extends InstrumentationBase<
this._getConfig().propagateTraceHeaderCorsUrls
)
) {
const headers: Partial<Record<string, unknown>> = {};
api.propagation.inject(api.context.active(), headers);
if (Object.keys(headers).length > 0) {
api.diag.debug('headers inject skipped due to CORS policy');
}
return;
}

Expand Down Expand Up @@ -292,8 +297,8 @@ export class FetchInstrumentation extends InstrumentationBase<
): Promise<Response> {
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);
Expand Down Expand Up @@ -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)
);
}
);
Expand Down
15 changes: 15 additions & 0 deletions packages/opentelemetry-instrumentation-fetch/test/fetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -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'
);
});
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ export class XMLHttpRequestInstrumentation extends InstrumentationBase<XMLHttpRe
this._getConfig().propagateTraceHeaderCorsUrls
)
) {
const headers: Partial<Record<string, unknown>> = {};
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 } = {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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'
);
});
}
);

Expand Down

0 comments on commit 4a468fc

Please sign in to comment.