This document contains instructions on how to set up OpenTelemetry instrumentation in your .NET applications and view your application traces in SigNoz.
Requirements
.NET SDK (.NET 5.0 or Later)
Send Traces to SigNoz Cloud
Based on your application environment, you can choose the setup below to send traces to SigNoz Cloud.
There are two ways to send data to SigNoz Cloud.
- Send traces directly to SigNoz Cloud
- Send traces via OTel Collector binary (recommended)
Send traces directly to SigNoz Cloud - No Code Automatic Instrumentation (recommended)
Step 1: Create a shell script with the following content
# 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
# Run your application with instrumentation
OTEL_SERVICE_NAME=<service_name> OTEL_TRACES_EXPORTER=otlp OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf OTEL_RESOURCE_ATTRIBUTES=deployment.environment=staging,service.version=1.0.0 OTEL_EXPORTER_OTLP_ENDPOINT=ingest.<region>.signoz.cloud OTEL_EXPORTER_OTLP_HEADERS=signoz-ingestion-key=<your-ingestion-key> ./MyNetApp
- Set the
<region>to match your SigNoz Cloud region - Replace
<your-ingestion-key>with your SigNoz ingestion key <service_name>is name of your service
If you are doing it on mac os , you will need to install coreutils, you can do it by using the following command
brew install coreutils
Send traces directly to SigNoz Cloud - Code Level Automatic Instrumentation
Step 1: Installing the OpenTelemetry dependency packages:
dotnet add package OpenTelemetry
dotnet add package OpenTelemetry.Exporter.OpenTelemetryProtocol
dotnet add package OpenTelemetry.Extensions.Hosting
dotnet add package OpenTelemetry.Instrumentation.Runtime
dotnet add package OpenTelemetry.Instrumentation.AspNetCore
dotnet add package OpenTelemetry.AutoInstrumentation
Step 2: Adding OpenTelemetry as a service and configuring exporter options in Program.cs:
In your Program.cs file, add OpenTelemetry as a service. Here, we are configuring these variables:
- Set the
<region>to match your SigNoz Cloud region - Replace
<your-ingestion-key>with your SigNoz ingestion key <service_name>is name of your service
Here’s a sample Program.cs file with the configured variables.
using System.Diagnostics;
using OpenTelemetry.Exporter;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
var builder = WebApplication.CreateBuilder(args);
// Configure OpenTelemetry with tracing and auto-start.
builder.Services.AddOpenTelemetry()
.ConfigureResource(resource =>
resource.AddService(serviceName: "sample-net-app"))
.WithTracing(tracing => tracing
.AddAspNetCoreInstrumentation()
.AddOtlpExporter(otlpOptions =>
{
//SigNoz Cloud Endpoint
otlpOptions.Endpoint = new Uri("https://ingest.<region>.signoz.cloud:443");
otlpOptions.Protocol = OtlpExportProtocol.Grpc;
//SigNoz Cloud account Ingestion key
string headerKey = "signoz-ingestion-key";
string headerValue = "<your-ingestion-key>";
string formattedHeader = $"{headerKey}={headerValue}";
otlpOptions.Headers = formattedHeader;
}));
var app = builder.Build();
// The index route ("/") is set up to write out the OpenTelemetry trace information on the response:
app.MapGet("/", () => $"Hello World! OpenTelemetry Trace: {Activity.Current?.Id}");
app.Run();
The program uses the OpenTelemetry.Instrumentation.AspNetCore package to automatically create traces for incoming ASP.NET Core requests.
The OpenTelemetry.Exporter.Options get or set the target to which the exporter is going to send traces. Here, we’re configuring it to send traces to the SigNoz cloud. The target must be a valid Uri with the scheme (http or https) and host and may contain a port and a path.
This is done by configuring an OpenTelemetry TracerProvider using extension methods and setting it to auto-start when the host is started.
You can find your SigNoz cloud address and ingestion key under the settings of your SigNoz cloud account.
Step 3. Running the .NET application:
dotnet build
dotnet run
Step 4: Generating some load data and checking your application in SigNoz UI
Once your application is running, generate some traffic by interacting with it.
In the SigNoz account, open the Services tab. Hit the Refresh button on the top right corner, and your application should appear in the list of Applications. Ensure that you're checking data for the time range filter applied in the top right corner. You might have to wait for a few seconds before the data appears on SigNoz UI.
Send traces via OTel Collector binary
Send traces via OTel Collector binary - No Code Automatic Instrumentation
Step 1: Create a shell script with the following content
# 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
# Run your application with instrumentation
OTEL_SERVICE_NAME=<service_name> OTEL_TRACES_EXPORTER=otlp OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf OTEL_RESOURCE_ATTRIBUTES=deployment.environment=staging,service.version=1.0.0 OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318/v1/traces ./MyNetApp
<service_name>is name of your service
If you are doing it on mac os , you will need to install coreutils, you can do it by using the following command
brew install coreutils
Send traces via OTel Collector binary - Code Level Automatic Instrumentation
Step 1: Setting up OpenTelemetry Collector binary as an agent in your machine
OpenTelemetry Collector binary helps to collect logs, hostmetrics, resource and infra attributes. It is recommended to install OTel Collector binary to collect and send traces to SigNoz cloud. You can correlate signals and have rich contextual data through this way.
Go to setup OTel Collector binary to install Otel Collector as agent that will collect telemetry data from your sample dotnet app and send it to SigNoz cloud.
Step 2: Installing the OpenTelemetry dependency packages:
Install the following dependencies in your application.
dotnet add package OpenTelemetry
dotnet add package OpenTelemetry.Exporter.OpenTelemetryProtocol
dotnet add package OpenTelemetry.Extensions.Hosting
dotnet add package OpenTelemetry.Instrumentation.Runtime
dotnet add package OpenTelemetry.Instrumentation.AspNetCore
dotnet add package OpenTelemetry.AutoInstrumentation
Step 2: Adding OpenTelemetry as a service and configuring exporter options in Program.cs:
In your Program.cs file, add OpenTelemetry as a service. Here, we are configuring these variables:
serviceName- It is the name of your service.otlpOptions.Endpoint- It is the endpoint for your OTel Collector binary agent.
Here’s a sample Program.cs file with the configured variables.
using System.Diagnostics;
using OpenTelemetry.Exporter;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
var builder = WebApplication.CreateBuilder(args);
// Configure OpenTelemetry with tracing and auto-start.
builder.Services.AddOpenTelemetry()
.ConfigureResource(resource =>
resource.AddService(serviceName: "sample-net-app"))
.WithTracing(tracing => tracing
.AddAspNetCoreInstrumentation()
.AddOtlpExporter(otlpOptions =>
{
otlpOptions.Endpoint = new Uri("http://localhost:4317");
otlpOptions.Protocol = OtlpExportProtocol.Grpc;
}));
var app = builder.Build();
//The index route ("/") is set up to write out the OpenTelemetry trace information on the response:
app.MapGet("/", () => $"Hello World! OpenTelemetry Trace: {Activity.Current?.Id}");
app.Run();
The program uses the OpenTelemetry.Instrumentation.AspNetCore package to automatically create traces for incoming ASP.NET Core requests.
The OpenTelemetry.Exporter.Options get or set the target to which the exporter is going to send traces. Here, we’re configuring it to send traces to the OTel collector agent. The target must be a valid Uri with the scheme (http or https) and host and may contain a port and a path.
This is done by configuring an OpenTelemetry TracerProvider using extension methods and setting it to auto-start when the host is started.
Step 4. Running the .NET application:
dotnet build
dotnet run
Step 5: Generating some load data and checking your application in SigNoz UI
After the Otel collector is all set and running, and your too application is running, generate some traffic by interacting with it.
In the SigNoz account, open the Services tab. Hit the Refresh button on the top right corner, and your application should appear in the list of Applications. Ensure that you're checking data for the time range filter applied in the top right corner. You might have to wait for a few seconds before the data appears on SigNoz UI.
You can auto-instrument sending traces from the .NET application using one of the following methods:
- Using Kubernetes OTel Operator (recommended)
- Using OTel Collector Agent
K8s OTel Operator Based Automatic Instrumentation (recommended)
Send traces directly to SigNoz Cloud - Using K8s OTel Operator
For .NET application deployed on Kubernetes, you can auto-instrument the traces using Kubernetes OpenTelemetry Operator.
An OpenTelemetry Operator is a Kubernetes Operator that manages OpenTelemetry Collectors and auto-instrumentation of workloads. It basically simplifies the deployment and management of OpenTelemetry in a Kubernetes environment.
The OpenTelemetry Operator provides two Custom Resource Definitions (CRDs):
OpenTelemetryCollectorInstrumentation
The OpenTelemetryCollector CRD allows you to deploy and manage OpenTelemetry Collectors in your Kubernetes cluster.
The Instrumentation CRD allows you to configure and inject OpenTelemetry auto-instrumentation libraries into your workloads.
Here are the steps you need to follow to auto-instrument .NET application using OTel Operator:
Step 1. Install cert-manager
Run the following commands to apply cert manager.
kubectl apply -f https://github.com/cert-manager/cert-manager/releases/download/v1.16.1/cert-manager.yaml
kubectl wait --for=condition=Available deployments/cert-manager -n cert-manager
Step 2. Install OpenTelemetry Operator
To install the operator in the existing K8s cluster, run the following command:
kubectl apply -f https://github.com/open-telemetry/opentelemetry-operator/releases/download/v0.116.0/opentelemetry-operator.yaml
Installing the OpenTelemetry Operator sets up the necessary components and configurations to enable the observability and monitoring of applications running in the cluster.
Step 3. Setup the OpenTelemetry Collector instance
Once the opentelemetry-operator has been deployed, you can proceed with the creation of the OpenTelemetry Collector (otelcol) instance. The OpenTelemetry Collector collects, processes, and exports telemetry data.
There are different deployment modes for the OpenTelemetryCollector, and you can specify them in the spec.mode section of the custom resource. The available deployment modes are:
- Daemonset
- Sidecar
- StatefulSet
- Deployment (default mode)
The default method - the Deployment mode, will be used here.
To create a simple instance of the OpenTelemetry Collector, create a file otel-collector.yaml with the following contents:
apiVersion: opentelemetry.io/v1alpha1
kind: OpenTelemetryCollector
metadata:
name: otel-collector
spec:
mode: deployment
config: |
receivers:
otlp:
protocols:
grpc:
endpoint: 0.0.0.0:4317
http:
endpoint: 0.0.0.0:4318
processors:
batch: {}
resource/env:
attributes:
- key: deployment.environment
value: prod # can be dev, prod, staging etc. based on your environment
action: upsert
exporters:
debug: {}
otlp:
endpoint: "ingest.<region>.signoz.cloud:443" # replace <region> with your region of SigNoz Cloud
tls:
insecure: false
headers:
"signoz-ingestion-key": "<your-ingestion-key>" # Obtain from https://{your-signoz-tenant-url}/settings/ingestion-settings
service:
pipelines:
traces:
receivers: [otlp]
processors: [batch, resource/env]
exporters: [otlp]
- Set the
<region>to match your SigNoz Cloud region - Replace
<your-ingestion-key>with your SigNoz ingestion key
Apply the above yaml file using the following command:
kubectl apply -f otel-collector.yaml
Step 4. Setup the Instrumentation instance
Once the OpenTelemetry Collector instance has been deployed, the next step will be to create an instrumentation instance, which will be responsible for sending OTLP data to the OTel Collector.
Create a file instrumentation.yaml with the following contents:
apiVersion: opentelemetry.io/v1alpha1
kind: Instrumentation
metadata:
name: traces-instrumentation
spec:
exporter:
endpoint: https://ingest.<region>.signoz.cloud:443 # replace <region> with your region of SigNoz Cloud
env:
- name: OTEL_EXPORTER_OTLP_HEADERS
value: signoz-ingestion-key="<signoz-token>" # Obtain from https://{your-signoz-url}/settings/ingestion-settings
- name: OTEL_EXPORTER_OTLP_INSECURE
value: "false"
propagators:
- tracecontext
- baggage
- b3
sampler:
type: parentbased_traceidratio
argument: "1"
dotnet:
image: ghcr.io/open-telemetry/opentelemetry-operator/autoinstrumentation-dotnet:latest
env:
- name: OTEL_EXPORTER_OTLP_ENDPOINT
value: http://otel-collector-collector:4318 #This is formed as: "http://<OpenTelemetryCollector name>-collector:4318"
Apply the above instrumentation using the following command:
kubectl apply -f instrumentation.yaml
Step 5. Auto-instrument your .NET app with OpenTelemetry
Create deployment.yaml file for your .NET application as follows:
apiVersion: apps/v1
kind: Deployment
metadata:
name: dotnet-app
spec:
selector:
matchLabels:
app: dotnet-app
replicas: 1
template:
metadata:
labels:
app: dotnet-app
annotations:
instrumentation.opentelemetry.io/inject-dotnet: "true"
resource.opentelemetry.io/service.name: "dotnet-app"
spec:
containers:
- name: app
image: dotnet-app:latest
ports:
- containerPort: 8080
It is important to add the following annotation under spec > template > metadata > annotations:
instrumentation.opentelemetry.io/inject-dotnet: "true"
This helps in auto-instrumenting the traces from the .NET application.
Apply the deployment using the following command:
kubectl apply -f deployment.yaml
With this, the auto-instrumentation of traces for .NET application is ready.
Step 6. Running the .NET application
In order to run the application on port 8080, run the following commands:
export POD_NAME=$(kubectl get pod -l app=<service-name> -o jsonpath="{.items[0].metadata.name}") # service name is `dotnet-app` in this case.
kubectl port-forward ${POD_NAME} 8080:8080
You can now access the application on port 8080.
You can validate if your application is sending traces to SigNoz cloud here.
In case you encounter an issue where all applications do not get listed in the services section then please refer to the troubleshooting section.
For .NET application deployed on Kubernetes, you need to install OTel Collector agent in your k8s infra to collect and send traces to SigNoz Cloud. You can find the instructions to install OTel Collector agent here.
Once you have set up OTel Collector agent, you can proceed with OpenTelemetry .NET instrumentation by following either of the two:
No Code Automatic Instrumentation (recommended)
Send traces directly to SigNoz Cloud - No Code Automatic Instrumentation (recommended)
Step 1: Create a shell script with the following content
# 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
# Run your application with instrumentation
OTEL_SERVICE_NAME=<service_name> OTEL_TRACES_EXPORTER=otlp OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf OTEL_RESOURCE_ATTRIBUTES=deployment.environment=staging,service.version=1.0.0 OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318/v1/traces ./MyNetApp
<service_name>is name of your service
Code Level Automatic Instrumentation
Step 1: Installing the OpenTelemetry dependency packages:
Install the following dependencies in your application.
dotnet add package OpenTelemetry
dotnet add package OpenTelemetry.Exporter.OpenTelemetryProtocol
dotnet add package OpenTelemetry.Extensions.Hosting
dotnet add package OpenTelemetry.Instrumentation.Runtime
dotnet add package OpenTelemetry.Instrumentation.AspNetCore
dotnet add package OpenTelemetry.AutoInstrumentation
Step 2: Adding OpenTelemetry as a service and configuring exporter options in Program.cs:
In your Program.cs file, add OpenTelemetry as a service. Here, we are configuring these variables:
serviceName- It is the name of your service.otlpOptions.Endpoint- It is the endpoint for your OTel Collector agent.
Here’s a sample Program.cs file with the configured variables.
using System.Diagnostics;
using OpenTelemetry.Exporter;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
var builder = WebApplication.CreateBuilder(args);
// Configure OpenTelemetry with tracing and auto-start.
builder.Services.AddOpenTelemetry()
.ConfigureResource(resource =>
resource.AddService(serviceName: "sample-net-app"))
.WithTracing(tracing => tracing
.AddAspNetCoreInstrumentation()
.AddOtlpExporter(otlpOptions =>
{
otlpOptions.Endpoint = new Uri("http://localhost:4317");
otlpOptions.Protocol = OtlpExportProtocol.Grpc;
}));
var app = builder.Build();
//The index route ("/") is set up to write out the OpenTelemetry trace information on the response:
app.MapGet("/", () => $"Hello World! OpenTelemetry Trace: {Activity.Current?.Id}");
app.Run();
The program uses the OpenTelemetry.Instrumentation.AspNetCore package to automatically create traces for incoming ASP.NET Core requests.
The OpenTelemetry.Exporter.Options get or set the target to which the exporter is going to send traces. Here, we’re configuring it to send traces to the OTel Collector agent. The target must be a valid Uri with the scheme (http or https) and host and may contain a port and a path.
This is done by configuring an OpenTelemetry TracerProvider using extension methods and setting it to auto-start when the host is started.
Step 4. Running the .NET application:
dotnet build
dotnet run
Step 5: Generating some load data and checking your application in SigNoz UI
After the Otel collector is all set and running, and your too application is running, generate some traffic by interacting with it.
In the SigNoz account, open the Services tab. Hit the Refresh button on the top right corner, and your application should appear in the list of Applications. Ensure that you're checking data for the time range filter applied in the top right corner. You might have to wait for a few seconds before the data appears on SigNoz UI.
There are two ways to send data to SigNoz Cloud.
- Send traces directly to SigNoz Cloud
- Send traces via OTel Collector binary (recommended)
Send traces directly to SigNoz Cloud
Send traces directly to SigNoz Cloud - No Code Automatic Instrumentation (recommended)
Step 1: Download installation scripts
$module_url = "https://github.com/open-telemetry/opentelemetry-dotnet-instrumentation/releases/latest/download/OpenTelemetry.DotNet.Auto.psm1"
$download_path = Join-Path $env:temp "OpenTelemetry.DotNet.Auto.psm1"
Invoke-WebRequest -Uri $module_url -OutFile $download_path -UseBasicParsing
Step 2: Execute following script to download automatic instrumentation for your development environment
Import-Module $download_path
Install-OpenTelemetryCore
Step 3: Set variables and execute auto instrumentation
$env:OTEL_TRACES_EXPORTER="otlp"
$env:OTEL_EXPORTER_OTLP_ENDPOINT="ingest.<region>.signoz.cloud:443"
$env:OTEL_SERVICE_NAME="<service_name>"
$env:OTEL_EXPORTER_OTLP_HEADERS="signoz-ingestion-key=<your-ingestion-key>"
$env:OTEL_DOTNET_AUTO_TRACES_CONSOLE_EXPORTER_ENABLED="true"
$env:OTEL_DOTNET_AUTO_LOGS_CONSOLE_EXPORTER_ENABLED="true"
Register-OpenTelemetryForCurrentSession -OTelServiceName "<service_name>"
dotnet run
- Set the
<region>to match your SigNoz Cloud region - Replace
<your-ingestion-key>with your SigNoz ingestion key <service_name>is name of your service- Replace
dotnet runwith the run command of your application
Send traces directly to SigNoz Cloud - Code Level Automatic Instrumentation
Step 1: Installing the OpenTelemetry dependency packages:
dotnet add package OpenTelemetry
dotnet add package OpenTelemetry.Exporter.OpenTelemetryProtocol
dotnet add package OpenTelemetry.Extensions.Hosting
dotnet add package OpenTelemetry.Instrumentation.Runtime
dotnet add package OpenTelemetry.Instrumentation.AspNetCore
dotnet add package OpenTelemetry.AutoInstrumentation
Step 2: Adding OpenTelemetry as a service and configuring exporter options in Program.cs:
In your Program.cs file, add OpenTelemetry as a service. Here, we are configuring these variables:
- Set the
<region>to match your SigNoz Cloud region - Replace
<your-ingestion-key>with your SigNoz ingestion key <service_name>is name of your service
Here’s a sample Program.cs file with the configured variables.
using System.Diagnostics;
using OpenTelemetry.Exporter;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
var builder = WebApplication.CreateBuilder(args);
// Configure OpenTelemetry with tracing and auto-start.
builder.Services.AddOpenTelemetry()
.ConfigureResource(resource =>
resource.AddService(serviceName: "sample-net-app"))
.WithTracing(tracing => tracing
.AddAspNetCoreInstrumentation()
.AddOtlpExporter(otlpOptions =>
{
//SigNoz Cloud Endpoint
otlpOptions.Endpoint = new Uri("https://ingest.<region>.signoz.cloud:443");
otlpOptions.Protocol = OtlpExportProtocol.Grpc;
//SigNoz Cloud account Ingestion key
string headerKey = "signoz-ingestion-key";
string headerValue = "<your-ingestion-key>";
string formattedHeader = $"{headerKey}={headerValue}";
otlpOptions.Headers = formattedHeader;
}));
var app = builder.Build();
// The index route ("/") is set up to write out the OpenTelemetry trace information on the response:
app.MapGet("/", () => $"Hello World! OpenTelemetry Trace: {Activity.Current?.Id}");
app.Run();
The program uses the OpenTelemetry.Instrumentation.AspNetCore package to automatically create traces for incoming ASP.NET Core requests.
The OpenTelemetry.Exporter.Options get or set the target to which the exporter is going to send traces. Here, we’re configuring it to send traces to the SigNoz cloud. The target must be a valid Uri with the scheme (http or https) and host and may contain a port and a path.
This is done by configuring an OpenTelemetry TracerProvider using extension methods and setting it to auto-start when the host is started.
Step 3. Running the .NET application:
dotnet build
dotnet run
Step 4: Generating some load data and checking your application in SigNoz UI
Once your application is running, generate some traffic by interacting with it.
In the SigNoz account, open the Services tab. Hit the Refresh button on the top right corner, and your application should appear in the list of Applications. Ensure that you're checking data for the time range filter applied in the top right corner. You might have to wait for a few seconds before the data appears on SigNoz UI.
Send traces via OTel Collector binary
Send traces via OTel Collector binary - No Code Automatic Instrumentation
Step 1: Setting up OpenTelemetry Collector binary as an agent in your machine
OpenTelemetry Collector binary helps to collect logs, hostmetrics, resource and infra attributes. It is recommended to install OTel Collector binary to collect and send traces to SigNoz cloud. You can correlate signals and have rich contextual data through this way.
Go to setup OTel Collector binary to install Otel Collector as agent that will collect telemetry data from your sample dotnet app and send it to SigNoz cloud.
Step 2: Download installation scripts
$module_url = "https://github.com/open-telemetry/opentelemetry-dotnet-instrumentation/releases/latest/download/OpenTelemetry.DotNet.Auto.psm1"
$download_path = Join-Path $env:temp "OpenTelemetry.DotNet.Auto.psm1"
Invoke-WebRequest -Uri $module_url -OutFile $download_path -UseBasicParsing
Step 3: Execute following script to download automatic instrumentation for your development environment
Import-Module $download_path
Install-OpenTelemetryCore
Step 4: Set variables and execute auto instrumentation
$env:OTEL_TRACES_EXPORTER="otlp"
$env:OTEL_EXPORTER_OTLP_ENDPOINT="http://localhost:4318/v1/traces"
$env:OTEL_DOTNET_AUTO_TRACES_CONSOLE_EXPORTER_ENABLED="true"
$env:OTEL_DOTNET_AUTO_LOGS_CONSOLE_EXPORTER_ENABLED="true"
Register-OpenTelemetryForCurrentSession -OTelServiceName "<service_name>"
dotnet run
<service_name>is name of your service
replace dotnet run with the run command of your application
Send traces via OTel Collector binary - Code Level Automatic Instrumentation
Step 1: Setting up OpenTelemetry Collector binary as an agent in your machine
OpenTelemetry Collector binary helps to collect logs, hostmetrics, resource and infra attributes. It is recommended to install OTel Collector binary to collect and send traces to SigNoz cloud. You can correlate signals and have rich contextual data through this way.
Go to setup OTel Collector binary to install Otel Collector as agent that will collect telemetry data from your sample dotnet app and send it to SigNoz cloud.
Step 2: Installing the OpenTelemetry dependency packages:
Install the following dependencies in your application.
dotnet add package OpenTelemetry
dotnet add package OpenTelemetry.Exporter.OpenTelemetryProtocol
dotnet add package OpenTelemetry.Extensions.Hosting
dotnet add package OpenTelemetry.Instrumentation.Runtime
dotnet add package OpenTelemetry.Instrumentation.AspNetCore
dotnet add package OpenTelemetry.AutoInstrumentation
Step 2: Adding OpenTelemetry as a service and configuring exporter options in Program.cs:
In your Program.cs file, add OpenTelemetry as a service. Here, we are configuring these variables:
serviceName- It is the name of your service.otlpOptions.Endpoint- It is the endpoint for your OTel Collector binary agent.
Here’s a sample Program.cs file with the configured variables.
using System.Diagnostics;
using OpenTelemetry.Exporter;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
var builder = WebApplication.CreateBuilder(args);
// Configure OpenTelemetry with tracing and auto-start.
builder.Services.AddOpenTelemetry()
.ConfigureResource(resource =>
resource.AddService(serviceName: "sample-net-app"))
.WithTracing(tracing => tracing
.AddAspNetCoreInstrumentation()
.AddOtlpExporter(otlpOptions =>
{
otlpOptions.Endpoint = new Uri("http://localhost:4317");
otlpOptions.Protocol = OtlpExportProtocol.Grpc;
}));
var app = builder.Build();
//The index route ("/") is set up to write out the OpenTelemetry trace information on the response:
app.MapGet("/", () => $"Hello World! OpenTelemetry Trace: {Activity.Current?.Id}");
app.Run();
The program uses the OpenTelemetry.Instrumentation.AspNetCore package to automatically create traces for incoming ASP.NET Core requests.
The OpenTelemetry.Exporter.Options get or set the target to which the exporter is going to send traces. Here, we’re configuring it to send traces to the OTel collector agent. The target must be a valid Uri with the scheme (http or https) and host and may contain a port and a path.
This is done by configuring an OpenTelemetry TracerProvider using extension methods and setting it to auto-start when the host is started.
Step 4. Running the .NET application:
dotnet build
dotnet run
Step 5: Generating some load data and checking your application in SigNoz UI
After the Otel collector is all set and running, and your too application is running, generate some traffic by interacting with it.
In the SigNoz account, open the Services tab. Hit the Refresh button on the top right corner, and your application should appear in the list of Applications. Ensure that you're checking data for the time range filter applied in the top right corner. You might have to wait for a few seconds before the data appears on SigNoz UI.
There are two ways to send data to SigNoz Cloud. You can containerize the images in both the cases.
Send traces directly to SigNoz Cloud
Step 1. Configure OpenTelemetry to run in Docker Container
Add the following in your Dockerfile.
...
# Download the installer script
RUN apt-get update && apt-get install -y curl unzip \
&& curl -sSfL https://github.com/open-telemetry/opentelemetry-dotnet-instrumentation/releases/latest/download/otel-dotnet-auto-install.sh -o otel-dotnet-auto-install.sh \
&& sh ./otel-dotnet-auto-install.sh \
&& chmod +x $HOME/.otel-dotnet-auto/instrument.sh
...
# Set OpenTelemetry environment variables
ENV OTEL_SERVICE_NAME="<service_name>"
ENV OTEL_TRACES_EXPORTER="otlp"
ENV OTEL_EXPORTER_OTLP_PROTOCOL="http/protobuf"
ENV OTEL_EXPORTER_OTLP_ENDPOINT="https://ingest.<region>.signoz.cloud:443"
ENV OTEL_EXPORTER_OTLP_HEADERS="signoz-ingestion-key=<your-ingestion-key>"
ENV OTEL_RESOURCE_ATTRIBUTES="deployment.environment=staging,service.version=1.0.0"
...
CMD . $HOME/.otel-dotnet-auto/instrument.sh && <run_command>
- Set the
<region>to match your SigNoz Cloud region - Replace
<your-ingestion-key>with your SigNoz ingestion key <service_name>is name of your service
The above steps install OpenTelemetry dependencies directly inside the Docker container without altering composer.json of project & sets environment variables to export the traces.
Step 2. Run your Docker container
Here's how you can run your docker container:
docker build -t <image-name> . && docker run -d -p <host-port>:<container-port> <image-name>
Replace
<image-name>,<host-port>, and<container-port>with values for your application.-druns the container in detached mode-pmaps a host port to a container port
Step 3. Validate if your application is sending traces to SigNoz cloud by following the instructions here.
In case you encounter an issue where all applications do not get listed in the services section then please refer to the troubleshooting section.
Send traces via OTel Collector binary
Step 1. Configure OpenTelemetry to run in Docker Container
Add the following in your Dockerfile.
# -------- Stage 1: Get OpenTelemetry Collector --------
FROM otel/opentelemetry-collector-contrib:0.131.0 AS otel
...
# Download the installer script
RUN apt-get update && apt-get install -y curl unzip \
&& curl -sSfL https://github.com/open-telemetry/opentelemetry-dotnet-instrumentation/releases/latest/download/otel-dotnet-auto-install.sh -o otel-dotnet-auto-install.sh \
&& sh ./otel-dotnet-auto-install.sh \
&& chmod +x $HOME/.otel-dotnet-auto/instrument.sh
...
# Copy OpenTelemetry Collector binary + config from first stage
COPY --from=otel /otelcol-contrib /otelcol-contrib
COPY --from=otel /etc/otelcol-contrib /etc/otelcol-contrib
COPY config.yaml /etc/otelcol-contrib/config.yaml
# Expose ports (App + Collector)
EXPOSE <APP_PORT> 4317 4318
...
# Environment variables (point .NET to local Collector)
ENV OTEL_SERVICE_NAME="<service_name>"
ENV OTEL_TRACES_EXPORTER="otlp"
ENV OTEL_EXPORTER_OTLP_PROTOCOL="http/protobuf"
ENV OTEL_EXPORTER_OTLP_ENDPOINT="http://localhost:4318"
ENV OTEL_PROPAGATORS="baggage,tracecontext"
# Run both Collector and PHP app in the same container
CMD sh -c "/otelcol-contrib --config=/etc/otelcol-contrib/config.yaml & . $HOME/.otel-dotnet-auto/instrument.sh && <run_command>"
Make sure you have config.yaml in root of the application. This config.yaml should be copied from third step of this
- Replace
<run_command>with the command to run your application <service_name>is name of your service
Step 2. Run your Docker container
Run your docker container to start exporting.
docker build -t <image-name> . && docker run -d -p <host-port>:<container-port> <image-name>
Replace
<image-name>,<host-port>, and<container-port>with values for your application.-druns the container in detached mode-pmaps a host port to a container port
Step 3. You can validate if your application is sending traces to SigNoz cloud by following the instructions here.
In case you encounter an issue where all applications do not get listed in the services section then please refer to the troubleshooting section.
Troubleshooting
The console exporter prints data to the Console window. You can use it to verify if the instrumentation is properly set up or not.
Below are the steps on how to use the console exporter:
Step 1. Adding the OpenTelemetry console exporter package:
dotnet add package OpenTelemetry.Exporter.Console
Step 2. Adding the console exporter method:
using System.Diagnostics;
using OpenTelemetry.Exporter;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
var builder = WebApplication.CreateBuilder(args);
// Configure OpenTelemetry with tracing and auto-start.
builder.Services.AddOpenTelemetry()
.ConfigureResource(resource =>
resource.AddService(serviceName: "sample-net-app"))
.WithTracing(tracing => tracing
.AddAspNetCoreInstrumentation()
.AddOtlpExporter(otlpOptions =>
{
//SigNoz Cloud Endpoint
otlpOptions.Endpoint = new Uri("https://ingest.<region>.signoz.cloud:443");
otlpOptions.Protocol = OtlpExportProtocol.Grpc;
//SigNoz Cloud account Ingestion key
string headerKey = "signoz-ingestion-key";
string headerValue = "<your-ingestion-key>";
string formattedHeader = $"{headerKey}={headerValue}";
otlpOptions.Headers = formattedHeader;
})
.AddConsoleExporter());
var app = builder.Build();
//The index route ("/") is set up to write out the OpenTelemetry trace information on the response:
app.MapGet("/", () => $"Hello World! OpenTelemetry Trace: {Activity.Current?.Id}");
app.Run();
Monitor the application on the console. You will be able to see the trace output as below:
info: Microsoft.Hosting.Lifetime[14]
Now listening on: https://localhost:7062
info: Microsoft.Hosting.Lifetime[14]
Now listening on: http://localhost:5017
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
Content root path: C:\sample-app2
Activity.TraceId: e1c2b70e9f39c6cc15d5d94b75412b70
Activity.SpanId: 17da84c0833e0075
Activity.TraceFlags: Recorded
Activity.ActivitySourceName: Microsoft.AspNetCore
Activity.DisplayName: /
Activity.Kind: Server
Activity.StartTime: 2023-11-05T19:59:39.7875151Z
Activity.Duration: 00:00:00.2548901
Activity.Tags:
net.host.name: localhost
net.host.port: 7062
http.method: GET
http.scheme: https
http.target: /
http.url: https://localhost:7062/
http.flavor: 2.0
http.user_agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36
http.status_code: 200
Resource associated with Activity:
service.name: sample-app2
service.instance.id: 44a34277-d46e-4758-b4f0-91b5a9435a4c
telemetry.sdk.name: opentelemetry
telemetry.sdk.language: dotnet
telemetry.sdk.version: 1.6.0