文章预览
.NET 9 中的 RuntimeMetrics Intro .NET 9 中引入了 RuntimeMetrics,基于 dotnet 里的 metrics 实现 System.Diagnostic.Metrics.Meter 来生成 metrics 数据,包含了 CPU、内存、GC、JIT 以及线程等信息 Sample 那我们就结合 OpenTelemetry 来看一个简单的示例,sample 引用了 OpenTelemetry.Exporter.Console 将 metrics 数据直接导出到 console using var _ = Sdk.CreateMeterProviderBuilder() .AddMeter( "System.Runtime" ) .AddConsoleExporter() .Build(); while ( true ) { await Task.Delay(TimeSpan.FromSeconds( 10 )); GC.Collect(); } 运行结果如下: Metric Name: dotnet.gc.collections, The number of garbage collections that have occurred since the process has started., Unit: {collection}, Meter: System.Runtime (2024-09-15T11:24:26.3765112Z, 2024-09-15T11:24:36.3639680Z] gc.heap.generation: gen2 LongSum Value: 0 (2024-09-15T11:24:26.3765112Z, 2024-09-15T11:24:36.3639680Z
………………………………