You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/core/diagnostics/diagnostic-resource-monitoring.md
+58-1Lines changed: 58 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -1,11 +1,14 @@
1
1
---
2
2
title: Diagnostic resource monitoring
3
3
description: Learn how to use the diagnostic resource monitoring library in .NET.
4
-
ms.date: 11/29/2023
4
+
ms.date: 07/09/2025
5
5
---
6
6
7
7
# Resource monitoring
8
8
9
+
> [!IMPORTANT]
10
+
> The <xref:Microsoft.Extensions.Diagnostics.ResourceMonitoring.IResourceMonitor> interface described in this article is deprecated and might be removed in future versions of .NET. We recommend migrating to the metrics-based approach described in the [.NET extensions metrics](built-in-metrics-diagnostics.md#microsoftextensionsdiagnosticsresourcemonitoring) article.
11
+
9
12
Resource monitoring involves the continuous measurement of resource utilization over a specified period. The [Microsoft.Extensions.Diagnostics.ResourceMonitoring](https://www.nuget.org/packages/Microsoft.Extensions.Diagnostics.ResourceMonitoring) NuGet package offers a collection of APIs tailored for monitoring the resource utilization of your .NET applications.
10
13
11
14
The <xref:Microsoft.Extensions.Diagnostics.ResourceMonitoring.IResourceMonitor> interface furnishes methods for retrieving real-time information concerning process resource utilization. This interface supports the retrieval of data related to CPU and memory usage and is currently compatible with both Windows and Linux platforms. All resource monitoring diagnostic information is published to OpenTelemetry by default, so there's no need to manually publish this yourself.
@@ -57,6 +60,59 @@ The following is an example of the output from the preceding code:
57
60
58
61
For the source code of this example, see the [Resource monitoring sample](https://github.com/dotnet/docs/tree/main/docs/core/diagnostics/snippets/resource-monitoring).
59
62
63
+
## Migrate to metrics-based resource monitoring
64
+
65
+
Since the <xref:Microsoft.Extensions.Diagnostics.ResourceMonitoring.IResourceMonitor> interface is deprecated, we recommend migrating to the metrics-based approach. The `Microsoft.Extensions.Diagnostics.ResourceMonitoring` package now provides several [metrics](built-in-metrics-diagnostics.md#microsoftextensionsdiagnosticsresourcemonitoring) that you can use instead, for instance:
66
+
67
+
-`container.cpu.limit.utilization`: The CPU consumption of the running containerized application relative to resource limit in range `[0, 1]`. Available for containerized apps on Linux and Windows.
68
+
-`container.cpu.request.utilization`: The CPU consumption of the running containerized application relative to resource request in range `[0, 1]`. Available for containerized apps on Linux.
69
+
-`container.memory.limit.utilization`: The memory consumption of the running containerized application relative to resource limit in range `[0, 1]`. Available for containerized apps on Linux and Windows.
70
+
71
+
### Example metrics-based resource monitoring
72
+
73
+
To use the metrics-based approach:
74
+
75
+
1. Add the `Microsoft.Extensions.Diagnostics.ResourceMonitoring` package to your project.
76
+
2. Add the resource monitoring services to your dependency injection container:
77
+
78
+
```csharp
79
+
services.AddResourceMonitoring();
80
+
```
81
+
82
+
3. Configure metrics collection using any OpenTelemetry-compatible metrics collector. For example:
builder.AddConsoleExporter(); // Or any other exporter you prefer
90
+
});
91
+
```
92
+
93
+
4. Now you can observe the resource usage metrics through your configured metrics exporter.
94
+
95
+
For more detailed information about the available metrics and how to collect them, see [.NET extensions metrics: Microsoft.Extensions.Diagnostics.ResourceMonitoring](built-in-metrics-diagnostics.md#microsoftextensionsdiagnosticsresourcemonitoring) and [Metrics collection](metrics-collection.md).
96
+
97
+
### 1:1 migration from `IResourceMonitor` to metrics
98
+
99
+
This chapter provides a 1:1 migration guide from the deprecated `IResourceMonitor` interface to the metrics-based approach. This is useful in case you need to manually listen to resource utilization metrics in your application. We provide this documentation for the sake of completeness, because, otherwise, you typically don't have to listen to metrics manually, as they are are automatically collected and exported to back-ends using the respective metrics exporters.
- Creates a cancellation token source and a cancellation token.
106
+
- Creates a new `Table` instance, configuring it with a title, caption, and columns.
107
+
- Performs a live render of the `Table` instance, passing in a delegate that will be invoked every three seconds.
108
+
- Gets the current resource utilization information using a callback set with the `SetMeasurementEventCallback` method and displays it as a new row in the `Table` instance.
109
+
110
+
The following is an example of the output from the preceding code:
For the source code of this example, see the [Resource monitoring with manual metrics sample](https://github.com/dotnet/docs/tree/main/docs/core/diagnostics/snippets/resource-monitoring-with-manual-metrics).
115
+
60
116
## Kubernetes probes
61
117
62
118
In addition to resource monitoring, apps that exist within a Kubernetes cluster report their health through diagnostic probes. The [Microsoft.Extensions.Diagnostics.Probes](https://www.nuget.org/packages/Microsoft.Extensions.Diagnostics.Probes) NuGet package provides support for Kubernetes probes. It externalizes various [health checks](diagnostic-health-checks.md) that align with various Kubernetes probes, for example:
@@ -72,3 +128,4 @@ To add support for Kubernetes probes, add a package reference to [Microsoft.Exte
0 commit comments