Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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}.")]
Expand All @@ -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}).")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,23 @@ public static InstrumentationAssemblyInfo[] GetContribResourceDetectors() =>
new()
{
Name = "Container",
Filename = "OpenTelemetry.Resources.Container.dll",
AssemblyName = "OpenTelemetry.Resources.Container",
FullyQualifiedType = "OpenTelemetry.Resources.ContainerResourceBuilderExtensions",
InstrumentationMethod = "AddContainerDetector"
},

new()
{
Name = "OperatingSystem",
Filename = "OpenTelemetry.Resources.OperatingSystem.dll",
AssemblyName = "OpenTelemetry.Resources.OperatingSystem",
FullyQualifiedType = "OpenTelemetry.Resources.OperatingSystemResourceBuilderExtensions",
InstrumentationMethod = "AddOperatingSystemDetector"
},

new()
{
Name = "Process",
Filename = "OpenTelemetry.Resources.Process.dll",
AssemblyName = "OpenTelemetry.Resources.Process",
FullyQualifiedType = "OpenTelemetry.Resources.ProcessResourceBuilderExtensions",
InstrumentationMethod = "AddProcessDetector"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
70 changes: 31 additions & 39 deletions src/Elastic.OpenTelemetry.Core/SignalBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,53 +189,45 @@ public static void AddInstrumentationViaReflection<T>(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);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,79 +18,79 @@ public static InstrumentationAssemblyInfo[] GetMetricsInstrumentationAssembliesI
new()
{
Name = "AspNet",
Filename = "OpenTelemetry.Instrumentation.AspNet.dll",
AssemblyName = "OpenTelemetry.Instrumentation.AspNet",
FullyQualifiedType = "OpenTelemetry.Metrics.MeterProviderBuilderExtensions",
InstrumentationMethod = "AddAspNetInstrumentation"
},

new()
{
Name = "AspNetCore",
Filename = "OpenTelemetry.Instrumentation.AspNetCore.dll",
AssemblyName = "OpenTelemetry.Instrumentation.AspNetCore",
FullyQualifiedType = "OpenTelemetry.Metrics.AspNetCoreInstrumentationMeterProviderBuilderExtensions",
InstrumentationMethod = "AddAspNetCoreInstrumentation"
},

new()
{
Name = "AWS",
Filename = "OpenTelemetry.Instrumentation.AWS.dll",
AssemblyName = "OpenTelemetry.Instrumentation.AWS",
FullyQualifiedType = "OpenTelemetry.Metrics.MeterProviderBuilderExtensions",
InstrumentationMethod = "AddAWSInstrumentation"
},

new()
{
Name = "Cassandra",
Filename = "OpenTelemetry.Instrumentation.Cassandra.dll",
AssemblyName = "OpenTelemetry.Instrumentation.Cassandra",
FullyQualifiedType = "OpenTelemetry.Metrics.MeterProviderBuilderExtensions",
InstrumentationMethod = "AddCassandraInstrumentation"
},

new()
{
Name = "Kafka (Producer)",
Filename = "OpenTelemetry.Instrumentation.ConfluentKafka.dll",
AssemblyName = "OpenTelemetry.Instrumentation.ConfluentKafka",
FullyQualifiedType = "OpenTelemetry.Metrics.MeterProviderBuilderExtensions",
InstrumentationMethod = "AddKafkaProducerInstrumentation"
},

new()
{
Name = "Kafka (Consumer)",
Filename = "OpenTelemetry.Instrumentation.ConfluentKafka.dll",
AssemblyName = "OpenTelemetry.Instrumentation.ConfluentKafka",
FullyQualifiedType = "OpenTelemetry.Metrics.MeterProviderBuilderExtensions",
InstrumentationMethod = "AddKafkaConsumerInstrumentation"
},

new()
{
Name = "EventCounters",
Filename = "OpenTelemetry.Instrumentation.EventCounters.dll",
AssemblyName = "OpenTelemetry.Instrumentation.EventCounters",
FullyQualifiedType = "OpenTelemetry.Metrics.MeterProviderBuilderExtensions",
InstrumentationMethod = "AddEventCountersInstrumentation"
},

new()
{
Name = "HTTP",
Filename = "OpenTelemetry.Instrumentation.Http.dll",
AssemblyName = "OpenTelemetry.Instrumentation.Http",
FullyQualifiedType = "OpenTelemetry.Metrics.HttpClientInstrumentationMeterProviderBuilderExtensions",
InstrumentationMethod = "AddHttpClientInstrumentation"
},

new()
{
Name = "Runtime",
Filename = "OpenTelemetry.Instrumentation.Runtime.dll",
AssemblyName = "OpenTelemetry.Instrumentation.Runtime",
FullyQualifiedType = "OpenTelemetry.Metrics.MeterProviderBuilderExtensions",
InstrumentationMethod = "AddRuntimeInstrumentation"
},

new()
{
Name = "Process",
Filename = "OpenTelemetry.Instrumentation.Process.dll",
AssemblyName = "OpenTelemetry.Instrumentation.Process",
FullyQualifiedType = "OpenTelemetry.Metrics.MeterProviderBuilderExtensions",
InstrumentationMethod = "AddProcessInstrumentation"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand All @@ -28,47 +28,47 @@ public static InstrumentationAssemblyInfo[] GetReflectionInstrumentationAssembli
new()
{
Name = "AWS",
Filename = "OpenTelemetry.Instrumentation.AWS.dll",
AssemblyName = "OpenTelemetry.Instrumentation.AWS",
FullyQualifiedType = "OpenTelemetry.Trace.TracerProviderBuilderExtensions",
InstrumentationMethod = "AddAWSInstrumentation"
},

new()
{
Name = "ElasticsearchClient (NEST)",
Filename = "OpenTelemetry.Instrumentation.ElasticsearchClient.dll",
AssemblyName = "OpenTelemetry.Instrumentation.ElasticsearchClient",
FullyQualifiedType = "OpenTelemetry.Trace.TracerProviderBuilderExtensions",
InstrumentationMethod = "AddElasticsearchClientInstrumentation"
},

new()
{
Name = "EntityFrameworkCore",
Filename = "OpenTelemetry.Instrumentation.EntityFrameworkCore.dll",
AssemblyName = "OpenTelemetry.Instrumentation.EntityFrameworkCore",
FullyQualifiedType = "OpenTelemetry.Trace.TracerProviderBuilderExtensions",
InstrumentationMethod = "AddEntityFrameworkCoreInstrumentation"
},

new()
{
Name = "GrpcNetClient",
Filename = "OpenTelemetry.Instrumentation.GrpcNetClient.dll",
AssemblyName = "OpenTelemetry.Instrumentation.GrpcNetClient",
FullyQualifiedType = "OpenTelemetry.Trace.TracerProviderBuilderExtensions",
InstrumentationMethod = "AddGrpcClientInstrumentation"
},

new()
{
Name = "GrpcCore",
Filename = "OpenTelemetry.Instrumentation.GrpcCore.dll",
AssemblyName = "OpenTelemetry.Instrumentation.GrpcCore",
FullyQualifiedType = "OpenTelemetry.Trace.TracerProviderBuilderExtensions",
InstrumentationMethod = "AddGrpcCoreInstrumentation"
},

new()
{
Name = "Hangfire",
Filename = "OpenTelemetry.Instrumentation.Hangfire.dll",
AssemblyName = "OpenTelemetry.Instrumentation.Hangfire",
FullyQualifiedType = "OpenTelemetry.Trace.TracerProviderBuilderExtensions",
InstrumentationMethod = "AddHangfireInstrumentation"
},
Expand All @@ -81,71 +81,71 @@ public static InstrumentationAssemblyInfo[] GetReflectionInstrumentationAssembli
new()
{
Name = "HTTP",
Filename = "OpenTelemetry.Instrumentation.Http.dll",
AssemblyName = "OpenTelemetry.Instrumentation.Http",
FullyQualifiedType = "OpenTelemetry.Trace.HttpClientInstrumentationTracerProviderBuilderExtensions",
InstrumentationMethod = "AddHttpClientInstrumentation"
},

new()
{
Name = "Kafka (Producer)",
Filename = "OpenTelemetry.Instrumentation.ConfluentKafka.dll",
AssemblyName = "OpenTelemetry.Instrumentation.ConfluentKafka",
FullyQualifiedType = "OpenTelemetry.Trace.TracerProviderBuilderExtensions",
InstrumentationMethod = "AddKafkaProducerInstrumentation"
},

new()
{
Name = "Kafka (Consumer)",
Filename = "OpenTelemetry.Instrumentation.ConfluentKafka.dll",
AssemblyName = "OpenTelemetry.Instrumentation.ConfluentKafka",
FullyQualifiedType = "OpenTelemetry.Trace.TracerProviderBuilderExtensions",
InstrumentationMethod = "AddKafkaConsumerInstrumentation"
},

new()
{
Name = "Owin",
Filename = "OpenTelemetry.Instrumentation.Owin.dll",
AssemblyName = "OpenTelemetry.Instrumentation.Owin",
FullyQualifiedType = "OpenTelemetry.Trace.TracerProviderBuilderExtensions",
InstrumentationMethod = "AddOwinInstrumentation"
},

new()
{
Name = "Quartz",
Filename = "OpenTelemetry.Instrumentation.Quartz.dll",
AssemblyName = "OpenTelemetry.Instrumentation.Quartz",
FullyQualifiedType = "OpenTelemetry.Trace.TracerProviderBuilderExtensions",
InstrumentationMethod = "AddQuartzInstrumentation"
},

new()
{
Name = "ServiceFabricRemoting",
Filename = "OpenTelemetry.Instrumentation.ServiceFabricRemoting.dll",
AssemblyName = "OpenTelemetry.Instrumentation.ServiceFabricRemoting",
FullyQualifiedType = "OpenTelemetry.Trace.TracerProviderBuilderExtensions",
InstrumentationMethod = "AddServiceFabricRemotingInstrumentation"
},

new()
{
Name = "SqlClient",
Filename = "OpenTelemetry.Instrumentation.SqlClient.dll",
AssemblyName = "OpenTelemetry.Instrumentation.SqlClient",
FullyQualifiedType = "OpenTelemetry.Trace.TracerProviderBuilderExtensions",
InstrumentationMethod = "AddSqlClientInstrumentation"
},

new()
{
Name = "StackExchangeRedis",
Filename = "OpenTelemetry.Instrumentation.StackExchangeRedis.dll",
AssemblyName = "OpenTelemetry.Instrumentation.StackExchangeRedis",
FullyQualifiedType = "OpenTelemetry.Trace.TracerProviderBuilderExtensions",
InstrumentationMethod = "AddRedisInstrumentation"
},

new()
{
Name = "WCF",
Filename = "OpenTelemetry.Instrumentation.Wcf.dll",
AssemblyName = "OpenTelemetry.Instrumentation.Wcf",
FullyQualifiedType = "OpenTelemetry.Trace.TracerProviderBuilderExtensions",
InstrumentationMethod = "AddWcfInstrumentation"
},
Expand Down
Loading