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

Supports gzip compression in the browser environment #4162

Closed
wants to merge 1 commit into from

Conversation

fuaiyi
Copy link
Contributor

@fuaiyi fuaiyi commented Sep 26, 2023

Which problem is this PR solving?

Supports gzip compression in the browser environment

Short description of the changes

Implemented support for Gzip compression in the browser environment for otlp-exporter-base (previously only supported in Node.js environment), utilizing pako for Gzip compression capability.
Using Gzip compression can greatly reduce transmission and bandwidth costs. When combined with the pb (protobuf) protocol, there can be even greater improvements. Testing showed an improvement of 70% to 80% before and after the implementation (based on sample data, the specific compression ratio may vary depending on the characteristics of the data).

none:
image
pb:
image
pb+gzip:
image

Type of change

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

How Has This Been Tested?

  • Unit Test

Checklist:

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

@fuaiyi fuaiyi requested a review from a team September 26, 2023 08:29
@fuaiyi fuaiyi closed this Sep 26, 2023
fuaiyi added a commit to fuaiyi/opentelemetry-js that referenced this pull request Sep 26, 2023
@fuaiyi fuaiyi reopened this Sep 26, 2023
@codecov
Copy link

codecov bot commented Sep 26, 2023

Codecov Report

Merging #4162 (8f5145a) into main (2b9832e) will decrease coverage by 0.02%.
The diff coverage is 91.30%.

❗ Current head 8f5145a differs from pull request most recent head 548cb2e. Consider uploading reports for the commit 548cb2e to get more accurate results

@@            Coverage Diff             @@
##             main    #4162      +/-   ##
==========================================
- Coverage   92.29%   92.28%   -0.02%     
==========================================
  Files         329      328       -1     
  Lines        9370     9381      +11     
  Branches     1991     1995       +4     
==========================================
+ Hits         8648     8657       +9     
- Misses        722      724       +2     
Files Coverage Δ
...-otlp-http/src/platform/browser/OTLPLogExporter.ts 83.33% <100.00%> (ø)
...tlp-http/src/platform/browser/OTLPTraceExporter.ts 100.00% <100.00%> (ø)
...lp-http/src/platform/browser/OTLPMetricExporter.ts 100.00% <100.00%> (ø)
...ter-base/src/platform/node/OTLPExporterNodeBase.ts 50.00% <ø> (ø)
...kages/otlp-exporter-base/src/platform/node/util.ts 63.80% <100.00%> (-0.35%) ⬇️
...erimental/packages/otlp-exporter-base/src/types.ts 44.44% <100.00%> (+27.77%) ⬆️
...es/otlp-exporter-base/src/platform/browser/util.ts 42.30% <87.50%> (+7.45%) ⬆️

... and 2 files with indirect coverage changes

fuaiyi added a commit to fuaiyi/opentelemetry-js that referenced this pull request Sep 26, 2023
fuaiyi added a commit to fuaiyi/opentelemetry-js that referenced this pull request Sep 26, 2023
@@ -61,14 +61,16 @@
"access": "public"
},
"dependencies": {
"@opentelemetry/core": "1.17.0"
"@opentelemetry/core": "1.17.0",
"pako": "^2.1.0"
Copy link
Member

Choose a reason for hiding this comment

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

if pako is only needed for web or also for node.js?
as far as I know node.js has gzip support built in.

Copy link
Contributor

Choose a reason for hiding this comment

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

For web there's also Compression Streams API which could avoid the extra bundle size of pako (if browser support matches expectations)

}).forEach(([k, v]) => {
xhr.setRequestHeader(k, v);
});

xhr.send(body);
Copy link
Contributor

Choose a reason for hiding this comment

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

Architectural design opinion: I don't think loading up code with a bunch of options is good, especially if you want to do this for web use

  • For browsers if-s don't get shaken out during bundle time (static analysis doesn't work that deep, mainly if (false)-s done by search-and-replace plugins), meaning if someone decides to not use something, it's still a bunch of dead code included in the bundle
  • It ties it down to a specific implementation and if there's anything js loves it's a billion ways to do something (eg. pako / deflate / new hotness lib of the month / builtin nodejs gz / compression stream api / maybe someone wants brotli at some point making first point double relevant)

I think it would be better to provide hooks configuration (analogous to middleware app.use on the server side) for exporters where callback functions could manipulate request based on users preferences/needs, which is a lot more easily treehshaken (as only functions used would get bundled), eg:

import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-grpc';

// This is actually where using fetch API internally would allow for a more js standards based API (Request object)
// Also all of this is totally inspired by ky library, gotta throw some respect to well designed libs
function compressUsingPako(req: Request) {
  return new Request(req, {
    body: pako.gzip(req.body),
    headers: {
      'Content-Encoding': 'gzip'
    }
  });
}

const exporter = new OTLPTraceExporter({
  url: 'https://somewhere',
  hooks: [
    compressUsingPako,
    // Hooks could find many other use cases around changes to request
    signRequest,
    attachBatchId,
  ]
});

@martinkuba
Copy link
Contributor

One important consideration, IMO, is also the CPU overhead the compression step adds during export. Have you looked at this, and would you be able to share some numbers?

Copy link

This PR is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 14 days.

@github-actions github-actions bot added the stale label Nov 27, 2023
@fuaiyi fuaiyi closed this Nov 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants