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

Conversation

MustafaHaddara
Copy link
Contributor

@MustafaHaddara MustafaHaddara commented May 14, 2024

Which problem is this PR solving?

The Fetch and XHR instrumentations expose http.response_content_length attributes but do not expose http.request_content_length attributes. This PR adds the http.request_content_length attributes to outgoing requests that have a body (ex. POST, PATCH, DELETE, etc.)

Short description of the changes

Ideally, there would be some browser API would could just read for this (similar to how we get the response content length via the PerformanceObserver API). However, no such API exists.

Second best would be if we could read the content-length request header. Unfortunately, the XMLHTTPRequest API does not offer any way to read request headers. Even if we could (ie. with the fetch API), this header seems to be set automatically by the browser before it actually sends the request, outside of user-space.

So, we have to compute the body length on our own. This PR implements that.

Detailed Description

The first few commits (e349fa4...eaf9786) are refactorings/updates, mainly to unit tests, to enable changes and tests that follow.

The primary changes are contained in these 3 commits:

  • d6149ca adds getXHRBodyLength and getFetchBodyLength utils to the opentelemetry-sdk-trace-web package.
    • getFetchBodyLength needs to call getXHRBodyLength, otherwise I would have defined these in their respective packages.
  • d97b02b calls getXHRBodyLength from the XHR instrumentation package and adds unit tests for the XHR instrumentation
  • 860557e calls getFetchBodyLength from the Fetch instrumentation package and adds unit tests for the Fetch instrumentation
  • bee76c8 makes this functionality opt-in

The getXHRBodyLength function is mostly straightforward; the XHR API is not too complicated and is fairly self-explanatory.

On the other hand, the getFetchBodyLength function is more complex. The root of the problem is that the fetch API doesn't expose clean ways for us to get the body content. In places where it is possible, it is often consumable only once, and often as aPromise that resolves to the body content. I had to take care to not consume the actual body content; we do not want this instrumentation to interfere with actual requests. It is possible that a bug in this implementation would result in the bodies on fetch requests getting consuming by this instrumentation and then not actually included in the network request.

Type of change

  • New feature (non-breaking change which adds functionality)

How Has This Been Tested?

  • Added unit tests to opentelemetry-sdk-trace-web, opentelemetry-instrumentation-xml-http-request, and opentelemetry-instrumentation-fetch

Checklist:

  • Followed the style guidelines of this project
  • Unit tests have been added

@MustafaHaddara MustafaHaddara requested a review from a team May 14, 2024 16:00
@MustafaHaddara MustafaHaddara force-pushed the request-body-size branch 3 times, most recently from a4cb688 to 14c9323 Compare May 15, 2024 14:54
@MustafaHaddara MustafaHaddara force-pushed the request-body-size branch 2 times, most recently from 0988e15 to c67e910 Compare May 22, 2024 19:45
Copy link

codecov bot commented May 22, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 93.27%. Comparing base (7ed67f9) to head (0007def).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4706      +/-   ##
==========================================
+ Coverage   93.26%   93.27%   +0.01%     
==========================================
  Files         317      317              
  Lines        8195     8195              
  Branches     1641     1641              
==========================================
+ Hits         7643     7644       +1     
+ Misses        552      551       -1     

see 1 file with indirect coverage changes

@MustafaHaddara
Copy link
Contributor Author

@scheler @MSNev I've made the changes we discussed and resolved merge conflicts. Please let me know if you have any other questions.

Comment on lines +267 to +278
function getFormDataSize(formData: FormData): number {
let size = 0;
for (const [key, value] of formData.entries()) {
size += key.length;
if (value instanceof Blob) {
size += value.size;
} else {
size += value.length;
}
}
return size;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an improvement over the old implementation (which was stringifying the FormData, and that wouldn't actually stringify file objects correctly), but it still doesn't calculate the exact FormData size. Browsers will serialize FormData in their own way (ex including a separator for each of the fields, etc.)

I will keep investigating for more accurate approaches.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this seems good enough for now, and maybe if anyone is sufficiently motivated they can contribute the changes to calculate it more precisely for their platform 😬

@MustafaHaddara
Copy link
Contributor Author

@JamieDanielson

It could change to http.request.body.size in an upcoming SemConv update, but may not since that attribute is not yet stable. TBD I think. I'm good with the attribute as-is in this PR.

I went ahead and used the new (unstable) attribute; let me know if you want me to revert that.

@tbrockman @MSNev @JamieDanielson I've applied all of the requested changes and this PR should be ready for another review. Let me know if I missed anything.

Comment on lines +271 to +276
size += key.length;
if (value instanceof Blob) {
size += value.size;
} else {
size += value.length;
}
Copy link

@tbrockman tbrockman Sep 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the 👻 here, just been a bit busy with life and quitting #dayjob!

Just noticed, should these also use getByteLength?

Feels silly to suggest it given that FormData size varies (as you mentioned, browser/platform-specific implementation differences from things like boundaries and such, I just checked out Firefox for example), so I understand if it seems unnecessary at this point since it'd just be shaving a tiny bit of hypothetical inaccuracy off an estimate that will inherently be incorrect under the circumstances that it applies to (and feel free to ignore).

Suggested change
size += key.length;
if (value instanceof Blob) {
size += value.size;
} else {
size += value.length;
}
size += getByteLength(key.length);
if (value instanceof Blob) {
size += value.size;
} else {
size += getByteLength(value.length);
}

}

if (typeof body === 'string') {
return body.length;
Copy link

@tbrockman tbrockman Sep 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MustafaHaddara For the most part I think that would be absolutely true! But I also wouldn't underestimate the potential for people to send large JSON payloads (either intentionally or unintentionally).

As a contemporary example, OpenAI's chat API accepts user image input as "[e]ither a URL of the image or the base64 encoded image data.", so you can imagine that for a large image this might amount to some meaningful overhead if someone sends their images inline.

Given some of the other inherent issues for calculating content length size with FormData, would you be open to allowing users to specify their own optional getXHRBodyLength (or maybe exposed as calculateBodyLength) function?

For context, I'm hoping to use this functionality in Browser Extension for OpenTelemetry, and I'd like to limit the overhead as much as possible when it already involves injecting big blobs of Javascript into pages which might be making requests that extension users may not have much control over. This way, I can supply my own implementation (which may be less maintainable/correct/costly) and also experiment with browser-specific calculations, without any maintenance burden being placed on this project.

@JamieDanielson
Copy link
Member

JamieDanielson commented Sep 25, 2024

from comment thread above, moving here for easier finding:

In the semantic conventions tooling meeting this morning a couple of things came up which affect what this PR should be doing

Follow-up notes from JS SIG meeting discussion:

  • This PR just uses http.request.body.size since the previous version http.request_content_length didn't exist previously anyway, which should be fine.
  • This PR does have an opt-in mechanism, and it is disabled by default.
  • Because this opt-in mechanism for certain http attributes includes more than just this attribute and this instrumentation, it may be preferred to move this configuration flag elsewhere so it can be shared by multiple instrumentations. For example, Java added an experimental-opt-in flag that includes these.

@dyladan @MSNev did I capture that correctly? And if so, what are the next steps here? I suspect we'll keep much of this logic already written in this PR and just change the enable/disable mechanism.

@MustafaHaddara
Copy link
Contributor Author

@dyladan @MSNev I just wanted to follow up on @JamieDanielson's message above-- what would you like me to do with this PR?

@MustafaHaddara MustafaHaddara requested a review from a team as a code owner October 16, 2024 15:06
@MustafaHaddara
Copy link
Contributor Author

We discussed this PR at the client-side SIG yesterday. The two remaining discussion threads:

from @JamieDanielson:

Because this opt-in mechanism for certain http attributes includes more than just this attribute and this instrumentation, it may be preferred to move this configuration flag elsewhere so it can be shared by multiple instrumentations. For example, Java added an experimental-opt-in flag that includes these.

My position is that we can update the opt-in mechanism in the future, if we decide to go that route and build one flag to control multiple instrumentations.

As for @tbrockman 's memory concerns:

But I also wouldn't underestimate the potential for people to send large JSON payloads (either intentionally or unintentionally).

[...]

Given some of the other inherent issues for calculating content length size with FormData, would you be open to allowing users to specify their own optional getXHRBodyLength (or maybe exposed as calculateBodyLength) function?

I'm not too worried about memory concerns since this entire instrumentation is opt-in, and I think making the suggested change is something that we can discuss in a follow up issue or PR.

@pichlermarc pichlermarc requested a review from a team October 23, 2024 09:53
| `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 |
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This instrumentation implement Semantic Conventions v1.7 so we'd have to emit http.request_content_length instead to avoid inconsistency in the emitted telemetry.

I know we mentioned earlier that this should be fine but we have rejected/requested changes on PRs (for example #4962) in the past for similar kinds of inconsistency.

Requested changes: using http.request_content_length over the latest semantic conventions (I think it was mentioned that the attribute did not exist in the semconv package in an earlier comment, if that's indeed the case we should introduce a local constant in the respective instrumentation package to use it)

@@ -125,6 +129,155 @@ export function addSpanNetworkEvents(
}
}

function _getBodyNonDestructively(body: ReadableStream) {
Copy link
Member

@pichlermarc pichlermarc Oct 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My guess is that the reason this requires changes to @opentelemetry/sdk-trace-web is that we're trying to:

  • reduce added bundle size for when both instrumentations are added
  • reduce duplicated code and tests for the same functionality.

I wonder though: is the added public API surface in a stable package worth the trade-off? I think since it's only duplicated twice the vast majority of the end-users will never use this function directly, I'd much rather have duplicated code than the added public stable API that we have to keep stable in perpetuity.

Q: given that these instrumentations share code so directly, and the concepts they instrument are so intertwined, would it make sense for these instrumentations to be located in a single package instead of two different ones? Both of the packages are experimental so moving them around is not completely impossible. I'm also open to other ideas 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
spec-feature This is a request to implement a new feature which is already specified by the OTel specification
Projects
Development

Successfully merging this pull request may close these issues.

7 participants