Migrate .Net OpenTelemetry APM

Overview

The OpenTelemetry .NET SDK supports both manual instrumentation via activities and automatic instrumentation via instrumentation libraries. This page covers installing the SDK packages, setting up instrumentation, and configuring the OTLP exporter to send traces to Kloudfuse.

Install

Run these commands to install the core .NET packages:

dotnet add package OpenTelemetry.Extensions.Hosting
dotnet add package OpenTelemetry.Instrumentation.AspNetCore
dotnet add package OpenTelemetry.Exporter.OpenTelemetryProtocol

Set Up Instrumentation

Register OpenTelemetry in your application’s Program.cs or Startup.cs. Define an ActivitySource to create spans:

using System.Diagnostics;
using OpenTelemetry.Trace;

var activitySource = new ActivitySource("my-service");

builder.Services.AddOpenTelemetry()
    .WithTracing(tracerProvider =>
        tracerProvider
            .AddSource("my-service")
            .AddAspNetCoreInstrumentation()
            .AddOtlpExporter());
csharp

Create spans in your application code using the same ActivitySource:

using var activity = activitySource.StartActivity("my-operation");
activity?.SetTag("key", "value");
// your work here
csharp

For full setup details, follow the OpenTelemetry documentation on Manual Instrumentation Setup.

Send to Kloudfuse

Configure the OTLP exporter by setting the following environment variables before starting your application:

export OTEL_EXPORTER_OTLP_ENDPOINT="https://<kloudfuse-hostname>/ingester/otlp"   (1)
export OTEL_EXPORTER_OTLP_HEADERS="Kf-Api-Key=<api-key>"                          (2)
export OTEL_SERVICE_NAME="my-service"
export OTEL_RESOURCE_ATTRIBUTES="deployment.environment=production"
1 Replace <kloudfuse-hostname> with your Kloudfuse cluster hostname.
2 Replace <api-key> with your Kloudfuse API key.

Instrumentation Libraries

OpenTelemetry .NET provides packages that automatically instrument standard .NET libraries and frameworks. Review the OpenTelemetry documentation on Using instrumentation libraries and the OpenTelemetry Registry for .Net.

Troubleshooting

No Traces Appear

Confirm that AddOtlpExporter() is called in your WithTracing builder and that OTEL_EXPORTER_OTLP_ENDPOINT is set before the application starts. If using appsettings.json for configuration, ensure the OTLP endpoint is not being overridden there.

Spans Not Showing From Custom ActivitySource

The ActivitySource name passed to AddSource() must exactly match the name used when constructing new ActivitySource(…​). A mismatch causes spans to be created but not sampled by the tracer provider.

Environment Variables Not Picked Up

On Windows, environment variables set with export in a terminal apply only to that session. Use setx for persistent variables or set them in the IIS application pool, service properties, or Docker environment configuration as appropriate.