in src/Elastic.OpenTelemetry/Extensions/TracerProviderBuilderExtensions.cs [211:279]
private static void AddAspNetCoreInstrumentation(TracerProviderBuilder builder, BuilderState builderState)
{
if (builderState.Components.Options.SkipInstrumentationAssemblyScanning)
return;
var logger = builderState.Components.Logger;
const string tracerProviderBuilderExtensionsTypeName = "OpenTelemetry.Trace.AspNetCoreInstrumentationTracerProviderBuilderExtensions";
const string aspNetCoreTraceInstrumentationOptionsTypeName = "OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreTraceInstrumentationOptions";
const string extensionMethodName = "AddAspNetCoreInstrumentation";
const string assemblyName = "OpenTelemetry.Instrumentation.AspNetCore";
var builderTypeName = builder.GetType().Name;
try
{
var tracerProviderBuilderExtensionsType = Type.GetType($"{tracerProviderBuilderExtensionsTypeName}, {assemblyName}");
var optionsType = Type.GetType($"{aspNetCoreTraceInstrumentationOptionsTypeName}, {assemblyName}");
if (tracerProviderBuilderExtensionsType is null)
{
logger.LogUnableToFindType(tracerProviderBuilderExtensionsTypeName, assemblyName);
return;
}
if (optionsType is null)
{
logger.LogUnableToFindType(aspNetCoreTraceInstrumentationOptionsTypeName, assemblyName);
return;
}
Action<object> configureOptions = options =>
{
var enrichWithExceptionProperty = options.GetType().GetProperty("EnrichWithException");
if (enrichWithExceptionProperty is not null)
{
var enrichWithExceptionDelegate = (Action<Activity, Exception>)((activity, ex) =>
{
activity.AddException(ex);
if (ex.Source is not null)
{
activity.SetTag("exception.source", ex.Source);
}
});
enrichWithExceptionProperty.SetValue(options, enrichWithExceptionDelegate);
}
};
var methodInfo = tracerProviderBuilderExtensionsType.GetMethod(extensionMethodName, BindingFlags.Static | BindingFlags.Public,
Type.DefaultBinder, [typeof(TracerProviderBuilder), typeof(Action<>).MakeGenericType(optionsType)], null);
if (methodInfo is null)
{
logger.LogUnableToFindMethodWarning(tracerProviderBuilderExtensionsTypeName, extensionMethodName, assemblyName);
return;
}
methodInfo.Invoke(null, [builder, configureOptions]);
logger.LogAddedInstrumentationViaReflection(assemblyName, builderTypeName, builderState.InstanceIdentifier);
}
catch (Exception ex)
{
logger.LogError(new EventId(503, "DynamicInstrumentaionFailed"), ex, "Failed to dynamically enable " +
"{InstrumentationName} on {Provider}.", assemblyName, builderTypeName);
}
}