-
-
Notifications
You must be signed in to change notification settings - Fork 154
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
Provide examples for async use of Hub
#507
Comments
Hi! Yes the support request came through me as well. I understand this is a confusing topic, as the ecosystem itself exposes some non-obvious footguns around this concept. The upstream tracing docs also have a related warning here: https://docs.rs/tracing/latest/tracing/span/struct.Span.html#in-asynchronous-code Either way, there are two general patterns here: If you spawn tasks, or use futures::join concurrency, you have to always create a new hub ( As those tasks can run concurrently (or in parallel on multiple threads), they need their own copy of the Hub/Scope so they don’t interfere with each other. The only exception from this rule is if you immediate As these are indeed footguns and hard concepts to teach, I would appreciate feedback on how we could improve the docs in this regard. From your perspective, is the explanation above sufficient? Where should these things be explained? The main |
One other time I found I needed to create a new hub was when processing a stream in parallel: let hub = Hub::current();
let x: Vec<_> = stream::iter(inputs)
.map(|input| {
async move { Self::do_something(input, pool).await }
.bind_hub(Hub::new_from_top(&hub))
})
.buffered(10)
.try_collect()
.await?;
Ah, interesting—I didn't realize that awaiting a spawned task would run it within the context of the current future.
I think the explanation above is great. Adding it to the Hub docs makes sense to me, although I think additionally pointing to that from somewhere more prominent (front page of the docs?) would be helpful too. "If you're writing async or threaded code, see the Hub documentation"? Maybe I should have read the docs more thoroughly to start with, but it took me a long time to figure out that the Hub docs were where I should be looking, because I hadn't actually used Hubs at all manually, since the tracing and tower integrations had been sufficient for everything else I needed to do. Thanks again! |
Actually, I said the wrong thing here :-D You do have to bind a hub, but you can bind the current hub, instead of a new one. I will try to add some more docs to the hub overview and main page. |
Well yes, because parallelism / concurrency means the tasks are running independently. |
#509 is a try at improving the docs around this with some examples. |
Hi @Swatinem, sorry to "hijack" this thread but I think I'm still using Specifically we encounter this with websocket handlers in Axum — the server itself follows the example to use the In the websocket handling method, I try to create a new hub and bind it to the future that handles the websocket upgrade:
The first time something errors inside As far as I can tell, this follows the examples you've provided above and in the docs, so any help would be appreciated! |
I assume that is for every new request that goes through the websocket upgrade? |
I recently had an issue (events/spans being recorded in the wrong places) which was caused by me not understanding that I needed to be manually cloning/creating
Hub
s and usingbind_hub
on futures which I was joining/spawning/etc. (See support request 72484 if you're interested). The docs do explain that, but:sentry-tracing
would deal with that for me, since it already has the concepts of spans which work with futuresI think it'd be helpful to provide an example in the repo of manually using
bind_hub
, and I think it might be worth linking to something about working with async code more prominently (the front page of the docs)?I'm also not totally clear on exactly how to manually deal with
Hub
s. Should I be usingHub::new_from_top
vsHub::new
, or just reusingHub::current
? (I don't understand what "the top scope of another hub" means in the docs fornew_from_top
.)Hub::new_from_top(Hub::current())
seems to be working the way I'd expect so far. 🤷🏻Anyway, thanks for the great SDK. I'm very happy with how seamless it's been to use (especially with the tower and tracing integrations). Just thought I'd report the troubles I ran into.
The text was updated successfully, but these errors were encountered: