Skip to content

Commit bec0b10

Browse files
committed
refactor RedisBatch to use new api for emitting il
1 parent 6be1a5e commit bec0b10

File tree

1 file changed

+25
-0
lines changed
  • src/Datadog.Trace.ClrProfiler.Managed/Integrations/StackExchange.Redis

1 file changed

+25
-0
lines changed

src/Datadog.Trace.ClrProfiler.Managed/Integrations/StackExchange.Redis/RedisBatch.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
2+
using System.Reflection;
23
using System.Threading.Tasks;
34
using Datadog.Trace.ClrProfiler.Emit;
5+
using Datadog.Trace.Logging;
46

57
namespace Datadog.Trace.ClrProfiler.Integrations.StackExchange.Redis
68
{
@@ -16,6 +18,8 @@ public static class RedisBatch
1618
private const string Major1 = "1";
1719
private const string Major2 = "2";
1820

21+
private static readonly ILog Log = LogProvider.GetLogger(typeof(RedisBatch));
22+
1923
/// <summary>
2024
/// Execute an asynchronous redis operation.
2125
/// </summary>
@@ -69,6 +73,27 @@ private static async Task<T> ExecuteAsyncInternal<T>(object redisBase, object me
6973
var processorType = asm.GetType("StackExchange.Redis.ResultProcessor`1").MakeGenericType(genericType);
7074
var serverType = asm.GetType("StackExchange.Redis.ServerEndPoint");
7175

76+
Action<object, object, object, object> instrumentedMethod = null;
77+
78+
instrumentedMethod =
79+
MethodBuilder<Action<object, object, object, object>>
80+
.Start(Assembly.GetCallingAssembly(), mdToken, opCode, nameof(BeforeAction))
81+
.WithConcreteTypeName(DiagnosticSource)
82+
.WithParameters(diagnosticSource, actionDescriptor, httpContext, routeData)
83+
.Build();
84+
85+
try
86+
{
87+
// call the original method, catching and rethrowing any unhandled exceptions
88+
instrumentedMethod?.Invoke(diagnosticSource, actionDescriptor, httpContext, routeData);
89+
}
90+
catch (Exception ex)
91+
{
92+
// profiled app will continue working as expected without this method
93+
Log.Error($"Exception when calling {methodDef}.", ex);
94+
throw;
95+
}
96+
7297
var originalMethod = Emit.DynamicMethodBuilder<Func<object, object, object, object, Task<T>>>
7398
.CreateMethodCallDelegate(
7499
thisType,

0 commit comments

Comments
 (0)