Skip to main content
OpenTelemetry provides a unified approach to collecting telemetry data from your .NET apps. This guide explains how to configure OpenTelemetry in a .NET app to send telemetry data to Axiom using the OpenTelemetry SDK.

Prerequisites

  • Install the .NET 6.0 SDK on your development machine.
  • Use your existing .NET app or start with the sample provided in the program.cs below.

Install dependencies

Run the following command in your terminal to install the necessary NuGet packages:
Replace the dotnet.csproj file in your project with the following:
The dotnet.csproj file is important for defining your project’s settings, including target framework, nullable reference types, and package references. It informs the .NET SDK and build tools about the components and configurations your project requires.

Core app

program.cs is the core of the .NET app. It uses ASP.NET to create a simple web server. The server has an endpoint /rolldice that returns a random number, simulating a basic API.

Exporter

The tracing.cs file sets up the OpenTelemetry instrumentation. It configures the OTLP (OpenTelemetry Protocol) exporters for traces and initializes the ASP.NET SDK with automatic instrumentation capabilities.
Replace the value of the serviceName variable with the name of the service you want to trace. This is used for identifying and categorizing trace data, particularly in systems with multiple services.Replace API_TOKEN with the Axiom API token you have generated. For added security, store the API token in an environment variable.Replace DATASET_NAME with the name of the Axiom dataset where you send your data.Replace AXIOM_DOMAIN with the base domain of your edge deployment. For more information, see Edge deployments.

Run the instrumented app

  1. Run in local development mode using the development settings in appsettings.development.json. Ensure your Axiom API token and dataset name are correctly set in tracing.cs.
  2. Before deploying, run in production mode by switching to appsettings.json for production settings. Ensure your Axiom API token and dataset name are correctly set in tracing.cs.
  3. Run your app with dotnet run. Your app starts and you can interact with it by sending requests to the /rolldice endpoint.
For example, if you are using port 8080, your app is accessible locally at http://localhost:8080/rolldice. This URL will direct your requests to the /rolldice endpoint of your server running on your local machine.

Observe the telemetry data

As you interact with your app, traces are collected and exported to Axiom where you can monitor and analyze your app’s performance and behavior.
  1. Log into your Axiom account and click the Datasets or Stream tab.
  2. Select your dataset from the list.
  3. From the list of fields, click on the trace_id, to view your spans.

Dynamic OpenTelemetry Traces dashboard

The data can then be further viewed and analyzed in the traces dashboard, providing insights into the performance and behavior of your app.
  1. Log into your Axiom account, select Dashboards, and click on the traces dashboard named after your dataset.
  2. View the dashboard which displays your total traces, incoming spans, average span duration, errors, slowest operations, and top 10 span errors across services.

Send data from an existing .NET project

Manual Instrumentation

Manual instrumentation involves adding code to create, configure, and manage telemetry data, such as traces and spans, providing control over what data is collected.
  1. Initialize ActivitySource. Define an ActivitySource to create activities (spans) for tracing specific operations within your app.
  1. Start and stop activities. Manually start activities (spans) at the beginning of the operations you want to trace and stop them when the operations complete. You can add custom attributes to these activities for more detailed tracing.
  1. Add custom attributes. Enhance activities with custom attributes to provide additional context, making it easier to analyze telemetry data.

Automatic Instrumentation

Automatic instrumentation uses the OpenTelemetry SDK and additional libraries to automatically generate telemetry data for certain operations, such as incoming HTTP requests and database queries.
  1. Configure OpenTelemetry SDK. Use the OpenTelemetry SDK to configure automatic instrumentation in your app. This typically involves setting up a TracerProvider in your program.cs or startup configuration, which automatically captures telemetry data from supported libraries.
    Replace API_TOKEN with the Axiom API token you have generated. For added security, store the API token in an environment variable.Replace DATASET_NAME with the name of the Axiom dataset where you send your data.Replace AXIOM_DOMAIN with the base domain of your edge deployment. For more information, see Edge deployments.
  2. Install and configure additional OpenTelemetry instrumentation packages as needed, based on the technologies your app uses. For example, to automatically trace SQL database queries, you might add the corresponding database instrumentation package.
  3. With automatic instrumentation set up, no further code changes are required for tracing basic operations. The OpenTelemetry SDK and its instrumentation packages handle the creation and management of traces for supported operations.

Reference

List of OpenTelemetry trace fields

List of imported libraries

OpenTelemetry

<PackageReference Include="OpenTelemetry" Version="1.7.0" /> This is the core SDK for OpenTelemetry in .NET. It provides the foundational tools needed to collect and manage telemetry data within your .NET apps. It’s the base upon which all other OpenTelemetry instrumentation and exporter packages build.

OpenTelemetry.Exporter.Console

<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.7.0" /> This package allows apps to export telemetry data to the console. It’s primarily useful for development and testing purposes, offering a simple way to view the telemetry data your app generates in real time.

OpenTelemetry.Exporter.OpenTelemetryProtocol

<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.7.0" /> This package enables your app to export telemetry data using the OpenTelemetry Protocol (OTLP) over gRPC or HTTP. It’s vital for sending data to observability platforms that support OTLP, ensuring your telemetry data can be easily analyzed and monitored across different systems.

OpenTelemetry.Extensions.Hosting

<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.7.0" /> Designed for .NET apps, this package integrates OpenTelemetry with the .NET Generic Host. It simplifies the process of configuring and managing the lifecycle of OpenTelemetry resources such as TracerProvider, making it easier to collect telemetry data in apps that use the hosting model.

OpenTelemetry.Instrumentation.AspNetCore

<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.7.1" /> This package is designed for instrumenting ASP.NET Core apps. It automatically collects telemetry data about incoming requests and responses. This is important for monitoring the performance and reliability of web apps and APIs built with ASP.NET Core.

OpenTelemetry.Instrumentation.Http

<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.6.0-rc.1" /> This package provides automatic instrumentation for HTTP clients in .NET apps. It captures telemetry data about outbound HTTP requests, including details such as request and response headers, duration, success status, and more. It’s key for understanding external dependencies and interactions in your app.