From 9f34b1e495d32fb066632b1b40040dced1e17ec6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Kie=C5=82kowicz?= Date: Thu, 26 Sep 2024 12:30:26 +0200 Subject: [PATCH] Fix MongoDB instrumentation for 2.29.0+ --- .../Loader.NetFramework.cs | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/OpenTelemetry.AutoInstrumentation.Loader/Loader.NetFramework.cs b/src/OpenTelemetry.AutoInstrumentation.Loader/Loader.NetFramework.cs index 5016955aa2..85b57fc019 100644 --- a/src/OpenTelemetry.AutoInstrumentation.Loader/Loader.NetFramework.cs +++ b/src/OpenTelemetry.AutoInstrumentation.Loader/Loader.NetFramework.cs @@ -33,7 +33,30 @@ private static string ResolveManagedProfilerDirectory() return null; } - Logger.Debug("Requester [{0}] requested [{1}]", args?.RequestingAssembly?.FullName ?? "", args?.Name ?? ""); + Logger.Debug("Requester [{0}] requested [{1}]", args.RequestingAssembly?.FullName ?? "", args.Name ?? ""); + + // All MongoDB* are signed and does not follow https://learn.microsoft.com/en-us/dotnet/standard/library-guidance/versioning#assembly-version + // There is no possibility to automatically redirect from 2.28.0 to 2.29.0. + // Loading assembly and ignoring this version. + if (assemblyName.StartsWith("MongoDB", StringComparison.OrdinalIgnoreCase) && + (string.Equals(assemblyName, "MongoDB.Driver.Core", StringComparison.OrdinalIgnoreCase) || + string.Equals(assemblyName, "MongoDB.Bson", StringComparison.OrdinalIgnoreCase) || + string.Equals(assemblyName, "MongoDB.Libmongocrypt", StringComparison.OrdinalIgnoreCase))) + { + try + { + var mongoAssembly = Assembly.Load(assemblyName); + Logger.Debug("Assembly.Load(\"{0}\") succeeded={1}", assemblyName, mongoAssembly != null); + return mongoAssembly; + } + catch (Exception ex) + { + Logger.Debug(ex, "Assembly.Load(\"{0}\") Exception: {1}", assemblyName, ex.Message); + } + + return null; + } + var path = Path.Combine(ManagedProfilerDirectory, $"{assemblyName}.dll"); if (File.Exists(path)) {