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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/Agent/NewRelic/Agent/Core/Wrapper/WrapperService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
using NewRelic.Agent.Core.Utilities;
using NewRelic.Agent.Extensions.Logging;
using NewRelic.Agent.Api;
using System.Reflection;
using System.Threading.Tasks;

namespace NewRelic.Agent.Core.Wrapper
{
Expand Down Expand Up @@ -88,6 +90,23 @@ public AfterWrappedMethodDelegate BeforeWrappedMethod(Type type, string methodNa
return null;
}

if (isAsync)
{
try
{
var returnType = type.GetMethod(methodName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static).ReturnType;
if ((returnType != typeof(Task)) &&
(!returnType.IsGenericType || (returnType.GetGenericTypeDefinition() != typeof(Task<>))))
{
Log.Warn("Instrumenting async methods that return a type other than Task or Task<> is not supported and may result in inconsistent data. '{0}' has a return type of '{1}'.", methodName, returnType?.Name);
}
}
catch
{
// Since this is just for logging purposes it doesn't matter if it fails
}
}

_functionIdToWrapper[functionId] = new InstrumentedMethodInfoWrapper(instrumentedMethodInfo, trackedWrapper);
GenerateLibraryVersionSupportabilityMetric(instrumentedMethodInfo);
}
Expand Down
57 changes: 57 additions & 0 deletions tests/Agent/UnitTests/Core.UnitTest/Wrapper/WrapperService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
using System.Collections.Generic;
using NewRelic.Agent.Core.Utilities;
using NewRelic.Agent.Api;
using System.Threading.Tasks;

namespace NewRelic.Agent.Core.Wrapper
{
[TestFixture]
public class Class_WrapperService
{
private const uint EmptyTracerArgs = 0;
private const uint AsyncTracerArgs = 1 << 23;

private WrapperService _wrapperService;

Expand Down Expand Up @@ -279,5 +281,60 @@ public void BeforeWrappedMethod_ReturnsNoOp_IfTheCurrentSegmentIsLeaf()
});
}
}

private class ValueTaskTestClass
{
public async ValueTask<int> SomeAsyncWork()
{
await Task.Delay(1);
return 0;
}
}

private class TaskTestClass
{
public async Task<int> SomeOtherAsyncWork()
{
await Task.Delay(1);
return 0;
}
}

[Test]
public void BeforeWrappedMethod_WarningAboutValueTask()
{
Mock.Arrange(() => _wrapperMap.Get(Arg.IsAny<InstrumentedMethodInfo>())).Returns(new TrackedWrapper(_transactionRequiredWrapper));

var type = typeof(ValueTaskTestClass);
const string methodName = "SomeAsyncWork";
const string tracerFactoryName = "MyTracer";
var target = new object();
var arguments = new object[0];

using (var logging = new TestUtilities.Logging())
{
var afterWrappedMethod = _wrapperService.BeforeWrappedMethod(type, methodName, string.Empty, target, arguments, tracerFactoryName, null, AsyncTracerArgs, 0);

Assert.That(logging.HasMessageThatContains("is not supported"), Is.True);
}
}
[Test]
public void BeforeWrappedMethod_NoWarningAboutTask()
{
Mock.Arrange(() => _wrapperMap.Get(Arg.IsAny<InstrumentedMethodInfo>())).Returns(new TrackedWrapper(_transactionRequiredWrapper));

var type = typeof(TaskTestClass);
const string methodName = "SomeOtherAsyncWork";
const string tracerFactoryName = "MyTracer";
var target = new object();
var arguments = new object[0];

using (var logging = new TestUtilities.Logging())
{
var afterWrappedMethod = _wrapperService.BeforeWrappedMethod(type, methodName, string.Empty, target, arguments, tracerFactoryName, null, AsyncTracerArgs, 0);

Assert.That(logging.HasMessageThatContains("is not supported"), Is.False);
}
}
}
}
Loading