33
44using System . Diagnostics ;
55using System . Reflection ;
6+ using System . Runtime . CompilerServices ;
67using OpenTelemetry . AutoInstrumentation . CallTarget ;
78using OpenTelemetry . AutoInstrumentation . Configurations ;
89using 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