OpenTelemetry Support
Using OpenTelemetry SDKs
OTel for .NET
8min
in this guide, we will walk you through the process of setting up and using opentelemetry in net you will learn how to instrument a simple net application to produce traces, metrics, and logs and export them to kloudmate step 1 prerequisites before diving into opentelemetry, be sure that you have the following installed net sdk 6+ step 2 example application if you already have a net application and don't need to create one from scratch, you can directly jump to the instrumentation part of this guide for this tutorial, we will be using a basic minimal api with asp net core application however, opentelemetry net is compatible with other web frameworks as well feel free to adapt the instructions to your preferred framework step 3 installation set up an environment in a new directory called dotnet simple run the following command within this directory dotnet new web 2\ within the same directory, replace the content of program cs with the following code using system globalization; using microsoft aspnetcore mvc; var builder = webapplication createbuilder(args); var app = builder build(); string handlerolldice(\[fromservices]ilogger\<program> logger, string? player) { var result = rolldice(); if (string isnullorempty(player)) { logger loginformation("anonymous player is rolling the dice {result}", result); } else { logger loginformation("{player} is rolling the dice {result}", player, result); } return result tostring(cultureinfo invariantculture); } int rolldice() { return random shared next(1, 7); } app mapget("/rolldice/{player?}", handlerolldice); app run(); 3\ in the properties subdirectory, replace the content of launchsettings json with the following { "$schema" "http //json schemastore org/launchsettings json", "profiles" { "http" { "commandname" "project", "dotnetrunmessages" true, "launchbrowser" true, "applicationurl" "http //localhost 8080", "environmentvariables" { "aspnetcore environment" "development" } } } } 4\ run the application using the following command then open http //localhost 8080/rolldice http //localhost 8080/rolldice in your web browser dotnet build dotnet run step 4 instrumentation to install the instrumentation packages, we will use the nuget packages from opentelemetry these packages will automatically generates telemetry data 1\ add the packages dotnet add package opentelemetry extensions hosting dotnet add package opentelemetry instrumentation aspnetcore dotnet add package opentelemetry exporter opentelemetryprotocol 2\ in program cs , replace the following lines var builder = webapplication createbuilder(args); var app = builder build(); with using opentelemetry exporter; using opentelemetry logs; using opentelemetry metrics; using opentelemetry resources; using opentelemetry trace; var builder = webapplication createbuilder(args); const string servicename = "roll dice"; builder logging addopentelemetry(options => { options setresourcebuilder( resourcebuilder createdefault() addservice(servicename)) addotlpexporter(otlpoptions => { otlpoptions endpoint = new uri("https //otel kloudmate com 4318/v1/logs"); otlpoptions protocol = otlpexportprotocol httpprotobuf; string headerkey = "authorization"; string headervalue = "\<km private key>"; string formattedheader = $ "{headerkey}={headervalue}"; otlpoptions headers = formattedheader; }); }); builder services addopentelemetry() configureresource(resource => resource addservice(servicename)) withtracing(tracing => tracing addaspnetcoreinstrumentation() addotlpexporter(otlpoptions => { otlpoptions endpoint = new uri("https //otel kloudmate com 4318/v1/traces"); otlpoptions protocol = otlpexportprotocol httpprotobuf; string headerkey = "authorization"; string headervalue = "\<km private key>"; string formattedheader = $ "{headerkey}={headervalue}"; otlpoptions headers = formattedheader; })) withmetrics(metrics => metrics addaspnetcoreinstrumentation() addotlpexporter(otlpoptions => { otlpoptions endpoint = new uri("https //otel kloudmate com 4318/v1/metrics"); otlpoptions protocol = otlpexportprotocol httpprotobuf; string headerkey = "authorization"; string headervalue = "\<km private key>"; string formattedheader = $ "{headerkey}={headervalue}"; otlpoptions headers = formattedheader; })); var app = builder build(); step 5 run the instrumented app 1\ run the instrumented application dotnet run note the output from the dotnet run 2\ from another terminal send a request using curl curl localhost 8080/rolldice source url for the example application https //opentelemetry io/docs/instrumentation/net/getting started/ https //opentelemetry io/docs/instrumentation/net/getting started/