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

chore: Warn about unsupported async return types #2565

Merged
merged 2 commits into from
Jun 20, 2024
Merged

Conversation

chynesNR
Copy link
Member

As per our documentation, we only support instrumenting async methods that return a Task. If we find another return type, log a Warning. This should make it easier to track down problems caused by using e.g. ValueTask. The logging will only occur once since the Wrapper is cached.

Sample line:

2024-06-18 20:12:10,856 NewRelic   WARN: [pid: 37628, tid: 24] Instrumenting async methods that return a type other than Task or Task<> is not supported and may result in inconsistent data. 'MyAsyncMethod' has a return type of 'ValueTask'.

Copy link
Member

@tippmar-nr tippmar-nr left a comment

Choose a reason for hiding this comment

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

Nice solution!

@nrcventura
Copy link
Member

This solution seems like a good one. Chris and I talked through some things that I think makes sense to call out more broadly.

  • This approach will warn when trying to instrument async void, async ValueTask, and async ValueTask<T> return types.
  • This approach will cover new instrumentation, custom instrumentation, and attribute-based instrumentation because all instrumentation is routed through that class.
  • The reflection is necessary because we do not have access to the return type of the method until the method completes.

Chris is going to take a look at an alternative approach in the Delegates class where we already have code that checks if the return type is a Task, to see which implementation may be better.

@chynesNR
Copy link
Member Author

This solution seems like a good one. Chris and I talked through some things that I think makes sense to call out more broadly.

  • This approach will warn when trying to instrument async void, async ValueTask, and async ValueTask<T> return types.
  • This approach will cover new instrumentation, custom instrumentation, and attribute-based instrumentation because all instrumentation is routed through that class.
  • The reflection is necessary because we do not have access to the return type of the method until the method completes.

Chris is going to take a look at an alternative approach in the Delegates class where we already have code that checks if the return type is a Task, to see which implementation may be better.

After talking it over with Chris and looking at the code together, we think the current solution is probably the less-impactful one. We do have access to the return object (and therefore its type) in the afterWrappedMethodDelegate created by Delegates.GetDelegateFor<Task>, but while this wouldn't incur the reflection cost, it would run after every invocation of the method. That feels like it will be more expensive in the long run.

@chynesNR chynesNR merged commit 68ed7ad into main Jun 20, 2024
93 checks passed
@chynesNR chynesNR deleted the chore/warn-valuetask branch June 20, 2024 16:38
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.

None yet

3 participants