-
Notifications
You must be signed in to change notification settings - Fork 821
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
feat: expose withSpanAsync on NodeTracer #752 #1011
Conversation
0e3d36c
to
b794a08
Compare
b794a08
to
3308204
Compare
@dyladan I've changed the return type of I don't think we should have the behavior to return the value returned by the callback too, the aim of the API isn't to pass value but to set a context, WDYT ? |
3308204
to
07e7843
Compare
I think it is a very common use case that you want the return value of the called function. Think something like this should work? <T extends () => any, U = ReturnType<T> extends Promise<infer V> ? V : ReturnType<T>>(context: Context, fn: T) => Promise<U>; Or you could factor out the confusing typing type Unpacked<T> = T extends Promise<infer U>
? U
: T;
class Foo {
async withAsync<T extends () => any, U = Unpacked<ReturnType<T>>>(context: Context, fn: T): Promise<U> {
return fn();
}
} edit: you could force the function to be async too like this: type Unpacked<T> = T extends Promise<infer U>
? U
: T;
class Foo {
async withAsync<T extends () => Promise<any>, U = Unpacked<ReturnType<T>>>(context: Context, fn: T): Promise<U> {
return fn();
}
} |
|
||
#### withSpanAsync | ||
|
||
The `NodeTracer` expose a method called `withSpanAsync` that allows you to set a current span in the context of a `async` function. This previously was a problem with `withSpan` because its synchronous behavior, read about that in [this issue](https://github.com/open-telemetry/opentelemetry-js/issues/752). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this functionality be integrated into the existing withSpan
method by detecting if a promise has been passed back? Not sure if I missed the discussion around this while catching up, but the ergonomics would be better if you didn't have to change the method call when you swap a callback in withSpan
to an async function.
You should be able to call fn
, and clear the context in a .finally
(or then/catch
pair) off the return value. I would imagine the context propagation for the interstitial promises could be handled by async_hooks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There has been a lot of discussion around this and we decided to keep them separate for now. This decision came down to performance impact and the higher likelihood for subtle bugs using the more complex async mechanism.
07e7843
to
9e66e40
Compare
Codecov Report
@@ Coverage Diff @@
## master #1011 +/- ##
===========================================
- Coverage 94.98% 73.88% -21.10%
===========================================
Files 238 71 -167
Lines 9085 1570 -7515
Branches 822 271 -551
===========================================
- Hits 8629 1160 -7469
+ Misses 456 410 -46
|
I've rebased on #1099 to run tests with it (and it works). waiting for it to be merged to set this one as ready for review |
As a solution has been to fix |
This doens't work because of some bug inside the AsyncHookContextManager but i'm opening the PR in the mean time to get feedback on how we can accomplish this.
The main issue is that i need to use// @ts-ignore
there since thewithAsync
is not exposed at the api level, don't know if we have other solutions hereWDYT ?