Skip to content

Commit c0e09c6

Browse files
committed
Fix cases when OpenTelemetry.AutoInstrumentation methods are not inlined by the compiler
1 parent 98037a0 commit c0e09c6

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

src/OpenTelemetry.AutoInstrumentation/Instrumentations/NoCode/NoCodeIntegrationHelper.cs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System.Diagnostics;
55
using System.Reflection;
6+
using System.Runtime.CompilerServices;
67
using OpenTelemetry.AutoInstrumentation.CallTarget;
78
using OpenTelemetry.AutoInstrumentation.Configurations;
89
using OpenTelemetry.AutoInstrumentation.Util;
@@ -19,9 +20,7 @@ internal static class NoCodeIntegrationHelper
1920

2021
internal static CallTargetState OnMethodBegin()
2122
{
22-
const int methodNameFrameIndex = 3;
23-
24-
var method = new StackFrame(methodNameFrameIndex).GetMethod();
23+
var method = GetFirstNonOtelAutoMethod();
2524
var methodName = method?.Name;
2625
var typeName = method?.DeclaringType?.FullName;
2726
var assemblyName = method?.DeclaringType?.Assembly.GetName().Name;
@@ -154,4 +153,23 @@ private static string GetParameterTypeNameDefinition(ParameterInfo parameterInfo
154153
? GenericParameterMethodNames[genericParameterPosition]
155154
: GenericParameterClassNames[genericParameterPosition];
156155
}
156+
157+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
158+
private static MethodBase? GetFirstNonOtelAutoMethod()
159+
{
160+
// Typically, the first method outside OpenTelemetry.AutoInstrumentation assembly is at skipFrames = 3
161+
// For some cases, compiler does not inline all OpenTelemetry.AutoInstrumentation methods, so we check up to skipFrames = 10
162+
163+
for (var skipFrames = 3; skipFrames < 10; skipFrames++)
164+
{
165+
var method = new StackFrame(skipFrames).GetMethod();
166+
var assemblyName = method?.DeclaringType?.Assembly.GetName().Name;
167+
if (assemblyName != null && !assemblyName.Equals("OpenTelemetry.AutoInstrumentation", StringComparison.Ordinal))
168+
{
169+
return method;
170+
}
171+
}
172+
173+
return null;
174+
}
157175
}

0 commit comments

Comments
 (0)