Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: adding info to debug whenever headers are being skipped due to cors policy #2061

Merged
merged 4 commits into from
Apr 9, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
chore: adding info to debug whenever headers are being skipped due to…
… cors policy
obecny committed Mar 30, 2021
commit 8284bf2dfb3bf23b9c351481edbfebe2c19992ea
15 changes: 10 additions & 5 deletions packages/opentelemetry-instrumentation-fetch/src/fetch.ts
Original file line number Diff line number Diff line change
@@ -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');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor suggestion on the actual error content headers not injected vs headers inject skipped

And as a general diagnostic error logging (I know we have previously talked about adding a scope using a standard mechanism), but in the mean-time do we want to manually add the scope to make it easier for us and users to understand where this log cam from?

eg. (Using OT-Fetch as an example -- it probably needs a larger general discussion to define a general convention)
api.diag.debug('OT-Fetch: headers not injected due to CORS policy");

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is yet to be decided how the namespace should be constructed but not in this PR, please move the discussion to separate issue. With regards to copy it is how it was suggested in the raised issue.

}
return;
}

@@ -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);
@@ -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)
);
}
);
15 changes: 15 additions & 0 deletions packages/opentelemetry-instrumentation-fetch/test/fetch.test.ts
Original file line number Diff line number Diff line change
@@ -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'
);
});
});
});

Original file line number Diff line number Diff line change
@@ -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 } = {};
Original file line number Diff line number Diff line change
@@ -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'
);
});
}
);