-
Notifications
You must be signed in to change notification settings - Fork 755
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
#[instrument] - must refer to _self instead of self when instrumenting an async-trait instance method #864
Comments
Yup, this is because We're already detecting when |
@nightmared do you have time to take a look at fixing this? Otherwise, I will hopefully get to it eventually. |
After taking a quick look, I believe it's not very easy to do, as I will try to think a little more about it (I started taking a tiny look at it, mainly adding a test for this behavior, master...nightmared:self-instrument-fields) when I have some time (this week-end ?). |
I think we want to do this one. I imagine we should be able to use It looks like |
Nice, I didn't know about The main concern is while this allows to support the following code: #[instrument(fields(val=self.foo(), test=%v+5))]
async fn foo(&mut self, v: usize) {}
#[instrument(fields(Self=std::any::type_name::<Self>()))]
async fn call_with_self(&self) {} This one won't work: #[instrument(fields(Self=std::any::type_name::<Self>()))]
async fn call() {} Because I'm getting the type __TRACING_SELF_<RANDOM_VALUE> = Self;
<Current implementation but where Self is replaced in fields by __TRACING_SELF_<RANDOM_VALUE>> (the random value (could also be a monotonic unique counter) is used to prevent name redefinitions if multiple async functions are annotated in the same impl block) Do you want me to open a PR or do you think we need to go in another direction in order to achieve this ? |
Hmm...I guess I think the approach you proposed with a type alias is perhaps the best way to make this work, even though it's pretty ugly.
I wouldn't want to rely on a random value for this, since there is the possibility of collisions...and then the proc macro depends on the type __Tracing_Self_<LINE NUMBER> = Self; that should be unique within the same impl block, without relying on a random ID. |
I answered in the PR (#875). I'm a bit saddened, as I can't really think of a way to extract that information :( |
#875) ## Motivation Fixes #864. ## Solution Visit the fields and replace 'self' with '_self', and 'Self' with the type of the impl (when we can acces it, see #864 (comment) for current limitations).
Bug Report
Version
tracing v0.1.16
tracing-attributes v0.1.9
tracing-core v0.1.11
Platform
n/a
Description
I have a struct
SomeStruct
with one important method that I care about when tracing/logging:I need to implement an
async-trait
method that needs instrumentation. In this case, since it's a method, the first argument isself
. I want to useinstrument
on this method.I cannot refer to
self
when settingfields()
in the#[instrument()]
proc macro. Theinstrument
macro re-writesself
as_self
(and then back again, I think).I can successfully use
_self.get_value()
, but this is a bit weird looking as it's not immediately obvious what_self
is.For an example, the code below currently works:
It would be nice to be able to refer to
self
instead of_self
, if at all possible.cheers bigears x
The text was updated successfully, but these errors were encountered: