Send .NET application data to Observe

This page describes how to install the Observe Agent, then instrument your .NET applications to send data to Observe.

Compatibility

To send .NET application data to Observe, you must have .NET 4.6.2+ on the following platforms:

  • x86
  • AMD64 (x86-64)
  • ARM64 (Experimental, CentOS images not supported)

See Supported .NET libraries and frameworks for the .NET compatibility matrix.

To manually instrument custom logic, see Instrumentation for .NET in the OpenTelemetry documentation.

Get the Observe Agent

Use the Add Data portal in the product to get the Observe Agent installed in your environment.

  1. From the left navigation rail, select Data & integrations > Add data.
  2. In the Observe Agent section, pick your environment, then follow the instructions to create a data ingest token and install the Observe Agent.

Depending on the environment you select, you are asked to select some options for installing and configuring the Observe Agent.

Customize your Observe Agent installation in Kubernetes with the following options:

  • Decide what data you want to collect: logs and/or metrics.
  • Whether or not to enable fleet monitoring.
  • Specify the environment value (deployment.environment.name) to enable usage and cost breakdowns.
  1. Follow the remainder of the instructions to install the Observe Agent and verify that your data is being received.

Install the OpenTelemetry zero-code instrumentation for your .NET apps

The instrumentation is distributed via an installer script. Follow the instructions for your operating system.

# Download the bash script
curl -sSfL https://github.com/open-telemetry/opentelemetry-dotnet-instrumentation/releases/latest/download/otel-dotnet-auto-install.sh -O

# Install core files
sh ./otel-dotnet-auto-install.sh

# Enable execution for the instrumentation script
chmod +x $HOME/.otel-dotnet-auto/instrument.sh

# Setup the instrumentation for the current shell session
. $HOME/.otel-dotnet-auto/instrument.sh

Instrument an ASP.NET application deployed on IIS

Run the following in Powershell.

📘

Note

The last command causes IIS to restart.

Import-Module "OpenTelemetry.DotNet.Auto.psm1"

Install-OpenTelemetryCore

Register-OpenTelemetryForIIS

Configure the instrumentation

Set the following environment variables. Replace the placeholders such as ${YOUR_SERVICE_NAME} with the actual information from your environment.

OTEL_SERVICE_NAME=${YOUR_SERVICE_NAME}
OTEL_RESOURCE_ATTRIBUTES=deployment.environment=${YOUR_APP_ENVIRONMENT}
OTEL_EXPORTER_OTLP_ENDPOINT=http://${YOUR_OBSERVE_AGENT_ENDPOINT}:4318 

Run your instrumented app

./MyNetApp

Instrument your .NET apps using OpenTelemetry NuGet packages

Use this approach when you can add NuGet packages to the application. If you cannot modify the app, want one machine-wide deployment for many apps, or are working with a legacy non-SDK-style project, Install the OpenTelemetry zero-code instrumentation for your .NET apps instead.

Install packages

Install these packages in every app:

dotnet add package OpenTelemetry.Extensions.Hosting
dotnet add package OpenTelemetry.Exporter.OpenTelemetryProtocol
dotnet add package OpenTelemetry.Instrumentation.Runtime
dotnet add package OpenTelemetry.Instrumentation.Process

Add packages for the host and libraries your app uses:

  • Web server, Minimal API, MVC, gRPC server: OpenTelemetry.Instrumentation.AspNetCore
  • Outbound HttpClient: OpenTelemetry.Instrumentation.Http
  • SQL Server: OpenTelemetry.Instrumentation.SqlClient
  • Redis: OpenTelemetry.Instrumentation.StackExchangeRedis
  • Outbound gRPC client: OpenTelemetry.Instrumentation.GrpcNetClient

For PostgreSQL, the default choice is usually to subscribe to Npgsql's native source with .AddSource("Npgsql"). Use Npgsql.OpenTelemetry only if you construct NpgsqlDataSource directly and need its extra tracing configuration.

Add a shared telemetry helper

Put the shared registration in OpenTelemetryExtensions.cs or a similar file.

using System.Reflection;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using OpenTelemetry.Logs;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;

namespace MyApp.Observability;

public static class OpenTelemetryExtensions
{
    public static IServiceCollection AddAppTelemetry(
        this IServiceCollection services,
        IConfiguration configuration)
    {
        var serviceName =
            configuration["OTEL_SERVICE_NAME"] ??
            Assembly.GetEntryAssembly()?.GetName().Name ??
            "app";

        var resourceBuilder = ResourceBuilder.CreateDefault()
            .AddService(serviceName)
            .AddEnvironmentVariableDetector();

        services.AddOpenTelemetry()
            .ConfigureResource(resource => resource
                .AddService(serviceName)
                .AddEnvironmentVariableDetector())
            .WithTracing(tracing => tracing
                .AddHttpClientInstrumentation()
                // Uncomment only the instrumentations your app uses:
                // .AddAspNetCoreInstrumentation(options => options.RecordException = true)
                // .AddGrpcClientInstrumentation()
                // .AddSqlClientInstrumentation()
                // .AddSource("Npgsql")
                .AddOtlpExporter())
            .WithMetrics(metrics => metrics
                .AddHttpClientInstrumentation()
                .AddRuntimeInstrumentation()
                .AddProcessInstrumentation()
                // Web apps only:
                // .AddAspNetCoreInstrumentation()
                .AddOtlpExporter());

        services.AddLogging(logging => logging.AddOpenTelemetry(options =>
        {
            options.SetResourceBuilder(resourceBuilder);
            options.AddOtlpExporter();
        }));

        return services;
    }
}

This helper registers traces, metrics, and logs in one place. The logging bridge also preserves trace-to-log correlation.

Call it from startup

For ASP.NET Core apps:

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddAppTelemetry(builder.Configuration);

For worker services or console apps:

var builder = Host.CreateApplicationBuilder(args);
builder.Services.AddAppTelemetry(builder.Configuration);

Enable gRPC attributes

For gRPC server spans, OpenTelemetry.Instrumentation.AspNetCore handles the server instrumentation, but RPC attributes are only emitted when this flag is enabled:

Next steps

Navigate to Traces in your Observe tenant to view traces from your application.