Set up EDOT .NET for ASP.NET applications on .NET Framework
Elastic Cloud Serverless projects are autoscaled environments, fully managed by Elastic and available on Elastic Cloud. Availability This functionality is generally available and ready for production usage. The Elastic Stack includes Elastic's core products such as Elasticsearch, Kibana, Logstash, and Beats. Availability This functionality is generally available and ready for production usage. Unless stated otherwise on the page, this functionality is available when your Elastic Stack is deployed on Elastic Cloud Hosted, Elastic Cloud Enterprise, Elastic Cloud on Kubernetes, and self-managed environments. This documentation corresponds to the latest patch available for each minor version. If you're not using the latest patch, check the release notes for changes. The Elastic Distribution of OpenTelemetry (EDOT) .NET SDK collects performance metrics, traces, and logs in OpenTelemetry format, and sends them to Elastic Observability. Availability This functionality is generally available and ready for production usage. This documentation corresponds to the latest patch available for each minor version. If you're not using the latest patch, check the release notes for changes.
Serverless Observability
Generally available
Stack
Generally available
EDOT .NET
Generally available
You can use EDOT .NET with ASP.NET applications by registering the OpenTelemetry SDK TelemetryHttpModule.
To get started with the Elastic Distribution of OpenTelemetry .NET, add the Elastic.OpenTelemetry NuGet package
reference to your project file:
<PackageReference Include="Elastic.OpenTelemetry" Version="<LATEST>" />
Replace the <LATEST> version placeholder with the latest available package from NuGet.org.
EDOT .NET includes a transitive dependency on the OpenTelemetry SDK, so you do not need to add the OpenTelemetry SDK package to your project directly. However, you can explicitly add the OpenTelemetry SDK as a dependency if you want to opt into newer SDK versions.
For ASP.NET applications, you also need to install the contrib instrumentation library. Add the OpenTelemetry.Instrumentation.AspNet NuGet package reference to your project file:
<PackageReference Include="OpenTelemetry.Instrumentation.AspNet" Version="<LATEST>" />
Replace the <LATEST> version placeholder with the latest available package from NuGet.org. If you use the Visual Studio NuGet Package Manager or the .NET CLI to install this package, you will need to allow prerelease package versions.
Next, modify your Web.Config file to add a required HttpModule:
<system.webServer>
<modules>
<add
name="TelemetryHttpModule"
type="OpenTelemetry.Instrumentation.AspNet.TelemetryHttpModule,
OpenTelemetry.Instrumentation.AspNet.TelemetryHttpModule"
preCondition="integratedMode,managedHandler" />
</modules>
</system.webServer>
Finally, initialize ASP.NET instrumentation in your Global.asax.cs file along with other OpenTelemetry initialization:
using OpenTelemetry;
using OpenTelemetry.Resources;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
namespace AspNetFramework
{
public class WebApiApplication : HttpApplication
{
private OpenTelemetrySdk _sdk;
protected void Application_Start()
{
_sdk = OpenTelemetrySdk.Create(builder => builder
.WithElasticDefaults()
.ConfigureResource(r => r.AddService("MyClassicAspNetApp")));
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
protected void Application_End()
{
_sdk?.Dispose();
}
}
}
The preceding code:
- Imports the required types from the
OpenTelemetrynamespace. - Creates an instance of the
OpenTelemetrySdkusing its factoryCreatemethod. - Configures the
IOpenTelemetryBuilderby passing a lambda. - Enables EDOT .NET and its opinionated defaults by calling
WithElasticDefaultson theIOpenTelemetryBuilder. - Calls
ConfigureResourceto configure the name for the service.
By default, EDOT .NET uses instrumentation assembly scanning and will detect the OpenTelemetry.Instrumentation.AspNet instrumentation, registering it with the OpenTelemetry SDK. Traces for ASP.NET requests are automatically observed and exported over OTLP without further configuration.
When calling OpenTelemetrySdk.Create a dedicated IServiceCollection and IServiceProvider are created for the SDK and shared by all signals. An IConfiguration is created automatically from environment variables. The recommended method to configure the OpenTelemetry SDK is through environment variables. At a minimum, set the environment variables used to configure the OTLP exporter using any suitable method for your operating system.
"OTEL_EXPORTER_OTLP_ENDPOINT" = "https://{MyServerlessEndpoint}.apm.us-east-1.aws.elastic.cloud:443",
"OTEL_EXPORTER_OTLP_HEADERS" = "Authorization=ApiKey {MyEncodedApiKey}"
For further advice on configuring OpenTelemetry in ASP.NET application visit the OpenTelemetry documentation.