ARTICLE DETAIL

资讯详情

深耕郑州网站建设与运营推广的一线实战洞察。

.NET运行时指标三件套:opentelemetry-dotnet-contrib的Runtime、Process与EventCounters监控完整清单

.NET运行时指标三件套:opentelemetry-dotnet-contrib的Runtime、Process与EventCounters监控完整清单 .NET运行时指标三件套opentelemetry-dotnet-contrib的Runtime、Process与EventCounters监控完整清单【免费下载链接】opentelemetry-dotnet-contribThis repository contains set of components extending functionality of the OpenTelemetry .NET SDK. Instrumentation libraries, exporters, and other components can find their home here.项目地址: https://gitcode.com/gh_mirrors/op/opentelemetry-dotnet-contribopentelemetry-dotnet-contrib 是 OpenTelemetry .NET SDK 的官方扩展组件仓库其中就包含一组开箱即用的.NET 运行时指标监控三件套Runtime、Process、EventCounters三套 Instrumentation。它们分别盯住GC 垃圾回收、JIT 编译、线程池等运行时内部行为CPU 使用率、内存占用、进程存活时间等操作系统级资源以及 .NET 内置EventCounters事件计数器到 OpenTelemetry 指标的自动转换。本文为你梳理这三件套的完整指标清单、启用方法与选型建议帮你快速搭建 .NET 应用的性能监控面板。 三件套速览各自负责什么组件监控对象典型场景稳定度Runtime运行时内部GC、JIT、线程池、异常排查 GC 频繁、内存碎片、锁竞争Stable稳定版Process操作系统层面CPU、内存、线程、句柄资源用量看板、容量规划Release CandidateEventCounters框架/第三方库发布的 EventCounters统一汇聚现有计数器生态Alpha一句话理解Process 看壳Runtime 看芯EventCounters 看生态。 Runtime InstrumentationGC、JIT 与线程池全掌握核心实现在src/OpenTelemetry.Instrumentation.Runtime/RuntimeMetrics.cs指标名统一带process.runtime.dotnet.前缀。GC垃圾回收相关指标指标名含义备注gc.collections.count进程启动以来各代 GC 次数带generation属性gen0/gen1/gen2gc.objects.sizeGC 堆中未回收对象占用字节数即时快照值gc.allocations.size进程启动以来托管堆累计分配字节数.NET 6gc.committed_memory.size托管堆已提交的虚拟内存.NET 6gc.heap.size/gc.heap.fragmentation.size堆大小与碎片按代.NET 6 / 7需至少发生一次 GCgc.duration进程启动以来 GC 累计暂停时间.NET 7 当看到gen2收集次数暴涨或gc.heap.fragmentation.size持续增大时通常意味着应用存在内存压力或对象生命周期管理问题。JIT 编译与线程相关指标指标名含义jit.methods_compiled.countJIT 累计编译方法次数分层编译可能多次编译同一方法jit.il_compiled.size累计编译的中间语言IL字节数jit.compilation_timeJIT 累计耗时thread_pool.threads.count当前线程池线程数thread_pool.queue.length队列中待处理工作项数量thread_pool.completed_items.count已处理工作项累计数monitor.lock_contention.count锁竞争次数C# 的lock关键字背后就是它timer.count当前活跃的 Timer 实例数assemblies.count当前已加载的 .NET 程序集数exceptions.count首次机会异常FirstChanceException计数一个重要的版本差异 ⚠️如果你面向.NET 9 及以上官方运行时已内置System.RuntimeMeter本组件会直接复用内置指标见MeterProviderBuilderExtensions.cs无需重复实现。面向 .NET 6~8 时则由组件自己采集上表指标。️ Process InstrumentationCPU 与内存的资源看板实现在src/OpenTelemetry.Instrumentation.Process/ProcessMetrics.cs指标命名遵循 OpenTelemetry 语义规范跨语言通用指标名含义单位process.memory.usage物理内存工作集占用字节process.memory.virtual已提交虚拟内存字节process.cpu.timeCPU 累计时间按cpu.mode属性区分user / system秒process.thread.count进程线程数个process.uptime进程运行时长秒process.windows.handle.countWindows 句柄数仅 Windows个process.unix.file_descriptor.countLinux 文件描述符数仅 Linux个它内部做了10ms 快照缓存多次导出不会重复触发Process.Refresh()性能开销很低。项目还附带了一个开箱即用的 Grafana 面板模板process-instrumentation-grafana-dashboard-sample.json位于 examples/process-instrumentation 目录下导入即可看到 CPU 利用率、内存等图表。 EventCounters Instrumentation打通 .NET 计数器生态这是三件套中的连接器把 .NET 生态里广泛使用的EventCountersKestrel、ADO.NET 等框架都在发自动转成 OpenTelemetry 指标实现见src/OpenTelemetry.Instrumentation.EventCounters/EventCountersMetrics.cs。工作机制与注意事项命名规则转换后的指标名为ec.EventSource名.计数器名若超过 63 字符会自动截断 EventSource 名。增量 → Counter均值 → GaugeEventCounterIncrement/Mean 二选一会被自动映射为 ObservableCounter 或 ObservableGauge。需要白名单通过options.AddEventSources(...)指定要监听的 EventSource避免全量订阅。System.Runtime被显式禁用监听它请改用前两件套组件会直接抛NotSupportedException提醒你。刷新间隔RefreshIntervalSecs最小 1 秒控制轮询频率指标要等首个间隔结束才出现。示例examples/event-counters 目录下的Program.cs创建EventCounter后写值配合AddEventCountersInstrumentation即可在控制台看到导出结果。 三步快速启用指南第 1 步安装 NuGet 包dotnet add package OpenTelemetry.Instrumentation.Runtime dotnet add package --prerelease OpenTelemetry.Instrumentation.Process dotnet add package --prerelease OpenTelemetry.Instrumentation.EventCounters第 2 步在 MeterProviderBuilder 上注册using var meterProvider Sdk.CreateMeterProviderBuilder() .AddRuntimeInstrumentation() // GC / JIT / 线程池 .AddProcessInstrumentation() // CPU / 内存 / 线程 .AddEventCountersInstrumentation(o { o.AddEventSources(Microsoft-AspNetCore-Server-Kestrel); o.RefreshIntervalSecs 1; }) .AddPrometheusHttpListener() // 接 Prometheus 导出 .Build();第 3 步接导出器出图三件套都面向Metrics可搭配 Prometheus HttpListener、OTLP 或控制台导出器接上 Prometheus Grafana 后就能得到完整的 .NET 运行时监控大盘。完整可运行 Demo 见仓库examples/runtime-instrumentation与examples/process-instrumentation目录。 选型与排查清单只想看资源用量CPU、内存、运行时长→ 只装Process。排查 GC 停顿、锁竞争、异常风暴→ 加Runtime注意 .NET 9 走内置System.Runtime指标。想统一 Kestrel、HttpClient 等框架自带计数器→ 加EventCounters并配好白名单。指标缺失怎么办确认 .NET 版本部分 Runtime 指标需 .NET 6/7GC 相关指标需至少发生一次回收EventCounters 需等待第一个刷新间隔。三件套各司其职、互不冲突组合起来就是 .NET 应用最完整的一套体检指标从操作系统资源到运行时内核再到框架计数器生态一层不漏。【免费下载链接】opentelemetry-dotnet-contribThis repository contains set of components extending functionality of the OpenTelemetry .NET SDK. Instrumentation libraries, exporters, and other components can find their home here.项目地址: https://gitcode.com/gh_mirrors/op/opentelemetry-dotnet-contrib创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表