Skip to content

Commit

Permalink
fix(fetch): pass spanResponse to _endSpan
Browse files Browse the repository at this point in the history
  • Loading branch information
Ugzuzg committed Jun 13, 2022
1 parent c8c4ec6 commit bd01667
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -323,14 +323,10 @@ export class FetchInstrumentation extends InstrumentationBase<Promise<Response>>
};
if (response.status >= 200 && response.status < 400) {
if (response.url != null && response.url !== '') {
spanResponse.url = url;
spanResponse.url = response.url;
}
}
plugin._endSpan(span, spanData, {
status: response.status,
statusText: response.statusText,
url,
});
plugin._endSpan(span, spanData, spanResponse);
}

function onSuccess(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ describe('fetch', () => {
let fetchInstrumentation: FetchInstrumentation;

const url = 'http://localhost:8090/get';
const redirectedUrl = 'http://localhost:8090/redirect';
const redirectedTo = 'http://after.redirect/get';
const badUrl = 'http://foo.bar.com/get';

const clearData = () => {
Expand Down Expand Up @@ -188,6 +190,13 @@ describe('fetch', () => {
response.status = 200;
response.statusText = 'OK';
resolve(new window.Response(JSON.stringify(response), response));
} else if ((input instanceof Request && input.url === redirectedUrl) || input === redirectedUrl) {
response.status = 200;
response.statusText = 'OK';
const responseObject = new window.Response(JSON.stringify(response), response);
Object.defineProperty(responseObject, 'url', { value: redirectedTo, writable: true });
responseObject.clone = () => responseObject;
resolve(responseObject);
} else {
response.status = 404;
response.statusText = 'Bad request';
Expand Down Expand Up @@ -622,6 +631,67 @@ describe('fetch', () => {
});
});

describe.only('when response has url', () => {
beforeEach(async () => {
const propagateTraceHeaderCorsUrls = [url];
await prepareData(redirectedUrl, { propagateTraceHeaderCorsUrls });
});

afterEach(() => {
clearData();
});

it('span should have correct attributes', () => {
const span: tracing.ReadableSpan = exportSpy.args[1][0][0];
const attributes = span.attributes;
const keys = Object.keys(attributes);

console.log(keys);
assert.ok(
attributes[keys[0]] !== '',
`attributes ${AttributeNames.COMPONENT} is not defined`
);
assert.strictEqual(
attributes[keys[1]],
'GET',
`attributes ${SemanticAttributes.HTTP_METHOD} is wrong`
);
assert.strictEqual(
attributes[keys[2]],
redirectedUrl,
`attributes ${SemanticAttributes.HTTP_URL} is wrong`
);
assert.strictEqual(
attributes[keys[3]],
200,
`attributes ${SemanticAttributes.HTTP_STATUS_CODE} is wrong`
);
assert.ok(
attributes[keys[4]] === 'OK' || attributes[keys[4]] === '',
`attributes ${AttributeNames.HTTP_STATUS_TEXT} is wrong`
);
assert.ok(
(attributes[keys[5]] as string).indexOf('after.redirect') === 0,
`attributes ${SemanticAttributes.HTTP_HOST} is wrong`
);
assert.ok(
attributes[keys[6]] === 'http' || attributes[keys[6]] === 'https',
`attributes ${SemanticAttributes.HTTP_SCHEME} is wrong`
);
assert.ok(
attributes[keys[7]] !== '',
`attributes ${SemanticAttributes.HTTP_USER_AGENT} is not defined`
);
assert.ok(
(attributes[keys[8]] as number) > 0,
`attributes ${SemanticAttributes.HTTP_RESPONSE_CONTENT_LENGTH} is <= 0`
);

assert.strictEqual(keys.length, 9, 'number of attributes is wrong');
});
});


describe('applyCustomAttributesOnSpan option', () => {
const prepare = async (
url: string,
Expand Down

0 comments on commit bd01667

Please sign in to comment.