~/codewithstu

Metrics in .NET has never been easier - OpenTelemetry

Transcript

Previously on this channel, we've seen how to set up OpenTelemetry tracing and OpenTelemetry logs. But did you know that we could also set up OpenTelemetry metrics? In this video, you'll learn how to set up an application to use OpenTelemetry metrics, how to edit and drop some metrics. And also, how to add some basic instrumentation. Some of what I'm going to show you today is based on prerelease packages. They're only prerelease because of the APIs state inside of OpenTelemetry. But the actual C# API should be stable enough. So if you do see a few differences from what you see today compared to the new NuGet packages that are currently live on NuGet.org, then that would explain it. So the first package that we're going to use today is OpenTelemetry. Exporter. Console. This is just going to help us view the metrics on our screen. Next, we're going to have OpenTelemetry. Extensions. Hosting. And lastly, we're going to add OpenTelemetry. Instrumentation. Runtime so that we can look at some additional metrics later on.

So if we switch across to our code now, the first thing that we're going to need to do is set up our resource the exact same way as we have done in previous videos. So we're going to use ResourceBuilder. CreateDefault and then add the service CodeWithStu. The next thing that we're going to do is set up something called a meter. Now, for the purposes of this video, you can think of a meter as a container for all of your metrics. So for the purposes of this video, we're going to keep our usage of meters pretty simple, but we're going to cover meters in a lot more detail in a different video and show you the different types. So the next thing that we're going to do is create a simple counter using Meter. CreateCounter. And then we're going to pass in the name of the metric that we want to record. The next thing that we're going to do is call AddOpenTelemetryMetrics on builder. Services. This is going to give us a lambda, which we can configure the options with.

So we need to use the options lambda that we've got here to set up a couple of different bits. The first one is we're going to call options. SetResourceBuilder. This creates a link between the resource that we created earlier and OpenTelemetry. Next, we add our console exporter by going options. AddConsoleExporter. And finally, we're going to call options. AddMeter passing in the meter that we had earlier as this creates a link between OpenTelemetry and the . NET meter. You'll be able to see later on in the video how to edit and drop certain metrics from this. Now, if we go down to the implementation of our web handler that we've got here, one thing that we can do is just call add on our counter that we created earlier and this counter will increment every time that we make a request to the page. Then when we run our application, what you'll see in the console after a few seconds is that our counter metrics will be output. So when you're building your APIs, what's the most important metric for you to be capturing?

Let me know in the comments below. So as I mentioned earlier, you can do some basic editing and dropping of metrics for a given meter inside of OpenTelemetry in the OpenTelemetry space. This is called views. When you add a view, you can provide in a lambda function that you can pass in your custom logic. For example, you might want to rename a metric, or you might want to drop the metric as being in. Now. And then when you run the application, you'll be able to see that certain metrics are dropped. So at the start of the video we installed a NuGet package called OpenTelemetry.Instrumentation.Runtime. This package allows you to have basic dot net metrics such as CPU, memory usage and things like that. In order to add these metrics to our application. We need to go back to the AddOpenTelemetryMetrics Lambda that we had earlier and add options. AddRuntimeMetrics. Once you run the application after a few seconds, then you metrics will start appearing on screen.

Now, you may not want all of the metrics that come out of this meter, so you can use the views that we just went over.

If you enjoyed this video, consider subscribing to the YouTube channel for more content like this.

// share_this