Lazy loading for asp net instrumentation (#1030)

* Add lazy loading for asp.net core instrumentation

* Add lazy loading for asp.net core metrics

* Add activity sources for ASP.NET instrumentation

* Add meter for ASP.NET metrics

* Add compilation time ifs for net core app asp.net instrumentation initialization

Co-authored-by: Rajkumar Rangaraj <rajrang@microsoft.com>
This commit is contained in:
Dawid Szmigielski 2022-08-03 20:07:41 +02:00 committed by GitHub
parent 70c54e566e
commit 0afe10f785
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 98 additions and 47 deletions

View File

@ -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;

View File

@ -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;

View File

@ -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)

View File

@ -0,0 +1,42 @@
// <copyright file="AspNetCoreInitializer.cs" company="OpenTelemetry Authors">
// 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.
// </copyright>
#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

View File

@ -0,0 +1,40 @@
// <copyright file="AspNetCoreMetricsInitializer.cs" company="OpenTelemetry Authors">
// 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.
// </copyright>
#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

View File

@ -1,35 +0,0 @@
// <copyright file="AssemblyDetector.cs" company="OpenTelemetry Authors">
// 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.
// </copyright>
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;
}
}
}