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

WASM Support #57

Merged
merged 1 commit into from
Aug 30, 2023
Merged

WASM Support #57

merged 1 commit into from
Aug 30, 2023

Conversation

jesseditson
Copy link
Contributor

  • add a time module at the root with a now implementation that is replaced with a js_sys call when compiling for wasm32
  • use Instant from web_time when targeting wasm32
  • add js_sys and web_time dependencies when targeting wasm32

Motivation

I was attempting to use this library with a wasm target in a browser. If you call std::time::{Instant, SystemTime}::now in a wasm target, it will panic at runtime. This library makes use of both ::now() calls, which will result in a panic when attempting to record a trace.

Solution

In the library that this library calls (opentelemetry-api), this is resolved by replacing calls to SystemTime::now() with an implementation that returns a SystemTime constructed from js_sys::Date. To preserve the ability to pass SystemTime-typed args to opentelemetry_api methods, I just added the same mechanism to this library.

Because Instant is never used in opentelemetry_api, we can instead replace the implementation entirely with the api-compatible "polyfill" from web_time.

All of these changes are behind cfg and automatically enabled when building for wasm32 targets, so this will not change any behavior/performance when built for other targets.

- add a time module at the root with a now implementation that is replaced with a js_sys call when compiling for wasm32
- use Instant from web_time when targeting wasm32
- add js_sys and web_time dependencies when targeting wasm32
@jesseditson jesseditson requested a review from jtescher as a code owner August 29, 2023 23:31
Copy link
Collaborator

@jtescher jtescher 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, thanks @jesseditson

@jtescher jtescher merged commit 80ae321 into tokio-rs:v0.1.x Aug 30, 2023
jtescher added a commit that referenced this pull request Nov 7, 2023
### Breaking Changes

- Upgrade to `v0.21.0` of `opentelemetry` For list of breaking changes
in OpenTelemetry, see the [v0.21.0
changelog](https://github.com/open-telemetry/opentelemetry-rust/blob/v0.21.0/opentelemetry/CHANGELOG.md).
- Update MSRV to require Rust 1.65+, as `opentelemetry` requires it now.
(#68)

### Fixed

- WASM Support (#57)
- Fix potential deadlock (#59)
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.

2 participants