Skip to content

Commit

Permalink
make response body measurement opt-in
Browse files Browse the repository at this point in the history
  • Loading branch information
MustafaHaddara committed Aug 12, 2024
1 parent 19890b5 commit bee76c8
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ Fetch instrumentation plugin has few options available to choose from. You can s

| Options | Type | Description |
|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------|-----------------------------------------------------------------------------------------|
| [`applyCustomAttributesOnSpan`](https://github.com/open-telemetry/opentelemetry-js/blob/main/experimental/packages/opentelemetry-instrumentation-fetch/src/fetch.ts#L64) | `HttpCustomAttributeFunction` | Function for adding custom attributes |
| [`ignoreNetworkEvents`](https://github.com/open-telemetry/opentelemetry-js/blob/main/experimental/packages/opentelemetry-instrumentation-fetch/src/fetch.ts#L67) | `boolean` | Disable network events being added as span events (network events are added by default) |
| [`applyCustomAttributesOnSpan`](https://github.com/open-telemetry/opentelemetry-js/blob/main/experimental/packages/opentelemetry-instrumentation-fetch/src/fetch.ts#L75) | `HttpCustomAttributeFunction` | Function for adding custom attributes |
| [`ignoreNetworkEvents`](https://github.com/open-telemetry/opentelemetry-js/blob/main/experimental/packages/opentelemetry-instrumentation-fetch/src/fetch.ts#L77) | `boolean` | Disable network events being added as span events (network events are added by default) |
| [`measureRequestSize`](https://github.com/open-telemetry/opentelemetry-js/blob/main/experimental/packages/opentelemetry-instrumentation-fetch/src/fetch.ts#L79) | `boolean` | Measure outgoing request length (outgoing request length is not measured by default) |

## Semantic Conventions

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ export interface FetchInstrumentationConfig extends InstrumentationConfig {
applyCustomAttributesOnSpan?: FetchCustomAttributeFunction;
// Ignore adding network events as span events
ignoreNetworkEvents?: boolean;
/** Measure outgoing request size */
measureRequestSize?: boolean;
}

/**
Expand Down Expand Up @@ -321,19 +323,21 @@ export class FetchInstrumentation extends InstrumentationBase<FetchInstrumentati
}
const spanData = plugin._prepareSpanData(url);

web
.getFetchBodyLength(...args)
.then(length => {
if (!length) return;

createdSpan.setAttribute(
SEMATTRS_HTTP_REQUEST_CONTENT_LENGTH,
length
);
})
.catch(error => {
plugin._diag.error('getFetchBodyLength', error);
});
if (plugin.getConfig().measureRequestSize) {
web
.getFetchBodyLength(...args)
.then(length => {
if (!length) return;

createdSpan.setAttribute(
SEMATTRS_HTTP_REQUEST_CONTENT_LENGTH,
length
);
})
.catch(error => {
plugin._diag.error('getFetchBodyLength', error);
});
}

function endSpanOnError(span: api.Span, error: FetchError) {
plugin._applyAttributesAfterFetch(span, options, error);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,7 @@ describe('fetch', () => {
});

describe('post data', () => {
describe('url and config object', () => {
describe('url and config object when request body measurement is disabled', () => {
beforeEach(async () => {
await prepareData(
url,
Expand All @@ -675,6 +675,43 @@ describe('fetch', () => {
clearData();
});

it('should post data', async () => {
assert.strictEqual(requestBody, '{"hello":"world"}');

const span: tracing.ReadableSpan = exportSpy.args[1][0][0];
const attributes = span.attributes;

assert.strictEqual(
attributes[SEMATTRS_HTTP_REQUEST_CONTENT_LENGTH],
undefined
);
});
});

describe('url and config object', () => {
beforeEach(async () => {
await prepareData(
url,
() =>
fetch(url, {
method: 'POST',
headers: {
foo: 'bar',
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({ hello: 'world' }),
}),
{
measureRequestSize: true,
}
);
});

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

it('should post data', async () => {
assert.strictEqual(requestBody, '{"hello":"world"}');

Expand All @@ -688,7 +725,7 @@ describe('fetch', () => {
});
});

describe.only('url and config object with stream', () => {
describe('url and config object with stream', () => {
beforeEach(async () => {
await prepareData(
url,
Expand All @@ -702,7 +739,9 @@ describe('fetch', () => {
},
body: textToReadableStream('{"hello":"world"}'),
}),
{}
{
measureRequestSize: true,
}
);
});

Expand Down Expand Up @@ -739,7 +778,9 @@ describe('fetch', () => {
});
return fetch(req);
},
{}
{
measureRequestSize: true,
}
);
});

Expand Down Expand Up @@ -778,7 +819,9 @@ describe('fetch', () => {
});
return fetch(req);
},
{}
{
measureRequestSize: true,
}
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,9 @@ XHR instrumentation plugin has few options available to choose from. You can set

| Options | Type | Description |
|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------|-----------------------------------------------------------------------------------------|
| [`applyCustomAttributesOnSpan`](https://github.com/open-telemetry/opentelemetry-js/blob/main/experimental/packages/opentelemetry-instrumentation-xml-http-request/src/xhr.ts#L76) | `XHRCustomAttributeFunction` | Function for adding custom attributes |
| [`ignoreNetworkEvents`](https://github.com/open-telemetry/opentelemetry-js/blob/main/experimental/packages/opentelemetry-instrumentation-xml-http-request/src/xhr.ts#L78) | `boolean` | Disable network events being added as span events (network events are added by default) |
| [`applyCustomAttributesOnSpan`](https://github.com/open-telemetry/opentelemetry-js/blob/main/experimental/packages/opentelemetry-instrumentation-xml-http-request/src/xhr.ts#L85) | `XHRCustomAttributeFunction` | Function for adding custom attributes |
| [`ignoreNetworkEvents`](https://github.com/open-telemetry/opentelemetry-js/blob/main/experimental/packages/opentelemetry-instrumentation-xml-http-request/src/xhr.ts#L87) | `boolean` | Disable network events being added as span events (network events are added by default) |
| [`measureRequestSize`](https://github.com/open-telemetry/opentelemetry-js/blob/main/experimental/packages/opentelemetry-instrumentation-xml-http-request/src/xhr.ts#L89) | `boolean` | Measure outgoing request length (outgoing request length is not measured by default) |

## Example Screenshots

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ export interface XMLHttpRequestInstrumentationConfig
applyCustomAttributesOnSpan?: XHRCustomAttributeFunction;
/** Ignore adding network events as span events */
ignoreNetworkEvents?: boolean;
/** Measure outgoing request size */
measureRequestSize?: boolean;
}

/**
Expand Down Expand Up @@ -488,7 +490,7 @@ export class XMLHttpRequestInstrumentation extends InstrumentationBase<XMLHttpRe
const spanUrl = xhrMem.spanUrl;

if (currentSpan && spanUrl) {
if (args?.[0]) {
if (plugin.getConfig().measureRequestSize && args?.[0]) {
const body = args[0];
currentSpan.setAttribute(
SEMATTRS_HTTP_REQUEST_CONTENT_LENGTH,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,10 @@ describe('xhr', () => {

beforeEach(done => {
const propagateTraceHeaderCorsUrls = [window.location.origin];
prepareData(done, url, { propagateTraceHeaderCorsUrls });
prepareData(done, url, {
propagateTraceHeaderCorsUrls,
measureRequestSize: true,
});
});

afterEach(() => {
Expand Down Expand Up @@ -1527,7 +1530,10 @@ describe('xhr', () => {

beforeEach(done => {
const propagateTraceHeaderCorsUrls = [window.location.origin];
prepareData(done, url, { propagateTraceHeaderCorsUrls });
prepareData(done, url, {
propagateTraceHeaderCorsUrls,
measureRequestSize: true,
});
});

afterEach(() => {
Expand Down Expand Up @@ -2203,8 +2209,8 @@ describe('xhr', () => {
] as number;
assert.strictEqual(
requestContentLength,
19,
`attributes ${SEMATTRS_HTTP_REQUEST_CONTENT_LENGTH} !== 19`
undefined,
`attributes ${SEMATTRS_HTTP_REQUEST_CONTENT_LENGTH} is defined`
);
const responseContentLength = attributes[
SEMATTRS_HTTP_RESPONSE_CONTENT_LENGTH
Expand Down Expand Up @@ -2241,7 +2247,7 @@ describe('xhr', () => {
`attributes ${SEMATTRS_HTTP_USER_AGENT} is not defined`
);

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

it('span should have correct events', () => {
Expand Down Expand Up @@ -2317,8 +2323,8 @@ describe('xhr', () => {
] as number;
assert.strictEqual(
requestContentLength,
19,
`attributes ${SEMATTRS_HTTP_REQUEST_CONTENT_LENGTH} !== 19`
undefined,
`attributes ${SEMATTRS_HTTP_REQUEST_CONTENT_LENGTH} is defined`
);
const responseContentLength = attributes[
SEMATTRS_HTTP_RESPONSE_CONTENT_LENGTH
Expand All @@ -2329,7 +2335,7 @@ describe('xhr', () => {
`attributes ${SEMATTRS_HTTP_RESPONSE_CONTENT_LENGTH} is defined`
);

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

it('span should have correct events', () => {
Expand Down Expand Up @@ -2402,8 +2408,8 @@ describe('xhr', () => {
] as number;
assert.strictEqual(
requestContentLength,
19,
`attributes ${SEMATTRS_HTTP_REQUEST_CONTENT_LENGTH} !== 19`
undefined,
`attributes ${SEMATTRS_HTTP_REQUEST_CONTENT_LENGTH} is defined`
);
const responseContentLength = attributes[
SEMATTRS_HTTP_RESPONSE_CONTENT_LENGTH
Expand All @@ -2414,7 +2420,7 @@ describe('xhr', () => {
`attributes ${SEMATTRS_HTTP_RESPONSE_CONTENT_LENGTH} is defined`
);

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

it('span should have correct events', () => {
Expand Down Expand Up @@ -2487,8 +2493,8 @@ describe('xhr', () => {
] as number;
assert.strictEqual(
requestContentLength,
19,
`attributes ${SEMATTRS_HTTP_REQUEST_CONTENT_LENGTH} !== 19`
undefined,
`attributes ${SEMATTRS_HTTP_REQUEST_CONTENT_LENGTH} is defined`
);
const responseContentLength = attributes[
SEMATTRS_HTTP_RESPONSE_CONTENT_LENGTH
Expand All @@ -2499,7 +2505,7 @@ describe('xhr', () => {
`attributes ${SEMATTRS_HTTP_RESPONSE_CONTENT_LENGTH} is defined`
);

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

it('span should have correct events', () => {
Expand Down

0 comments on commit bee76c8

Please sign in to comment.