Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
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');
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;
}

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