Skip to content

Conversation

@xiang17
Copy link
Contributor

@xiang17 xiang17 commented Feb 9, 2023

Why

Fixes a special case that #1692 didn't cover.

If the customer included the OpenTelemetry SDK with PackageReference in the csproj file but didn't have C# code calling OpenTelemetry APIs, the app will crash with a System.Reflection.TargetInvocationException which doesn't have the helpful System.NotSupportedException.

This is because Assembly.GetEntryAssembly()?.GetReferencedAssemblies()'s return value doesn't contain OpenTelemetry assembly unless it's invoked in C# code.

What

Rely on Type.GetType to check whether customer's app has dependency on OpenTelemetry.

Tests

Case 1

Add <PackageReference Include="OpenTelemetry" VersionOverride="1.4.0-rc.2" /> in the <ItemGroup> tag of examples\playground\AspNetCoreMvc\Examples.AspNetCoreMvc.csproj.

Unhandled exception. System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
 ---> System.NotSupportedException: Application has direct or indirect reference to older version of OpenTelemetry package 1.4.0.753.
   at StartupHook.ThrowIfReferenceIncorrectOpenTelemetryVersion(String loaderAssemblyLocation) in C:\c\opentelemetry-dotnet-instrumentation\src\OpenTelemetry.AutoInstrumentation.StartupHook\StartupHook.cs:line 201
   at StartupHook.Initialize() in C:\c\opentelemetry-dotnet-instrumentation\src\OpenTelemetry.AutoInstrumentation.StartupHook\StartupHook.cs:line 70
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
   at System.Reflection.MethodInvoker.Invoke(Object obj, IntPtr* args, BindingFlags invokeAttr)
   --- End of inner exception stack trace ---
   at System.Reflection.MethodInvoker.Invoke(Object obj, IntPtr* args, BindingFlags invokeAttr)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.StartupHookProvider.CallStartupHook(StartupHookNameOrPath startupHook)
   at System.StartupHookProvider.ProcessStartupHooks()

Case 2

examples\playground\AspNetCoreMvc\Examples.AspNetCoreMvc.csproj works fine.

Checklist

- [ ] CHANGELOG.md is updated.
- [ ] Documentation is updated.
- [ ] New features are covered by tests.

@xiang17 xiang17 requested a review from a team February 9, 2023 01:59
@Kielek Kielek merged commit 82f0b26 into open-telemetry:main Feb 9, 2023
@xiang17 xiang17 deleted the xiang17/FixOTelVersionValidateBug branch February 9, 2023 20:43
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.

3 participants