.NET
Integration
Section titled “Integration”Instrument your .NET applications to send traces, logs, and metrics to Kopai.
Installation
Section titled “Installation”dotnet add package OpenTelemetry.Extensions.Hostingdotnet add package OpenTelemetry.Instrumentation.AspNetCoredotnet add package OpenTelemetry.Exporter.OpenTelemetryProtocolBasic Setup
Section titled “Basic Setup”Add OpenTelemetry to your ASP.NET Core application:
using OpenTelemetry.Exporter;using OpenTelemetry.Logs;using OpenTelemetry.Metrics;using OpenTelemetry.Resources;using OpenTelemetry.Trace;
var builder = WebApplication.CreateBuilder(args);
var endpoint = Environment.GetEnvironmentVariable("OTEL_EXPORTER_OTLP_ENDPOINT") ?? "http://localhost:4318";var serviceName = Environment.GetEnvironmentVariable("OTEL_SERVICE_NAME") ?? "my-service";
// Setup Traces and Metricsbuilder.Services.AddOpenTelemetry() .ConfigureResource(r => r.AddService(serviceName)) .WithTracing(tracing => tracing .AddAspNetCoreInstrumentation() .AddOtlpExporter(opts => { opts.Endpoint = new Uri($"{endpoint}/v1/traces"); opts.Protocol = OtlpExportProtocol.HttpProtobuf; })) .WithMetrics(metrics => metrics .AddAspNetCoreInstrumentation() .AddOtlpExporter(opts => { opts.Endpoint = new Uri($"{endpoint}/v1/metrics"); opts.Protocol = OtlpExportProtocol.HttpProtobuf; }));
// Setup Logsbuilder.Logging.AddOpenTelemetry(logging =>{ logging.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService(serviceName)); logging.AddOtlpExporter(opts => { opts.Endpoint = new Uri($"{endpoint}/v1/logs"); opts.Protocol = OtlpExportProtocol.HttpProtobuf; });});Important: Use OtlpExportProtocol.HttpProtobuf and append the signal path (/v1/traces, /v1/logs, /v1/metrics) to the endpoint.
Running Your Application
Section titled “Running Your Application”Set the required environment variables and run:
export OTEL_EXPORTER_OTLP_ENDPOINT="https://otlp.kopai.app"export OTEL_SERVICE_NAME="my-service"dotnet run| Variable | Description |
|---|---|
OTEL_EXPORTER_OTLP_ENDPOINT | Kopai OTLP endpoint |
OTEL_SERVICE_NAME | Name shown in Kopai UI |
Sending Headers
Section titled “Sending Headers”For authentication, add headers to the exporter:
opts.Headers = "Authorization=Bearer YOUR_TOKEN";Working Example
Section titled “Working Example”For a complete working example: