diff --git a/src/Elastic.OpenTelemetry.Core/Diagnostics/LoggerMessages.cs b/src/Elastic.OpenTelemetry.Core/Diagnostics/LoggerMessages.cs index 6f2b0ab4..1f75b50c 100644 --- a/src/Elastic.OpenTelemetry.Core/Diagnostics/LoggerMessages.cs +++ b/src/Elastic.OpenTelemetry.Core/Diagnostics/LoggerMessages.cs @@ -98,8 +98,8 @@ internal static partial class LoggerMessages "{ProviderBuilderType} (instance:{BuilderInstanceIdentifier}) via reflection.")] public static partial void LogAddedInstrumentationViaReflection(this ILogger logger, string instrumentationName, string providerBuilderType, string builderInstanceIdentifier); - [LoggerMessage(EventId = 35, EventName = "UnableToFindTypeViaReflection", Level = LogLevel.Warning, Message = "Unable to find {FullyQualifiedTypeName} in {AssemblyFullName}.")] - public static partial void LogUnableToFindTypeWarning(this ILogger logger, string fullyQualifiedTypeName, string assemblyFullName); + [LoggerMessage(EventId = 35, EventName = "UnableToFindTypeViaReflection", Level = LogLevel.Debug, Message = "Unable to find {FullyQualifiedTypeName} in {AssemblyFullName}.")] + public static partial void LogUnableToFindType(this ILogger logger, string fullyQualifiedTypeName, string assemblyFullName); [LoggerMessage(EventId = 36, EventName = "UnableToFindMethodViaReflection", Level = LogLevel.Warning, Message = "Unable to find the {FullyQualifiedTypeName}.{MethodName} extension " + "method in {AssemblyFullName}.")] @@ -113,6 +113,10 @@ internal static partial class LoggerMessages "{ProviderBuilderType} (instance:{BuilderInstanceId}) because it is disabled in configuration.")] public static partial void LogSkippingAssemblyScanning(this ILogger logger, string providerBuilderType, string builderInstanceId); + [LoggerMessage(EventId = 39, EventName = "AddedResourceDetectorViaReflection", Level = LogLevel.Debug, Message = "Added contrib resource detector '{InstrumentationName}' to " + + "{ProviderBuilderType} (instance:{BuilderInstanceIdentifier}) via reflection.")] + public static partial void LogAddedResourceDetectorViaReflection(this ILogger logger, string instrumentationName, string providerBuilderType, string builderInstanceIdentifier); + [LoggerMessage(EventId = 40, EventName = "ProcessorAdded", Level = LogLevel.Debug, Message = "Added '{ProcessorTypeName}' processor to TracerProviderBuilder (instance:{BuilderInstanceId}).")] diff --git a/src/Elastic.OpenTelemetry.Core/Instrumentation/ContribResourceDetectors.cs b/src/Elastic.OpenTelemetry.Core/Instrumentation/ContribResourceDetectors.cs index 4e1a818b..71a8b740 100644 --- a/src/Elastic.OpenTelemetry.Core/Instrumentation/ContribResourceDetectors.cs +++ b/src/Elastic.OpenTelemetry.Core/Instrumentation/ContribResourceDetectors.cs @@ -23,7 +23,7 @@ public static InstrumentationAssemblyInfo[] GetContribResourceDetectors() => new() { Name = "Container", - Filename = "OpenTelemetry.Resources.Container.dll", + AssemblyName = "OpenTelemetry.Resources.Container", FullyQualifiedType = "OpenTelemetry.Resources.ContainerResourceBuilderExtensions", InstrumentationMethod = "AddContainerDetector" }, @@ -31,7 +31,7 @@ public static InstrumentationAssemblyInfo[] GetContribResourceDetectors() => new() { Name = "OperatingSystem", - Filename = "OpenTelemetry.Resources.OperatingSystem.dll", + AssemblyName = "OpenTelemetry.Resources.OperatingSystem", FullyQualifiedType = "OpenTelemetry.Resources.OperatingSystemResourceBuilderExtensions", InstrumentationMethod = "AddOperatingSystemDetector" }, @@ -39,7 +39,7 @@ public static InstrumentationAssemblyInfo[] GetContribResourceDetectors() => new() { Name = "Process", - Filename = "OpenTelemetry.Resources.Process.dll", + AssemblyName = "OpenTelemetry.Resources.Process", FullyQualifiedType = "OpenTelemetry.Resources.ProcessResourceBuilderExtensions", InstrumentationMethod = "AddProcessDetector" } diff --git a/src/Elastic.OpenTelemetry.Core/Instrumentation/InstrumentationAssemblyInfo.cs b/src/Elastic.OpenTelemetry.Core/Instrumentation/InstrumentationAssemblyInfo.cs index 71b21530..c4d616fb 100644 --- a/src/Elastic.OpenTelemetry.Core/Instrumentation/InstrumentationAssemblyInfo.cs +++ b/src/Elastic.OpenTelemetry.Core/Instrumentation/InstrumentationAssemblyInfo.cs @@ -7,7 +7,7 @@ namespace Elastic.OpenTelemetry.Core; internal readonly struct InstrumentationAssemblyInfo { public readonly required string Name { get; init; } - public readonly required string Filename { get; init; } + public readonly required string AssemblyName { get; init; } public readonly required string FullyQualifiedType { get; init; } public readonly required string InstrumentationMethod { get; init; } } diff --git a/src/Elastic.OpenTelemetry.Core/SignalBuilder.cs b/src/Elastic.OpenTelemetry.Core/SignalBuilder.cs index 21abfa9b..5aec7111 100644 --- a/src/Elastic.OpenTelemetry.Core/SignalBuilder.cs +++ b/src/Elastic.OpenTelemetry.Core/SignalBuilder.cs @@ -189,53 +189,45 @@ public static void AddInstrumentationViaReflection(T builder, ILogger logger, where T : class { var builderTypeName = builder.GetType().Name; - var assemblyLocation = AppContext.BaseDirectory; - if (!string.IsNullOrEmpty(assemblyLocation)) + foreach (var assemblyInfo in assemblyInfos) { - foreach (var assemblyInfo in assemblyInfos) + try { - try + var type = Type.GetType($"{assemblyInfo.FullyQualifiedType}, {assemblyInfo.AssemblyName}"); + + if (type is null) { - var assemblyPath = Path.Combine(assemblyLocation, assemblyInfo.Filename); - if (File.Exists(assemblyPath)) - { - logger.LogLocatedInstrumentationAssembly(assemblyInfo.Filename, assemblyLocation); - - var assembly = Assembly.LoadFrom(assemblyPath); - var type = assembly.GetType(assemblyInfo.FullyQualifiedType); - - if (type is null) - { - logger.LogUnableToFindTypeWarning(assemblyInfo.FullyQualifiedType, assembly.FullName ?? "UNKNOWN"); - continue; - } - - var methodInfo = type.GetMethod(assemblyInfo.InstrumentationMethod, BindingFlags.Static | BindingFlags.Public, - Type.DefaultBinder, [typeof(T)], null); - - if (methodInfo is null) - { - logger.LogUnableToFindMethodWarning(assemblyInfo.FullyQualifiedType, assemblyInfo.InstrumentationMethod, - assembly.FullName ?? "UNKNOWN"); - continue; - } - - methodInfo.Invoke(null, [builder]); // Invoke the extension method to register the instrumentation with the builder. - - logger.LogAddedInstrumentationViaReflection(assemblyInfo.Name, builderTypeName, builderInstanceId); - } + logger.LogUnableToFindType(assemblyInfo.FullyQualifiedType, assemblyInfo.AssemblyName); + continue; } - catch (Exception ex) + + var methodInfo = type.GetMethod(assemblyInfo.InstrumentationMethod, BindingFlags.Static | BindingFlags.Public, + Type.DefaultBinder, [typeof(T)], null); + + if (methodInfo is null) + { + logger.LogUnableToFindMethodWarning(assemblyInfo.FullyQualifiedType, assemblyInfo.InstrumentationMethod, + assemblyInfo.AssemblyName); + continue; + } + + methodInfo.Invoke(null, [builder]); // Invoke the extension method to register the instrumentation with the builder. + + if (builderTypeName.StartsWith("ResourceBuilder")) { - logger.LogError(new EventId(503, "DynamicInstrumentaionFailed"), ex, "Failed to dynamically enable " + - "{InstrumentationName} on {Provider}.", assemblyInfo.Name, builderTypeName); + logger.LogAddedResourceDetectorViaReflection(assemblyInfo.Name, builderTypeName, builderInstanceId); + } + else + { + logger.LogAddedInstrumentationViaReflection(assemblyInfo.Name, builderTypeName, builderInstanceId); } } - } - else - { - logger.LogBaseDirectoryWarning(); + catch (Exception ex) + { + logger.LogError(new EventId(503, "DynamicInstrumentaionFailed"), ex, "Failed to dynamically enable " + + "{InstrumentationName} on {Provider}.", assemblyInfo.Name, builderTypeName); + } } } } diff --git a/src/Elastic.OpenTelemetry/Extensions/TracerProviderBuilderExtensions.cs b/src/Elastic.OpenTelemetry/Extensions/TracerProviderBuilderExtensions.cs index fb7b06d6..a54b84d2 100644 --- a/src/Elastic.OpenTelemetry/Extensions/TracerProviderBuilderExtensions.cs +++ b/src/Elastic.OpenTelemetry/Extensions/TracerProviderBuilderExtensions.cs @@ -229,13 +229,13 @@ private static void AddAspNetCoreInstrumentation(TracerProviderBuilder builder, if (tracerProviderBuilderExtensionsType is null) { - logger.LogUnableToFindTypeWarning(tracerProviderBuilderExtensionsTypeName, assemblyName); + logger.LogUnableToFindType(tracerProviderBuilderExtensionsTypeName, assemblyName); return; } if (optionsType is null) { - logger.LogUnableToFindTypeWarning(aspNetCoreTraceInstrumentationOptionsTypeName, assemblyName); + logger.LogUnableToFindType(aspNetCoreTraceInstrumentationOptionsTypeName, assemblyName); return; } diff --git a/src/Elastic.OpenTelemetry/Instrumentation/ContribMetricsInstrumentation.cs b/src/Elastic.OpenTelemetry/Instrumentation/ContribMetricsInstrumentation.cs index 303d1ee2..2ec13f42 100644 --- a/src/Elastic.OpenTelemetry/Instrumentation/ContribMetricsInstrumentation.cs +++ b/src/Elastic.OpenTelemetry/Instrumentation/ContribMetricsInstrumentation.cs @@ -18,7 +18,7 @@ public static InstrumentationAssemblyInfo[] GetMetricsInstrumentationAssembliesI new() { Name = "AspNet", - Filename = "OpenTelemetry.Instrumentation.AspNet.dll", + AssemblyName = "OpenTelemetry.Instrumentation.AspNet", FullyQualifiedType = "OpenTelemetry.Metrics.MeterProviderBuilderExtensions", InstrumentationMethod = "AddAspNetInstrumentation" }, @@ -26,7 +26,7 @@ public static InstrumentationAssemblyInfo[] GetMetricsInstrumentationAssembliesI new() { Name = "AspNetCore", - Filename = "OpenTelemetry.Instrumentation.AspNetCore.dll", + AssemblyName = "OpenTelemetry.Instrumentation.AspNetCore", FullyQualifiedType = "OpenTelemetry.Metrics.AspNetCoreInstrumentationMeterProviderBuilderExtensions", InstrumentationMethod = "AddAspNetCoreInstrumentation" }, @@ -34,7 +34,7 @@ public static InstrumentationAssemblyInfo[] GetMetricsInstrumentationAssembliesI new() { Name = "AWS", - Filename = "OpenTelemetry.Instrumentation.AWS.dll", + AssemblyName = "OpenTelemetry.Instrumentation.AWS", FullyQualifiedType = "OpenTelemetry.Metrics.MeterProviderBuilderExtensions", InstrumentationMethod = "AddAWSInstrumentation" }, @@ -42,7 +42,7 @@ public static InstrumentationAssemblyInfo[] GetMetricsInstrumentationAssembliesI new() { Name = "Cassandra", - Filename = "OpenTelemetry.Instrumentation.Cassandra.dll", + AssemblyName = "OpenTelemetry.Instrumentation.Cassandra", FullyQualifiedType = "OpenTelemetry.Metrics.MeterProviderBuilderExtensions", InstrumentationMethod = "AddCassandraInstrumentation" }, @@ -50,7 +50,7 @@ public static InstrumentationAssemblyInfo[] GetMetricsInstrumentationAssembliesI new() { Name = "Kafka (Producer)", - Filename = "OpenTelemetry.Instrumentation.ConfluentKafka.dll", + AssemblyName = "OpenTelemetry.Instrumentation.ConfluentKafka", FullyQualifiedType = "OpenTelemetry.Metrics.MeterProviderBuilderExtensions", InstrumentationMethod = "AddKafkaProducerInstrumentation" }, @@ -58,7 +58,7 @@ public static InstrumentationAssemblyInfo[] GetMetricsInstrumentationAssembliesI new() { Name = "Kafka (Consumer)", - Filename = "OpenTelemetry.Instrumentation.ConfluentKafka.dll", + AssemblyName = "OpenTelemetry.Instrumentation.ConfluentKafka", FullyQualifiedType = "OpenTelemetry.Metrics.MeterProviderBuilderExtensions", InstrumentationMethod = "AddKafkaConsumerInstrumentation" }, @@ -66,7 +66,7 @@ public static InstrumentationAssemblyInfo[] GetMetricsInstrumentationAssembliesI new() { Name = "EventCounters", - Filename = "OpenTelemetry.Instrumentation.EventCounters.dll", + AssemblyName = "OpenTelemetry.Instrumentation.EventCounters", FullyQualifiedType = "OpenTelemetry.Metrics.MeterProviderBuilderExtensions", InstrumentationMethod = "AddEventCountersInstrumentation" }, @@ -74,7 +74,7 @@ public static InstrumentationAssemblyInfo[] GetMetricsInstrumentationAssembliesI new() { Name = "HTTP", - Filename = "OpenTelemetry.Instrumentation.Http.dll", + AssemblyName = "OpenTelemetry.Instrumentation.Http", FullyQualifiedType = "OpenTelemetry.Metrics.HttpClientInstrumentationMeterProviderBuilderExtensions", InstrumentationMethod = "AddHttpClientInstrumentation" }, @@ -82,7 +82,7 @@ public static InstrumentationAssemblyInfo[] GetMetricsInstrumentationAssembliesI new() { Name = "Runtime", - Filename = "OpenTelemetry.Instrumentation.Runtime.dll", + AssemblyName = "OpenTelemetry.Instrumentation.Runtime", FullyQualifiedType = "OpenTelemetry.Metrics.MeterProviderBuilderExtensions", InstrumentationMethod = "AddRuntimeInstrumentation" }, @@ -90,7 +90,7 @@ public static InstrumentationAssemblyInfo[] GetMetricsInstrumentationAssembliesI new() { Name = "Process", - Filename = "OpenTelemetry.Instrumentation.Process.dll", + AssemblyName = "OpenTelemetry.Instrumentation.Process", FullyQualifiedType = "OpenTelemetry.Metrics.MeterProviderBuilderExtensions", InstrumentationMethod = "AddProcessInstrumentation" } diff --git a/src/Elastic.OpenTelemetry/Instrumentation/ContribTraceInstrumentation.cs b/src/Elastic.OpenTelemetry/Instrumentation/ContribTraceInstrumentation.cs index a9a6c66c..1dc2a980 100644 --- a/src/Elastic.OpenTelemetry/Instrumentation/ContribTraceInstrumentation.cs +++ b/src/Elastic.OpenTelemetry/Instrumentation/ContribTraceInstrumentation.cs @@ -18,7 +18,7 @@ public static InstrumentationAssemblyInfo[] GetReflectionInstrumentationAssembli new() { Name = "AspNet", - Filename = "OpenTelemetry.Instrumentation.AspNet.dll", + AssemblyName = "OpenTelemetry.Instrumentation.AspNet", FullyQualifiedType = "OpenTelemetry.Trace.TracerProviderBuilderExtensions", InstrumentationMethod = "AddAspNetInstrumentation" }, @@ -28,7 +28,7 @@ public static InstrumentationAssemblyInfo[] GetReflectionInstrumentationAssembli new() { Name = "AWS", - Filename = "OpenTelemetry.Instrumentation.AWS.dll", + AssemblyName = "OpenTelemetry.Instrumentation.AWS", FullyQualifiedType = "OpenTelemetry.Trace.TracerProviderBuilderExtensions", InstrumentationMethod = "AddAWSInstrumentation" }, @@ -36,7 +36,7 @@ public static InstrumentationAssemblyInfo[] GetReflectionInstrumentationAssembli new() { Name = "ElasticsearchClient (NEST)", - Filename = "OpenTelemetry.Instrumentation.ElasticsearchClient.dll", + AssemblyName = "OpenTelemetry.Instrumentation.ElasticsearchClient", FullyQualifiedType = "OpenTelemetry.Trace.TracerProviderBuilderExtensions", InstrumentationMethod = "AddElasticsearchClientInstrumentation" }, @@ -44,7 +44,7 @@ public static InstrumentationAssemblyInfo[] GetReflectionInstrumentationAssembli new() { Name = "EntityFrameworkCore", - Filename = "OpenTelemetry.Instrumentation.EntityFrameworkCore.dll", + AssemblyName = "OpenTelemetry.Instrumentation.EntityFrameworkCore", FullyQualifiedType = "OpenTelemetry.Trace.TracerProviderBuilderExtensions", InstrumentationMethod = "AddEntityFrameworkCoreInstrumentation" }, @@ -52,7 +52,7 @@ public static InstrumentationAssemblyInfo[] GetReflectionInstrumentationAssembli new() { Name = "GrpcNetClient", - Filename = "OpenTelemetry.Instrumentation.GrpcNetClient.dll", + AssemblyName = "OpenTelemetry.Instrumentation.GrpcNetClient", FullyQualifiedType = "OpenTelemetry.Trace.TracerProviderBuilderExtensions", InstrumentationMethod = "AddGrpcClientInstrumentation" }, @@ -60,7 +60,7 @@ public static InstrumentationAssemblyInfo[] GetReflectionInstrumentationAssembli new() { Name = "GrpcCore", - Filename = "OpenTelemetry.Instrumentation.GrpcCore.dll", + AssemblyName = "OpenTelemetry.Instrumentation.GrpcCore", FullyQualifiedType = "OpenTelemetry.Trace.TracerProviderBuilderExtensions", InstrumentationMethod = "AddGrpcCoreInstrumentation" }, @@ -68,7 +68,7 @@ public static InstrumentationAssemblyInfo[] GetReflectionInstrumentationAssembli new() { Name = "Hangfire", - Filename = "OpenTelemetry.Instrumentation.Hangfire.dll", + AssemblyName = "OpenTelemetry.Instrumentation.Hangfire", FullyQualifiedType = "OpenTelemetry.Trace.TracerProviderBuilderExtensions", InstrumentationMethod = "AddHangfireInstrumentation" }, @@ -81,7 +81,7 @@ public static InstrumentationAssemblyInfo[] GetReflectionInstrumentationAssembli new() { Name = "HTTP", - Filename = "OpenTelemetry.Instrumentation.Http.dll", + AssemblyName = "OpenTelemetry.Instrumentation.Http", FullyQualifiedType = "OpenTelemetry.Trace.HttpClientInstrumentationTracerProviderBuilderExtensions", InstrumentationMethod = "AddHttpClientInstrumentation" }, @@ -89,7 +89,7 @@ public static InstrumentationAssemblyInfo[] GetReflectionInstrumentationAssembli new() { Name = "Kafka (Producer)", - Filename = "OpenTelemetry.Instrumentation.ConfluentKafka.dll", + AssemblyName = "OpenTelemetry.Instrumentation.ConfluentKafka", FullyQualifiedType = "OpenTelemetry.Trace.TracerProviderBuilderExtensions", InstrumentationMethod = "AddKafkaProducerInstrumentation" }, @@ -97,7 +97,7 @@ public static InstrumentationAssemblyInfo[] GetReflectionInstrumentationAssembli new() { Name = "Kafka (Consumer)", - Filename = "OpenTelemetry.Instrumentation.ConfluentKafka.dll", + AssemblyName = "OpenTelemetry.Instrumentation.ConfluentKafka", FullyQualifiedType = "OpenTelemetry.Trace.TracerProviderBuilderExtensions", InstrumentationMethod = "AddKafkaConsumerInstrumentation" }, @@ -105,7 +105,7 @@ public static InstrumentationAssemblyInfo[] GetReflectionInstrumentationAssembli new() { Name = "Owin", - Filename = "OpenTelemetry.Instrumentation.Owin.dll", + AssemblyName = "OpenTelemetry.Instrumentation.Owin", FullyQualifiedType = "OpenTelemetry.Trace.TracerProviderBuilderExtensions", InstrumentationMethod = "AddOwinInstrumentation" }, @@ -113,7 +113,7 @@ public static InstrumentationAssemblyInfo[] GetReflectionInstrumentationAssembli new() { Name = "Quartz", - Filename = "OpenTelemetry.Instrumentation.Quartz.dll", + AssemblyName = "OpenTelemetry.Instrumentation.Quartz", FullyQualifiedType = "OpenTelemetry.Trace.TracerProviderBuilderExtensions", InstrumentationMethod = "AddQuartzInstrumentation" }, @@ -121,7 +121,7 @@ public static InstrumentationAssemblyInfo[] GetReflectionInstrumentationAssembli new() { Name = "ServiceFabricRemoting", - Filename = "OpenTelemetry.Instrumentation.ServiceFabricRemoting.dll", + AssemblyName = "OpenTelemetry.Instrumentation.ServiceFabricRemoting", FullyQualifiedType = "OpenTelemetry.Trace.TracerProviderBuilderExtensions", InstrumentationMethod = "AddServiceFabricRemotingInstrumentation" }, @@ -129,7 +129,7 @@ public static InstrumentationAssemblyInfo[] GetReflectionInstrumentationAssembli new() { Name = "SqlClient", - Filename = "OpenTelemetry.Instrumentation.SqlClient.dll", + AssemblyName = "OpenTelemetry.Instrumentation.SqlClient", FullyQualifiedType = "OpenTelemetry.Trace.TracerProviderBuilderExtensions", InstrumentationMethod = "AddSqlClientInstrumentation" }, @@ -137,7 +137,7 @@ public static InstrumentationAssemblyInfo[] GetReflectionInstrumentationAssembli new() { Name = "StackExchangeRedis", - Filename = "OpenTelemetry.Instrumentation.StackExchangeRedis.dll", + AssemblyName = "OpenTelemetry.Instrumentation.StackExchangeRedis", FullyQualifiedType = "OpenTelemetry.Trace.TracerProviderBuilderExtensions", InstrumentationMethod = "AddRedisInstrumentation" }, @@ -145,7 +145,7 @@ public static InstrumentationAssemblyInfo[] GetReflectionInstrumentationAssembli new() { Name = "WCF", - Filename = "OpenTelemetry.Instrumentation.Wcf.dll", + AssemblyName = "OpenTelemetry.Instrumentation.Wcf", FullyQualifiedType = "OpenTelemetry.Trace.TracerProviderBuilderExtensions", InstrumentationMethod = "AddWcfInstrumentation" },