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

Use web-time on Wasm #2758

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Use web-time on Wasm #2758

wants to merge 1 commit into from

Conversation

daxpedda
Copy link
Contributor

@daxpedda daxpedda commented Oct 13, 2023

Motivation

Currently Uptime, SystemTime and spans panic on browsers, this is because std::time is not supported in browsers.

Even external layers, like tracing-web, are currently not able to circumvent the issue with spans.

Solution

The solution is very straightforward: not using std::time when on Web. This PR uses web-time instead.
This will only be enabled when on Wasm and when enabling the wasm-bindgen crate feature, otherwise no dependencies are added.

Alternatively I could import some of the necessary code from web-time if this is more desirable.

I originally wanted to fix some of these issues in #2533, but it turned out that instant wasn't as accurate a drop-in replacement as I expected, in addition to the bugs and missing features/improvements, which was the original motivation to make web-time.


Partly addresses #642.
Fixes #2720.

@daxpedda
Copy link
Contributor Author

Rebased on master.

Copy link
Member

@hawkw hawkw left a comment

Choose a reason for hiding this comment

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

Overall, I think this change seems good to me! I have some questions, but once those are addressed, I think we can probably move forwards with this. In particular:

  • I wasn't sure about the feature flagging, which I left some inline comments on.
  • Would it be possible to add some kind of tests for the webassembly support for fmt::Layer? In other crates, we are able to run some tests for wasm targets using wasm-bindgen-test; is that possible here?

@@ -32,6 +32,7 @@ fmt = ["registry", "std"]
ansi = ["fmt", "nu-ansi-term"]
registry = ["sharded-slab", "thread_local", "std"]
json = ["tracing-serde", "serde", "serde_json"]
wasm-bindgen = ["web-time"]
Copy link
Member

Choose a reason for hiding this comment

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

is this necessary as a feature flag, or can we just make the dependency always be used if fmt is enabled and we're on a WASM target? Are there reasons that a user might want to compile for a WASM target without WASI, but still disable this feature?

Also, this feature flag should probably not introduce the additional dependency unless the fmt feature is also enabled.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

is this necessary as a feature flag, or can we just make the dependency always be used if fmt is enabled and we're on a WASM target?

We can.

Are there reasons that a user might want to compile for a WASM target without WASI, but still disable this feature?

Yes, they might be using the wasm32-unknown-unknown target but not targeting the Web. Spans won't be working for them but they might still be using tracing-subscriber (as were Wasm on Web users until now). Though this is probably niche.

Also, this feature flag should probably not introduce the additional dependency unless the fmt feature is also enabled.

I'm not aware of a way to do this. But if it's decided to enable it by default anyway we could just add it to the fmt crate feature.


I know that there are no-std Wasm users out there that can't rely on wasm-bindgen, but I don't know if they are using tracing-subscriber or not. It's also possible that a new Web backend gets popular, besides wasm-bindgen, in which case it would be useful to have separate crate features, though I'm not aware of any current development in that area.

Let me know how you want to proceed.

Comment on lines +77 to +78
//! - `wasm-bindgen`: Uses the [`web-time`] crate to allow `Uptime` and `SystemTime` to
//! be used in browsers.
Copy link
Member

Choose a reason for hiding this comment

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

it seems a bit strange to me that the feature flag is wasm-bindgen rather than the actual name of the optional dependency (web-time)...is this a standard practice for feature flag naming in the wasm ecosystem?

Copy link
Member

Choose a reason for hiding this comment

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

On the other hand, I suppose if we want to add additional WASM support, such as using Console.log with more structured values when running in the browser, we may want to have one big feature flag for that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it seems a bit strange to me that the feature flag is wasm-bindgen rather than the actual name of the optional dependency (web-time)...is this a standard practice for feature flag naming in the wasm ecosystem?

This was done in the past when stdweb was still a thing, because there were two different Wasm Web backends: wasm-bindgen and stdweb.

But yes, the name refers to the Wasm Web backend.

@daxpedda
Copy link
Contributor Author

  • Would it be possible to add some kind of tests for the webassembly support for fmt::Layer? In other crates, we are able to run some tests for wasm targets using wasm-bindgen-test; is that possible here?

It is possible, I briefly looked into it, but it didn't make much sense to me because fmt::Layer actually doesn't work with this PR on Web yet, this only fixes it crashing for external Layer implementations.

I found some tests that test Uptime directly:

#[test]
fn impls() {
let f = Format::default().with_timer(time::Uptime::default());
let subscriber = Collector::builder().event_format(f).finish();
let _dispatch = Dispatch::new(subscriber);
let f = format::Format::default();
let subscriber = Collector::builder().event_format(f).finish();
let _dispatch = Dispatch::new(subscriber);
let f = format::Format::default().compact();
let subscriber = Collector::builder().event_format(f).finish();
let _dispatch = Dispatch::new(subscriber);
}

#[test]
fn impls() {
let f = Format::default().with_timer(time::Uptime::default());
let fmt = fmt::Subscriber::default().event_format(f);
let subscriber = fmt.with_collector(Registry::default());
let _dispatch = Dispatch::new(subscriber);
let f = format::Format::default();
let fmt = fmt::Subscriber::default().event_format(f);
let subscriber = fmt.with_collector(Registry::default());
let _dispatch = Dispatch::new(subscriber);
let f = format::Format::default().compact();
let fmt = fmt::Subscriber::default().event_format(f);
let subscriber = fmt.with_collector(Registry::default());
let _dispatch = Dispatch::new(subscriber);
}

I can look into hooking those up?

@daxpedda
Copy link
Contributor Author

daxpedda commented Nov 9, 2023

Rebased on master, CI currently fails with the same issue as on master.
@hawkw friendly ping.

@daxpedda
Copy link
Contributor Author

Rebased on master after #2814, so now CI is green.

@daxpedda
Copy link
Contributor Author

daxpedda commented Feb 7, 2024

Updated to web-time v1 and rebased on master.
@hawkw another friendly ping!

@istankovic
Copy link

And another ping. :-)

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.

tracing-subscriber: panic in wasm32-unknown-unknown calculating span durations (in chrome)
3 participants