From bd01667977943e005f90ecb125435c58ca8b2d79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaras=C5=82a=C5=AD=20Viktor=C4=8Dyk?= Date: Mon, 13 Jun 2022 16:22:17 +0200 Subject: [PATCH] fix(fetch): pass spanResponse to _endSpan --- .../src/fetch.ts | 8 +-- .../test/fetch.test.ts | 70 +++++++++++++++++++ 2 files changed, 72 insertions(+), 6 deletions(-) diff --git a/experimental/packages/opentelemetry-instrumentation-fetch/src/fetch.ts b/experimental/packages/opentelemetry-instrumentation-fetch/src/fetch.ts index ba4a2f422b7..b7822cf8812 100644 --- a/experimental/packages/opentelemetry-instrumentation-fetch/src/fetch.ts +++ b/experimental/packages/opentelemetry-instrumentation-fetch/src/fetch.ts @@ -323,14 +323,10 @@ export class FetchInstrumentation extends InstrumentationBase> }; 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( diff --git a/experimental/packages/opentelemetry-instrumentation-fetch/test/fetch.test.ts b/experimental/packages/opentelemetry-instrumentation-fetch/test/fetch.test.ts index c7a55acb7d4..084c757fa15 100644 --- a/experimental/packages/opentelemetry-instrumentation-fetch/test/fetch.test.ts +++ b/experimental/packages/opentelemetry-instrumentation-fetch/test/fetch.test.ts @@ -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 = () => { @@ -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'; @@ -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,