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

Track request body size in XHR and Fetch instrumentations #4706

Open
wants to merge 32 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 31 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
e349fa4
reduce overlap between (variable shadowing) between individual tests
MustafaHaddara May 10, 2024
656cbc4
update xhr test to check attr names directly instead of using key order
MustafaHaddara May 9, 2024
538e712
update fetch test to read attributes directly instead of using key order
MustafaHaddara May 14, 2024
eaf9786
override api request in beforeEach call
MustafaHaddara May 14, 2024
d6149ca
Add getXHRBodyLength and getFetchBodyLength functions to opentelemetr…
MustafaHaddara May 14, 2024
d97b02b
track request body content length in xhr instrumentation
MustafaHaddara May 14, 2024
860557e
track request body content length in fetch instrumentation
MustafaHaddara May 14, 2024
9dfe663
add changelog entry
MustafaHaddara May 14, 2024
a14c1f9
update browser; webworker tests
MustafaHaddara May 14, 2024
499c8ca
account for older platforms where ReadableStream can't tee()
MustafaHaddara May 22, 2024
19890b5
lint fix: add comment after @ts-expect-error
MustafaHaddara May 30, 2024
bee76c8
make response body measurement opt-in
MustafaHaddara Jul 15, 2024
854cc53
update named exports
MustafaHaddara Aug 12, 2024
24e3b46
Merge branch 'main' of github.com:open-telemetry/opentelemetry-js int…
MustafaHaddara Aug 19, 2024
12c42e3
switch to experimental new semantic attribute
MustafaHaddara Aug 19, 2024
91637aa
warn instead of error
MustafaHaddara Aug 19, 2024
08ab734
correctly read string length
MustafaHaddara Aug 19, 2024
8d3c533
consistent return value when we can't calculate length
MustafaHaddara Aug 19, 2024
bf92869
lint fix
MustafaHaddara Aug 19, 2024
f21eb54
update ReadableStream length calculation
MustafaHaddara Aug 23, 2024
102d128
switch to pipeThrough() to reduce memory use
MustafaHaddara Aug 23, 2024
a0a26ea
Merge branch 'main' of github.com:open-telemetry/opentelemetry-js int…
MustafaHaddara Aug 23, 2024
8b94dbc
document the semconv attributes we include
MustafaHaddara Aug 23, 2024
3569096
more correct handling of FormData
MustafaHaddara Aug 23, 2024
d011829
support formData.entries()
MustafaHaddara Aug 26, 2024
178e73b
update stream in fetch tests
MustafaHaddara Aug 26, 2024
74cb0ea
add a warning for old platforms
MustafaHaddara Aug 26, 2024
7c2bfe2
Merge branch 'main' of github.com:open-telemetry/opentelemetry-js int…
MustafaHaddara Aug 26, 2024
51cc125
fix fetch tests
MustafaHaddara Aug 26, 2024
f0dcca9
Merge branch 'main' of github.com:open-telemetry/opentelemetry-js int…
MustafaHaddara Aug 27, 2024
192f440
Merge branch 'main' into request-body-size
JamieDanielson Aug 27, 2024
0007def
Merge branch 'main' into request-body-size
MustafaHaddara Oct 16, 2024
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
1 change: 1 addition & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ All notable changes to experimental packages in this project will be documented
* feat(otlp-transformer): Do not limit @opentelemetry/api upper range peerDependency [#4816](https://github.com/open-telemetry/opentelemetry-js/pull/4816) @mydea
* feat(instrumentation-http): Allow to opt-out of instrumenting incoming/outgoing requests [#4643](https://github.com/open-telemetry/opentelemetry-js/pull/4643) @mydea
* feat(sampler-jaeger-remote): added support of jaeger-remote-sampler according to this [spec](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/sdk.md#jaegerremotesampler) [#4534](https://github.com/open-telemetry/opentelemetry-js/pull/4589) @legalimpurity
* feat(instrumentation): Track request body size in XHR and Fetch instrumentations [#4706](https://github.com/open-telemetry/opentelemetry-js/pull/4706) @mustafahaddara

### :bug: (Bug Fix)

Expand Down
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) |
JamieDanielson marked this conversation as resolved.
Show resolved Hide resolved

## Semantic Conventions

Expand All @@ -80,12 +81,13 @@ Attributes collected:

| Attribute | Short Description |
| ------------------------------------------- | ------------------------------------------------------------------------------ |
| `http.status_code` | HTTP response status code |
| `http.status_code` | HTTP response status code |
| `http.host` | The value of the HTTP host header |
| `http.user_agent` | Value of the HTTP User-Agent header sent by the client |
| `http.scheme` | The URI scheme identifying the used protocol |
| `http.url` | Full HTTP request URL |
| `http.method` | HTTP request method |
| `http.request.body.size` | Uncompressed size of the request body, if any body exists |

## Useful links

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
SEMATTRS_HTTP_URL,
SEMATTRS_HTTP_METHOD,
} from '@opentelemetry/semantic-conventions';
import { ATTR_HTTP_REQUEST_BODY_SIZE } from '@opentelemetry/semantic-conventions/incubating';
import { FetchError, FetchResponse, SpanData } from './types';
import { VERSION } from './version';
import { _globalThis } from '@opentelemetry/core';
Expand Down Expand Up @@ -74,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 @@ -320,6 +323,19 @@ export class FetchInstrumentation extends InstrumentationBase<FetchInstrumentati
}
const spanData = plugin._prepareSpanData(url);

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

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

function endSpanOnError(span: api.Span, error: FetchError) {
plugin._applyAttributesAfterFetch(span, options, error);
plugin._endSpan(span, spanData, {
Expand Down
Loading