From c6c142c8d62204e0f1415be221d1a3b44b96267e Mon Sep 17 00:00:00 2001 From: RassK Date: Tue, 15 Nov 2022 16:33:53 +0200 Subject: [PATCH] Optimize SqlClient instrumentation loading Signed-off-by: RassK --- .../EnvironmentConfigurationTracerHelper.cs | 8 ++-- .../Initializers/SqlClientInitializer.cs | 43 +++++++++++++++++++ .../ModuleTests.Default.NetCore.verified.txt | 3 +- .../ModuleTests.Default.NetFx.verified.txt | 3 +- 4 files changed, 50 insertions(+), 7 deletions(-) create mode 100644 src/OpenTelemetry.AutoInstrumentation/Loading/Initializers/SqlClientInitializer.cs diff --git a/src/OpenTelemetry.AutoInstrumentation/Configuration/EnvironmentConfigurationTracerHelper.cs b/src/OpenTelemetry.AutoInstrumentation/Configuration/EnvironmentConfigurationTracerHelper.cs index 6136abf7b6..de55539d08 100644 --- a/src/OpenTelemetry.AutoInstrumentation/Configuration/EnvironmentConfigurationTracerHelper.cs +++ b/src/OpenTelemetry.AutoInstrumentation/Configuration/EnvironmentConfigurationTracerHelper.cs @@ -42,7 +42,7 @@ public static TracerProviderBuilder UseEnvironmentVariables( TracerInstrumentation.GrpcNetClient => Wrappers.AddGrpcClientInstrumentation(builder, pluginManager, lazyInstrumentationLoader), TracerInstrumentation.HttpClient => Wrappers.AddHttpClientInstrumentation(builder, pluginManager), TracerInstrumentation.Npgsql => builder.AddSource("Npgsql"), - TracerInstrumentation.SqlClient => Wrappers.AddSqlClientInstrumentation(builder, pluginManager), + TracerInstrumentation.SqlClient => Wrappers.AddSqlClientInstrumentation(builder, pluginManager, lazyInstrumentationLoader), TracerInstrumentation.Wcf => Wrappers.AddWcfInstrumentation(builder, pluginManager, lazyInstrumentationLoader), #if NET6_0_OR_GREATER TracerInstrumentation.MassTransit => builder.AddSource("MassTransit"), @@ -129,9 +129,11 @@ public static TracerProviderBuilder AddMySqlClientInstrumentation(TracerProvider #endif [MethodImpl(MethodImplOptions.NoInlining)] - public static TracerProviderBuilder AddSqlClientInstrumentation(TracerProviderBuilder builder, PluginManager pluginManager) + public static TracerProviderBuilder AddSqlClientInstrumentation(TracerProviderBuilder builder, PluginManager pluginManager, LazyInstrumentationLoader lazyInstrumentationLoader) { - return builder.AddSqlClientInstrumentation(pluginManager.ConfigureOptions); + lazyInstrumentationLoader.Add(new SqlClientInitializer(pluginManager)); + + return builder.AddSource("OpenTelemetry.SqlClient"); } [MethodImpl(MethodImplOptions.NoInlining)] diff --git a/src/OpenTelemetry.AutoInstrumentation/Loading/Initializers/SqlClientInitializer.cs b/src/OpenTelemetry.AutoInstrumentation/Loading/Initializers/SqlClientInitializer.cs new file mode 100644 index 0000000000..2275b5bdec --- /dev/null +++ b/src/OpenTelemetry.AutoInstrumentation/Loading/Initializers/SqlClientInitializer.cs @@ -0,0 +1,43 @@ +// +// Copyright The OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +using System; +using OpenTelemetry.AutoInstrumentation.Plugins; + +namespace OpenTelemetry.AutoInstrumentation.Loading.Initializers; + +internal class SqlClientInitializer : InstrumentationInitializer +{ + private readonly PluginManager _pluginManager; + + public SqlClientInitializer(PluginManager pluginManager) + : base("Microsoft.Data.SqlClient") + { + _pluginManager = pluginManager; + } + + public override void Initialize(ILifespanManager lifespanManager) + { + var instrumentationType = Type.GetType("OpenTelemetry.Instrumentation.SqlClient.SqlClientInstrumentation, OpenTelemetry.Instrumentation.SqlClient"); + + var options = new OpenTelemetry.Instrumentation.SqlClient.SqlClientInstrumentationOptions(); + _pluginManager.ConfigureOptions(options); + + var instrumentation = Activator.CreateInstance(instrumentationType, options); + + lifespanManager.Track(instrumentation); + } +} diff --git a/test/IntegrationTests/ModuleTests.Default.NetCore.verified.txt b/test/IntegrationTests/ModuleTests.Default.NetCore.verified.txt index a0aaacc789..a676acbda7 100644 --- a/test/IntegrationTests/ModuleTests.Default.NetCore.verified.txt +++ b/test/IntegrationTests/ModuleTests.Default.NetCore.verified.txt @@ -7,6 +7,5 @@ OpenTelemetry.Exporter.OpenTelemetryProtocol, OpenTelemetry.Instrumentation.Http, OpenTelemetry.Instrumentation.Process, - OpenTelemetry.Instrumentation.Runtime, - OpenTelemetry.Instrumentation.SqlClient + OpenTelemetry.Instrumentation.Runtime ] \ No newline at end of file diff --git a/test/IntegrationTests/ModuleTests.Default.NetFx.verified.txt b/test/IntegrationTests/ModuleTests.Default.NetFx.verified.txt index 80551042e5..5253db0a00 100644 --- a/test/IntegrationTests/ModuleTests.Default.NetFx.verified.txt +++ b/test/IntegrationTests/ModuleTests.Default.NetFx.verified.txt @@ -6,6 +6,5 @@ OpenTelemetry.Exporter.OpenTelemetryProtocol, OpenTelemetry.Instrumentation.Http, OpenTelemetry.Instrumentation.Process, - OpenTelemetry.Instrumentation.Runtime, - OpenTelemetry.Instrumentation.SqlClient + OpenTelemetry.Instrumentation.Runtime ] \ No newline at end of file