Skip to main content
Sumo Logic

.NET OpenTelemetry auto-instrumentation

Automatic instrumentation of the .NET applications is a very easy task. The simplest way to start capturing telemetry data is to implement the solution coming from OpenTelemetry-dotNet. All the libraries shipped with the OpenTelemetry-dotNet repository support all the officially supported versions of .NET Core and .NET framework with an except for .NET Framework 3.5 SP1. See a list of the supported libraries.

How to instrument your ASP.NET Core application?

There are a few simple steps to instrument the application and obtain telemetry data.

1. Packages installation

The installation of the packages listed below is required to apply the instrumentation and export telemetry data.

$ dotnet add package OpenTelemetry -v 0.7.0-beta.1
$ dotnet add package OpenTelemetry.Instrumentation.AspNetCore -v 0.7.0-beta.1
$ dotnet add package OpenTelemetry.Exporter.OpenTelemetryProtocol -v 0.7.0-beta.1
$ dotnet add package OpenTelemetry.Extensions.Hosting -v 0.7.0-beta.1

2. Instrumentation with OpenTelemetryProtocol exporter

In this step, all the magic related to code instrumentation will happen. To enable instrumentation in the application it is enough to add the code below into your Startup class in the ConfigureServices method.

In the code line .AddAspNetCoreInstrumentation() add the OpenTelemetry instrumentation to the .NET Core application. Line .SetResource() sets the service name. The last line .AddOtlpExporter() is responsible for the configuration of the OpenTelemetryProtocol Exporter. It sets the OpenTelemetryProtocol Collector endpoint. In the example below Endpoint points to the default Sumologic Kubernetes Collector.

public void ConfigureServices(IServiceCollection services)
{
   services.AddOpenTelemetryTracing((builder) => builder
       .AddAspNetCoreInstrumentation()
       .SetResource(OpenTelemetry.Resources.Resources.CreateServiceResource("my-app"))
       .AddOtlpExporter(options =>
       {
           options.Endpoint = new Uri("collection-sumologic-otelcol.sumologic:55680")
               .ToString();
       })
   );
}

How to instrument your ASP.NET application?

Instrumentation of the .NET application requires a little more effort but is still simple.

1. Packages installation

The installation of the packages listed below is required to apply the instrumentation and export telemetry data.

$ dotnet add package OpenTelemetry -v 0.7.0-beta.1
$ dotnet add package OpenTelemetry.Instrumentation.AspNet -v 0.7.0-beta.1
$ dotnet add package OpenTelemetry.Exporter.OpenTelemetryProtocol -v 0.7.0-beta.1

2. Web.config file changes

The Web server, in addition to the code changes required for application instrumentation, needs a supplementary module called TelemetryCorrelationHttpModule. The code below is needed when the IIS web server is used.

<system.webServer>
   <modules>
       <add name="TelemetryCorrelationHttpModule"
            type="Microsoft.AspNet.TelemetryCorrelation.TelemetryCorrelationHttpModule,
                  Microsoft.AspNet.TelemetryCorrelation"
            preCondition="integratedMode,managedHandler" />
   </modules>
</system.webServer>

3. Instrumentation with OpenTelemetryProtocol exporter

To enable .NET instrumentation some additional code has to be introduced. The code needs to be added at the application startup phase usually located in the Global.asax.cs file. A few things in the code below were added.

private TracerProvider tracerProvider was added as a class variable to create the trace provider in the next step. In the Application_Start() method, Sdk.CreateTracerProviderBuilder() was introduced. Thanks to this it is possible to configure what can be instrumented and where the telemetry data will be sent.

.AddAspNetInstrumentation() adds .NET instrumentation. The service name is set by the .SetResource() method. An exporter has to be configured to send the spans. In this case, it is the OpenTelemetryProtocol exporter which is configured by the .AddOtlpExporter() method. It sets the OpenTelemetryProtocol Collector endpoint. In the example below Endpoint points to the default Sumologic Kubernetes Collector.

public class MvcApplication : System.Web.HttpApplication
{   
   private TracerProvider tracerProvider;
  
   protected void Application_Start()
   {   
       this.tracerProvider = Sdk.CreateTracerProviderBuilder()
           .AddAspNetInstrumentation()
           .SetResource(OpenTelemetry.Resources.Resources.
               CreateServiceResource("my-app"))
           .AddOtlpExporter(options =>
           {
               options.Endpoint =
                   new Uri("collection-sumologic-otelcol.sumologic:55680")
                       .ToString();
           })
           .Build();
      
       AreaRegistration.RegisterAllAreas();
       FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
       RouteConfig.RegisterRoutes(RouteTable.Routes);
       BundleConfig.RegisterBundles(BundleTable.Bundles);
   }
  
   protected void Application_End()
   {
       this.tracerProvider?.Dispose();
   }
}

Additional libraries instrumentation

In the case of the other libraries like GrpcNetClient, HttpClient, StackExchangeRedis, and SqlClient, it is highly recommended to install specific packages by adding a single line of code in the application startup methods. This will ensure full end-to-end visibility including the health of client calls. 

GrpcNetClient instrumentation

Requires installation of the package:

dotnet add package OpenTelemetry.Instrumentation.GrpcNetClient -v 0.7.0-beta.1

and a small code change, in addition to the GrpcClient instrumentation .AddGrpcClientInstrumentation(). More details can be found here.

  • .NET Core code example:

       services.AddOpenTelemetryTracing((builder) => builder
           .AddAspNetCoreInstrumentation()
           .AddGrpcClientInstrumentation()

     
  • .NET code example:

       this.tracerProvider = Sdk.CreateTracerProviderBuilder()
               .AddAspNetInstrumentation()
               .AddGrpcClientInstrumentation()
HttpClient instrumentation

Requires installation of the package:

$ dotnet add package OpenTelemetry.Instrumentation.Http -v 0.7.0-beta.1

and small code change, in addition to the HttpClient instrumentation .AddHttpClientInstrumentation(). More details can be found here.

  • .NET Core code example

    services.AddOpenTelemetryTracing((builder) => builder
           .AddAspNetCoreInstrumentation()
           .AddHttpClientInstrumentation()

     

  • .NET code example

    this.tracerProvider = Sdk.CreateTracerProviderBuilder()
               .AddAspNetInstrumentation()
               .AddHttpClientInstrumentation()

Redis instrumentation

Requires installation of the package:

$ dotnet add package OpenTelemetry.Instrumentation.StackExchangeRedis -v 0.7.0-beta.1

and small code change, in addition to the Redis instrumentation .AddRedisInstrumentation(connection) Redis instrumentation requires a connection to the Redis server. More details can be found here.

  • .NET Core code example

    services.AddOpenTelemetryTracing((builder) => builder
           .AddAspNetCoreInstrumentation()
           .AddRedisInstrumentation(connection)

     

  • .NET code example

    this.tracerProvider = Sdk.CreateTracerProviderBuilder()
               .AddAspNetInstrumentation()
               .AddRedisInstrumentation(connection)

SqlClient instrumentation

Requires installation of the package:

$ dotnet add package OpenTelemetry.Instrumentation.SqlClient -v 0.7.0-beta.1

and small code change, in addition to the SqlClient instrumentation .AddSqlClientInstrumentation(). More details can be found here.

  • .NET Core code example

    services.AddOpenTelemetryTracing((builder) => builder
           .AddAspNetCoreInstrumentation()
           .AddSqlClientInstrumentation()

  • .NET code example

    this.tracerProvider = Sdk.CreateTracerProviderBuilder()
               .AddAspNetInstrumentation()
               .AddSqlClientInstrumentation()