prometheus-net.DotNetRuntime 获取 CLR 指标原理解析
prometheus-net.DotNetRuntime
介绍
Intro
前面集成 Prometheus 的文章中简单提到过,prometheus-net.DotNetRuntime
可以获取到一些 CLR 的数据,比如说 GC, ThreadPool, Contention, JIT 等指标,而这些指标可以很大程度上帮助我们解决很多问题,比如应用执行过程中是否经常发生 GC,GC 等待时间时间是否过长,是否有发生死锁或竞争锁时间过长,是否有发生线程池饿死等等一系列问题,有了这些指标我们就可以清晰的在运行时了解到这些信息。
来看一下官方介绍
A plugin for the prometheus-net package, exposing .NET core runtime metrics including:
- Garbage collection collection frequencies and timings by generation/ type, pause timings and GC CPU consumption ratio
- Heap size by generation
- Bytes allocated by small/ large object heap
- JIT compilations and JIT CPU consumption ratio
- Thread pool size, scheduling delays and reasons for growing/ shrinking
- Lock contention
- Exceptions thrown, broken down by type
These metrics are essential for understanding the peformance of any non-trivial application. Even if your application is well instrumented, you're only getting half the story- what the runtime is doing completes the picture.
支持的指标
Contention Events
只要运行时使用的 System.Threading.Monitor 锁或 Native锁出现争用情况,就会引发争用事件。
一个线程等待的锁被另一线程占有时将发生争用。
Name | Description | Type |
---|---|---|
dotnet_contention_seconds_total | 发生锁争用的耗时(秒)总计 | Counter |
dotnet_contention_total | 锁争用获得锁的数量总计 | Counter |
Thread Pool Events
Worker thread 线程池和 IO thread 线程池信息
Name | Description | Type |
---|---|---|
dotnet_threadpool_num_threads | 线程池中活跃的线程数量 | Gauge |
dotnet_threadpool_io_num_threads | IO 线程池中活跃线程数量(WindowsOnly) | Gauge |
dotnet_threadpool_adjustments_total | 线程池中线程调整总计 | Counter |
Garbage Collection Events
Captures information pertaining to garbage collection, to help in diagnostics and debugging.
Name | Description | Type |
---|---|---|
dotnet_gc_collection_seconds | 执行 GC 回收过程耗费的时间(秒) | Histogram |
dotnet_gc_pause_seconds | GC 回收造成的 Pause 耗费的时间(秒) | Histogram |
dotnet_gc_collection_reasons_total | 触发 GC 垃圾回收的原因统计 | Counter |
dotnet_gc_cpu_ratio | 运行垃圾收集所花费的进程CPU时间的百分比 | Gauge |
dotnet_gc_pause_ratio | 进程暂停进行垃圾收集所花费的时间百分比 | Gauge |
dotnet_gc_heap_size_bytes | 当前各个 GC 堆的大小 (发生垃圾回收之后才会更新) | Gauge |
dotnet_gc_allocated_bytes_total | 大小对象堆上已分配的字节总数(每100 KB分配更新) | Counter |
dotnet_gc_pinned_objects | pinned 对象的数量 | Gauge |
dotnet_gc_finalization_queue_length | 等待 finalize 的对象数 | Gauge |
JIT Events
Name | Description | Type |
---|---|---|
dotnet_jit_method_total | JIT编译器编译的方法总数 | Counter |
dotnet_jit_method_seconds_total | JIT编译器中花费的总时间(秒) | Counter |
dotnet_jit_cpu_ratio | JIT 花费的 CPU 时间 | Gauge |
集成方式
上面的列出来的指标是我觉得比较重要的指标,还有一些 ThreadPool Scheduling 的指标和 CLR Exception 的指标我觉得意义不是特别大,有需要的可以去源码里看一看
集成的方式有两种,一种是作者提供了一个默认的 Collector 会去收集所有支持的 CLR 指标信息,另外一种则是可以自己自定义的要收集的 CLR 指标类型,来看示例:
使用默认的 Collector 收集 CLR 指标
DotNetRuntimeStatsBuilder.Default().StartCollecting();
使用自定义的 Collector 收集 CLR 指标
DotNetRuntimeStatsBuilder.Customize() .WithContentionStats() // Contention event .WithGcStats() // GC 指标 .WithThreadPoolStats() // ThreadPool 指标 // .WithCustomCollector(null) // 你可以自己实现.........
0条评论