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

TraceExporter for fetch-only environment #4631

Open
lacolaco opened this issue Apr 14, 2024 · 13 comments
Open

TraceExporter for fetch-only environment #4631

lacolaco opened this issue Apr 14, 2024 · 13 comments

Comments

@lacolaco
Copy link

lacolaco commented Apr 14, 2024

NB: Before opening a feature request against this repo, consider whether the feature should/could be implemented in the other OpenTelemetry client libraries. If so, please open an issue on opentelemetry-specification first.

Is your feature request related to a problem? Please describe.

Currently, built-in TraceExporter implementation for browser depends on XMLHttpRequest or sendBeacon. These can't be adopted for fetch-only environments like WebWorker, ServiceWorker, or some edge workers (e.g. Cloudflare workers).

https://github.com/open-telemetry/opentelemetry-js/blob/main/experimental/packages/otlp-exporter-base/src/platform/browser/OTLPExporterBrowserBase.ts#L59

Once we accept fetch as an HTTP request sender, it can also be used in Node.js environments.

Describe the solution you'd like

  • Option 1: Add "fetch mode" to OTLPExporterBrowserBase
  • Option 2: Add a different class like OTLPExporterFetch

Describe alternatives you've considered

Additional context

@david-luna
Copy link
Contributor

Hi @lacolaco

I would like to understand better your use case. Do you want to instrument a WebWorker/ServiceWorker that runs on the Browser? If so, do you also want to instrument the UI thread and correlation between traces form the UI and the worker?

@lacolaco
Copy link
Author

@david-luna I have two use cases; ServiceWorker on the browser and Cloudflare Workers. ServiceWorker needs to be able to propagate traces from UI threads to and from the backend server. This is equivalent to a proxy server from an overall perspective. The Cloudflare Worker is simply instrumentation as a back-end server.

@david-luna
Copy link
Contributor

I'm not familiar with Cloudfare Workers but regarding Service Workers I think sending traces with fetch is not enough to have the proper instrumentation.

At least instrumentation of FetchEvent needs to be implemented so context is properly propagated when creating spans within the SW. Also I think there might be necesary to review core and web-sdk packages to make sure they are not using APIs not available in workers.

@lacolaco
Copy link
Author

To clarify in case there is some misunderstanding, I am referring to the implementation of the exporter using the fetch api, not instrumentation for the fetch api.

@david-luna
Copy link
Contributor

okay, so I guess you will do manual instrumentation of the incoming requests to the Service Worker to keep the trace context from the UI. I'll share today with the JavaScript SIG or maybe you can join and discuss this topic.

@lacolaco
Copy link
Author

Thank you for the escalating!

@david-luna
Copy link
Contributor

Checking on the agenda notes there is a discussion about this and also a PR. I'm adding the links here to complete the context.
Discussion: #3845
PR: #3542

@david-luna
Copy link
Contributor

@lacolaco

We did discuss adding this feature and definitely is a feature we want in OTEL. PR #3542 it's quite complex but it will get simpler once #4542 lands into the main branch. So the plan would be:

@lacolaco
Copy link
Author

@david-luna Sounds nice! Thank you all for the great job!

@pichlermarc
Copy link
Member

pichlermarc commented Apr 26, 2024

@lacolaco

We did discuss adding this feature and definitely is a feature we want in OTEL. PR #3542 it's quite complex but it will get simpler once #4542 lands into the main branch. So the plan would be:

* merge [feat!: move serialization to `@opentelemetry/otlp-transformer` #4542](https://github.com/open-telemetry/opentelemetry-js/pull/4542)

* update [feat(otlp-exporter-base): Add fetch sender for ServiceWorker environment #3542](https://github.com/open-telemetry/opentelemetry-js/pull/3542) to use the new exporter API

* review and merge [feat(otlp-exporter-base): Add fetch sender for ServiceWorker environment #3542](https://github.com/open-telemetry/opentelemetry-js/pull/3542)

* wait for the next release

Thank you @david-luna for summarizing this and bringing it up in the SIG.

To expand on what David said, there are a few more steps to be able to merge #3542 - we'll need to re-structure how we handle transports in the OTLP exporters (see #4116 for a more in-depth reasoning on why this is necessary). I have a draft for this at #4415, but applying that'll be a multi-step process as we try to avoid breaking users (the exporters are quite heavily used, so we try to tread lightly with changes there).

After all these changes adding fetch as a transport should be as easy as implementing one Transport interface and adding it to the existing code, which will also make it a component that we can test individually.

@danielwgetvim
Copy link

hi

this is very much needed!

Copy link

This issue 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.

@jgibo
Copy link

jgibo commented Nov 16, 2024

Looking forward to when this is supported here in the upstream, much love to all the awesome contributors spending time and effort to prepare the foundations for this change!

In the meantime, I've forked @sugi working change in #3542 to https://github.com/jgibo/opentelemetry-js/tree/service-worker-support.

And from my fork I've published the changes to npm as package https://www.npmjs.com/package/@jgibo/exporter-trace-otlp-http

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

No branches or pull requests

5 participants