diff --git a/src/OpenTelemetry.AutoInstrumentation/Configuration/EnvironmentConfigurationMetricHelper.cs b/src/OpenTelemetry.AutoInstrumentation/Configuration/EnvironmentConfigurationMetricHelper.cs index 8476fa257..a6cda3279 100644 --- a/src/OpenTelemetry.AutoInstrumentation/Configuration/EnvironmentConfigurationMetricHelper.cs +++ b/src/OpenTelemetry.AutoInstrumentation/Configuration/EnvironmentConfigurationMetricHelper.cs @@ -17,7 +17,6 @@ using System; using System.Collections.Generic; using System.Linq; -using OpenTelemetry.AutoInstrumentation.Util; using OpenTelemetry.Metrics; namespace OpenTelemetry.AutoInstrumentation.Configuration; @@ -53,10 +52,7 @@ internal static class EnvironmentConfigurationMetricHelper #if NET462 builder.AddAspNetInstrumentation(); #elif NETCOREAPP3_1_OR_GREATER - if (AssemblyDetector.IsAspNetCoreDetected) - { - builder.AddAspNetCoreInstrumentation(); - } + builder.AddMeter("OpenTelemetry.Instrumentation.AspNetCore"); #endif return builder; diff --git a/src/OpenTelemetry.AutoInstrumentation/Configuration/EnvironmentConfigurationTracerHelper.cs b/src/OpenTelemetry.AutoInstrumentation/Configuration/EnvironmentConfigurationTracerHelper.cs index 47c350ece..ae683bcd0 100644 --- a/src/OpenTelemetry.AutoInstrumentation/Configuration/EnvironmentConfigurationTracerHelper.cs +++ b/src/OpenTelemetry.AutoInstrumentation/Configuration/EnvironmentConfigurationTracerHelper.cs @@ -17,9 +17,6 @@ using System; using System.Collections.Generic; using System.Linq; -#if NETCOREAPP3_1_OR_GREATER -using OpenTelemetry.AutoInstrumentation.Util; -#endif using OpenTelemetry.Trace; namespace OpenTelemetry.AutoInstrumentation.Configuration; @@ -66,10 +63,8 @@ internal static class EnvironmentConfigurationTracerHelper #if NET462 builder.AddAspNetInstrumentation(); #elif NETCOREAPP3_1_OR_GREATER - if (AssemblyDetector.IsAspNetCoreDetected) - { - builder.AddAspNetCoreInstrumentation(); - } + builder.AddSource("OpenTelemetry.Instrumentation.AspNetCore"); + builder.AddLegacySource("Microsoft.AspNetCore.Hosting.HttpRequestIn"); #endif return builder; diff --git a/src/OpenTelemetry.AutoInstrumentation/Instrumentation.cs b/src/OpenTelemetry.AutoInstrumentation/Instrumentation.cs index ac4534ad1..ba240e93e 100644 --- a/src/OpenTelemetry.AutoInstrumentation/Instrumentation.cs +++ b/src/OpenTelemetry.AutoInstrumentation/Instrumentation.cs @@ -116,6 +116,11 @@ public static class Instrumentation // and TracerSettings.EnabledInstrumentations would be passed as input #if NETCOREAPP3_1_OR_GREATER + if (TracerSettings.EnabledInstrumentations.Contains(TracerInstrumentation.AspNet)) + { + LazyInstrumentationLoader.Add(new AspNetCoreInitializer()); + } + if (TracerSettings.EnabledInstrumentations.Contains(TracerInstrumentation.MySqlData)) { LazyInstrumentationLoader.Add(new MySqlDataInitializer()); @@ -135,6 +140,14 @@ public static class Instrumentation if (MeterSettings.LoadMetricsAtStartup) { +#if NETCOREAPP3_1_OR_GREATER + + if (MeterSettings.EnabledInstrumentations.Contains(MeterInstrumentation.AspNet)) + { + LazyInstrumentationLoader.Add(new AspNetCoreMetricsInitializer()); + } +#endif + var builder = Sdk .CreateMeterProviderBuilder() .SetResourceBuilder(_resourceBuilder) diff --git a/src/OpenTelemetry.AutoInstrumentation/Loading/AspNetCoreInitializer.cs b/src/OpenTelemetry.AutoInstrumentation/Loading/AspNetCoreInitializer.cs new file mode 100644 index 000000000..bec0cbc2b --- /dev/null +++ b/src/OpenTelemetry.AutoInstrumentation/Loading/AspNetCoreInitializer.cs @@ -0,0 +1,42 @@ +// +// 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. +// + +#if NETCOREAPP3_1_OR_GREATER + +using System; + +namespace OpenTelemetry.AutoInstrumentation.Loading +{ + internal class AspNetCoreInitializer : InstrumentationInitializer + { + public AspNetCoreInitializer() + : base("Microsoft.AspNetCore.Http") + { + } + + public override void Initialize(ILifespanManager lifespanManager) + { + var instrumentationType = Type.GetType("OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreInstrumentation, OpenTelemetry.Instrumentation.AspNetCore"); + var httpInListenerType = Type.GetType("OpenTelemetry.Instrumentation.AspNetCore.Implementation.HttpInListener, OpenTelemetry.Instrumentation.AspNetCore"); + + var httpInListener = Activator.CreateInstance(httpInListenerType, args: new OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreInstrumentationOptions()); + var instrumentation = Activator.CreateInstance(instrumentationType, args: httpInListener); + + lifespanManager.Track(instrumentation); + } + } +} +#endif diff --git a/src/OpenTelemetry.AutoInstrumentation/Loading/AspNetCoreMetricsInitializer.cs b/src/OpenTelemetry.AutoInstrumentation/Loading/AspNetCoreMetricsInitializer.cs new file mode 100644 index 000000000..4d764816b --- /dev/null +++ b/src/OpenTelemetry.AutoInstrumentation/Loading/AspNetCoreMetricsInitializer.cs @@ -0,0 +1,40 @@ +// +// 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. +// + +#if NETCOREAPP3_1_OR_GREATER + +using System; + +namespace OpenTelemetry.AutoInstrumentation.Loading +{ + internal class AspNetCoreMetricsInitializer : InstrumentationInitializer + { + public AspNetCoreMetricsInitializer() + : base("Microsoft.AspNetCore.Http") + { + } + + public override void Initialize(ILifespanManager lifespanManager) + { + var metricsType = Type.GetType("OpenTelemetry.Instrumentation.AspNetCore.AspNetCoreMetrics, OpenTelemetry.Instrumentation.AspNetCore"); + + var aspNetCoreMetrics = Activator.CreateInstance(metricsType); + + lifespanManager.Track(aspNetCoreMetrics); + } + } +} +#endif diff --git a/src/OpenTelemetry.AutoInstrumentation/Util/AssemblyDetector.cs b/src/OpenTelemetry.AutoInstrumentation/Util/AssemblyDetector.cs deleted file mode 100644 index b5e5e7502..000000000 --- a/src/OpenTelemetry.AutoInstrumentation/Util/AssemblyDetector.cs +++ /dev/null @@ -1,35 +0,0 @@ -// -// 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.Linq; -using System.Reflection; - -namespace OpenTelemetry.AutoInstrumentation.Util -{ - internal static class AssemblyDetector - { - private static bool? _isAspNetCoreDetected; - - public static bool IsAspNetCoreDetected => _isAspNetCoreDetected ??= DetectAspNetCore(); - - private static bool DetectAspNetCore() - { - return Assembly.GetEntryAssembly()? - .GetReferencedAssemblies() - .Any(x => x.Name == "Microsoft.AspNetCore") ?? false; - } - } -}