private static void ConfigureBuilder()

in src/Elastic.OpenTelemetry/Extensions/TracerProviderBuilderExtensions.cs [143:203]


	private static void ConfigureBuilder(TracerProviderBuilder builder, BuilderState builderState, IServiceCollection? services)
	{
		const string tracerProviderBuilderName = nameof(TracerProviderBuilder);

		var components = builderState.Components;
		var logger = components.Logger;

		logger.LogConfiguringBuilder(tracerProviderBuilderName, builderState.InstanceIdentifier);

		builder.ConfigureResource(r => r.WithElasticDefaults(builderState, services));

		if (services is null)
			builder.ConfigureServices(sc => sc.Configure<OtlpExporterOptions>(OtlpExporterDefaults.OtlpExporterOptions));

#if NET9_0_OR_GREATER
		// .NET 9 introduced semantic convention compatible instrumentation in System.Net.Http so it's recommended to no longer
		// use the contrib instrumentation. We don't bring in the dependency for .NET 9+. However, if the consuming app depends
		// on it, it will be assumed that the user prefers it and therefore we allow the assembly scanning to add it. We don't
		// add the native source to avoid doubling up on spans.
		if (SignalBuilder.InstrumentationAssemblyExists("OpenTelemetry.Instrumentation.Http.dll"))
		{
			logger.LogHttpInstrumentationFound("trace", tracerProviderBuilderName, builderState.InstanceIdentifier);

			if (!RuntimeFeature.IsDynamicCodeSupported)
				logger.LogWarning("The OpenTelemetry.Instrumentation.Http.dll was found alongside the executing assembly. " +
					"When using Native AOT publishing on .NET, the trace instrumentation is not registered automatically. Either register it manually, " +
					"or remove the dependency so that the native `System.Net.Http` instrumentation (available in .NET 9) is observed instead.");
		}
		else
		{
			TracerProvderBuilderExtensions.AddActivitySourceWithLogging(builder, logger, "System.Net.Http", builderState.InstanceIdentifier);
		}
#endif

		TracerProvderBuilderExtensions.AddActivitySourceWithLogging(builder, logger, "Elastic.Transport", builderState.InstanceIdentifier);

#if NET
		if (RuntimeFeature.IsDynamicCodeSupported)
#endif
		{
			SignalBuilder.AddInstrumentationViaReflection(builder, components, ContribTraceInstrumentation.GetReflectionInstrumentationAssemblies(), builderState.InstanceIdentifier);

			// This is special-cased because we need to register additional options to ensure we capture exceptions by default
			// This improves the UI experience as requests which cause an exception are highlighted in the UI and users can view the
			// log generated from the span event.
			AddAspNetCoreInstrumentation(builder, builderState);
		}

		TracerProvderBuilderExtensions.AddElasticProcessorsCore(builder, builderState, null, services);

		if (components.Options.SkipOtlpExporter)
		{
			logger.LogSkippingOtlpExporter(nameof(Signals.Traces), nameof(TracerProviderBuilder), builderState.InstanceIdentifier);
		}
		else
		{
			builder.AddOtlpExporter();
		}

		logger.LogConfiguredSignalProvider(nameof(Signals.Traces), nameof(TracerProviderBuilder), builderState.InstanceIdentifier);
	}