elastic / elastic-otel-dotnet

Elastic OpenTelemetry .NET Distribution

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Agent: Integration with ASP.NET (full framework) via custom `HttpModule` [Research]

stevejgordon opened this issue · comments

We should support ASP.NET (full framework) by either extending the OTel HttpModule or shipping our own.

Having reviewed the code, I see that this already works!

Basic steps

public class WebApiApplication : HttpApplication
{
	private const string SourceName = "Example.AspNetClassic";

	internal static readonly ActivitySource ActivitySource = new(SourceName);

	private IInstrumentationLifetime _lifetime;

	protected void Application_Start()
	{
		GlobalConfiguration.Configure(WebApiConfig.Register);
		FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);

		_lifetime = new ElasticOpenTelemetryBuilder()
			.ConfigureResource(r => r.AddService("aspnet-classic-webapi-example"))
			.WithTracing(t => t
				.AddAspNetInstrumentation()
				.AddSource(SourceName))
			.Build();
	}

	protected void Application_End() => _lifetime?.Dispose();
}

Note: Unfortunately, as we depend on some historically problematic libraries, specifically System.Memory and System.Runtime.CompilerServices.Unsafe consumers may hit issues with the resolution of the package in the form of FileLoadException: Could not load file or assembly 'System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference.

This can be worked around by manually adding suitable binding redirects such as:

<dependentAssembly>
	<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
	<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>
<dependentAssembly>
	<assemblyIdentity name="System.Memory" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
	<bindingRedirect oldVersion="0.0.0.0-4.0.1.2" newVersion="4.0.1.2" />
</dependentAssembly>

An example has been added in #97