Skip to content

feat(browser otlp exporter): add fetch transport for fetch-only environments#5807

Merged
pichlermarc merged 31 commits intoopen-telemetry:mainfrom
SacDeNoeuds:feat/otlp-exporter--base-fetch-transport
Aug 29, 2025
Merged

feat(browser otlp exporter): add fetch transport for fetch-only environments#5807
pichlermarc merged 31 commits intoopen-telemetry:mainfrom
SacDeNoeuds:feat/otlp-exporter--base-fetch-transport

Conversation

@SacDeNoeuds
Copy link
Copy Markdown
Contributor

@SacDeNoeuds SacDeNoeuds commented Jul 22, 2025

Which problem is this PR solving?

In service workers, XMLHttpRequest and navigator.sendBeacon are both unavailable, making the OTLPTraceExporter from @opentelemetry/exporter-trace-otlp-http unusable.

Fixes #4631

Short description of the changes

Add the fetch transport and enables it only when both xhr and beacons are unavailable.

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

I took the xhr transport tests and adapted the arrange step for the fetch transport.
See the test file: experimental/packages/otlp-exporter-base/test/browser/fetch-transport.test.ts

Checklist:

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

Additional context

@SacDeNoeuds SacDeNoeuds requested a review from a team as a code owner July 22, 2025 07:18
@linux-foundation-easycla
Copy link
Copy Markdown

linux-foundation-easycla Bot commented Jul 22, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

@SacDeNoeuds SacDeNoeuds changed the title feat(browser otlp exporter): add fetch transport if xhr and beacon are unavailable feat(browser otlp exporter): add fetch transport for fetch-only environments Jul 22, 2025
@SacDeNoeuds SacDeNoeuds force-pushed the feat/otlp-exporter--base-fetch-transport branch from 2c4a8b6 to e3cd53a Compare July 22, 2025 08:28
Copy link
Copy Markdown
Member

@dyladan dyladan left a comment

Choose a reason for hiding this comment

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

Seems reasonable to me and I don't see any obvious issues. I'd wait for @pichlermarc review before merging because he's much closer to the exporters but overall LGTM

Comment thread experimental/packages/otlp-exporter-base/src/transport/fetch-transport.ts Outdated
@codecov
Copy link
Copy Markdown

codecov Bot commented Jul 22, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.06%. Comparing base (95b40aa) to head (84f971e).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #5807   +/-   ##
=======================================
  Coverage   95.06%   95.06%           
=======================================
  Files         307      307           
  Lines        8031     8031           
  Branches     1627     1627           
=======================================
  Hits         7635     7635           
  Misses        396      396           
Files with missing lines Coverage Δ
...-base/src/configuration/otlp-http-configuration.ts 100.00% <100.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Comment thread experimental/packages/otlp-exporter-base/src/transport/fetch-transport.ts Outdated
@SacDeNoeuds
Copy link
Copy Markdown
Contributor Author

Hi guys, any news on this?

@raphael-theriault-swi
Copy link
Copy Markdown
Member

raphael-theriault-swi commented Aug 7, 2025

We looked at it during the SIG meeting two or three weeks ago but @pichlermarc (who's the de-facto code owner for the exporters) was on vacation at the time. IIRC there was also talks of using fetch for Node.js too / dropping the XHR transport.

@pichlermarc
Copy link
Copy Markdown
Member

IRC there was also talks of using fetch for Node.js too / dropping the XHR transport.

yes, my personal preference would be to drop XHR in favor of fetch as we could increase compatibility with other runtimes while still keeping bundle size roughly the same and keep complexity low.

There has been some push-back to doing this in the past - so I will ask around in the SIG again today and will report back here.

Copy link
Copy Markdown
Member

@pichlermarc pichlermarc left a comment

Choose a reason for hiding this comment

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

Alright, so looks like we'll move ahead with this PR here and then we'll drop XHR in a later release.

@SacDeNoeuds would you mind marking XhrTransport and associated types as @depreacated? (referring to the newly added FetchTransport as a replacement - with polyfill provided by the end-user if necessary.

I'll create a follow-up issue to tackle removing XHR eventually once this PR has merged.

Comment thread experimental/packages/otlp-exporter-base/src/transport/fetch-transport.ts Outdated
Comment thread experimental/packages/otlp-exporter-base/src/transport/fetch-transport.ts Outdated
Comment thread experimental/packages/otlp-exporter-base/test/browser/fetch-transport.test.ts Outdated
Comment on lines +50 to 60
export function inferExportDelegateToUse(
configHeaders: OTLPExporterConfigBase['headers']
) {
if (!configHeaders && typeof navigator.sendBeacon === 'function') {
return createOtlpSendBeaconExportDelegate;
} else if (typeof globalThis.fetch !== 'undefined') {
return createOtlpFetchExportDelegate;
} else {
return createOtlpSendBeaconExportDelegate(options, serializer);
return createOtlpXhrExportDelegate;
}
}
Copy link
Copy Markdown
Contributor Author

@SacDeNoeuds SacDeNoeuds Aug 21, 2025

Choose a reason for hiding this comment

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

The former test I wrote was ineffective, every createOtlpXxExporterDelegate returns an instance of the class OTLPExportDelegate, the initial tests were flawed considering they were testing the instance constructor and it was the same constructor – OTLPExportDelegate – in all cases.

I added an intermediary testable function and updated the test.

Copy link
Copy Markdown
Member

@pichlermarc pichlermarc left a comment

Choose a reason for hiding this comment

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

Thanks for making the changes, just needs some more adjustment on the changelog and we should be good to go. :)

Comment thread experimental/CHANGELOG.md Outdated
Comment thread experimental/CHANGELOG.md
@pichlermarc
Copy link
Copy Markdown
Member

oh - looks like some of the tests in the individual exporters now need to be adapted too as they expect XHR to be used. There is no need to test all cases in the individual exporters as we do test that behavior in the base package already, but we should change the tests from XHR to fetch there.

@SacDeNoeuds
Copy link
Copy Markdown
Contributor Author

Thanks again for your reviews, I took a bit of time to update all the tests, all tests should pass now.

It may be worth adding the script npm run test:browser along with npm run test in the CONTRIBUTING guide, I assumed npm run test was covering both server and browser environments.

Copy link
Copy Markdown
Member

@pichlermarc pichlermarc left a comment

Choose a reason for hiding this comment

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

Looks good, thank you for working on this 🙌

@SacDeNoeuds
Copy link
Copy Markdown
Contributor Author

Thanks @pichlermarc, I just fixed a small changelog conflict. I suppose you will be dealing with PR merging?

@pichlermarc pichlermarc added this pull request to the merge queue Aug 29, 2025
Merged via the queue into open-telemetry:main with commit 3dcdba6 Aug 29, 2025
24 checks passed
@opentelemetrybot
Copy link
Copy Markdown
Contributor

Thank you for your contribution @SacDeNoeuds! 🎉 We would like to hear from you about your experience contributing to OpenTelemetry by taking a few minutes to fill out this survey.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Relative logging-url ('./api/logs) no longer supported - 'Could not parse user-provided export URL' TraceExporter for fetch-only environment

8 participants