APM
APM Instrumentation
.NET
9min
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 net metrics name description process runtime dotnet gc collections count number of garbage collections that have occurred since process start process runtime dotnet gc objects size count of bytes currently in use by objects in the gc heap that haven't been collected yet fragmentation and other gc committed memory pools are excluded process runtime dotnet gc allocations size count of bytes allocated on the managed gc heap since the process start net objects are allocated from this heap object allocations from unmanaged languages such as c/c++ do not use this heap process runtime dotnet gc committed memory size the amount of committed virtual memory for the managed gc heap, as observed during the latest garbage collection committed virtual memory may be larger than the heap size because it includes both memory for storing existing objects (the heap size) and some extra memory that is ready to handle newly allocated objects in the future the value will be unavailable until at least one garbage collection has occurred process runtime dotnet gc heap size the heap size (including fragmentation), as observed during the latest garbage collection the value will be unavailable until at least one garbage collection has occurred process runtime dotnet gc heap fragmentation size the heap fragmentation was observed during the latest garbage collection the value will be unavailable until at least one garbage collection has occurred process runtime dotnet gc duration the total amount of time paused in gc since the process start process runtime dotnet jit il compiled size count of bytes of intermediate language that have been compiled since the process start process runtime dotnet jit methods compiled count the number of times the jit compiler compiled a method since the process start the jit compiler may be invoked multiple times for the same method to compile with different generic parameters, or because tiered compilation requested different optimization settings process runtime dotnet jit compilation time the amount of time the jit compiler has spent compiling methods since the process start process runtime dotnet monitor lock contention count the number of times there was contention when trying to acquire a monitor lock since the process start monitor locks are commonly acquired by using the lock keyword in c# or by calling monitor enter() and monitor tryenter() process runtime dotnet thread pool threads count the number of thread pool threads that currently exist process runtime dotnet thread pool completed items count the number of work items that have been processed by the thread pool since the process start process runtime dotnet thread pool queue length the number of work items that are currently queued to be processed by the thread pool process runtime dotnet timer count the number of timer instances that are currently active timers can be created by many sources, such as system threading timer, task delay, or the timeout in a cancellationsource an active timer is registered to tick at some point in the future and has not yet been canceled process runtime dotnet assemblies count the number of net assemblies that are currently loaded process runtime dotnet exceptions count count of exceptions that have been thrown in managed code, since the observation started the value will be unavailable until an exception has been thrown after opentelemetry instrumentation runtime initialization source url for the example application https //opentelemetry io/docs/instrumentation/net/getting started/ https //opentelemetry io/docs/instrumentation/net/getting started/