You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用OpenTelemetry.Process包添加指标时Prometheus报错求助

解决OpenTelemetry Process仪表与Prometheus导出器的指标命名冲突问题

问题原因

process_cpu_time_seconds_total指标的单位被标记为seconds,但Prometheus的命名规范要求指标名称的后缀需与单位匹配(累计计数器通常带_total后缀,单位应隐含在名称逻辑中),两者不匹配导致报错。

可行解决方案

1. 升级OpenTelemetry.Instrumentation.Process包

直接将OpenTelemetry.Instrumentation.Process NuGet包升级到最新稳定版,新版本通常已修复这类命名规范不兼容的问题,无需修改代码。

2. 添加指标重命名处理器修正命名

若暂时无法升级包,可通过添加自定义处理器调整指标名称和单位,使其符合Prometheus规范:

.WithMetrics(x =>
{
    x.AddProcessInstrumentation();
    // 新增指标重命名处理器
    x.AddProcessor(new MetricProcessorWrapper<Metric>(new RenameMetricProcessor(metric =>
    {
        if (metric.Name == "process_cpu_time_seconds_total")
        {
            // 修改指标名称并调整单位,匹配Prometheus规范
            return metric.WithName("process_cpu_time_total").WithUnit("s");
        }
        return metric;
    })));

    x.AddHttpClientInstrumentation();
    x.AddAspNetCoreInstrumentation();
    x.AddPrometheusExporter()
        .AddMeter("MyApplication.Api")
        .AddMeter("System.Runtime")
        .AddMeter("System.Net.Http")
        .AddMeter("System.Data.SqlClient")
        .AddMeter("Microsoft.AspNetCore.Hosting")
        .AddMeter("Microsoft.AspNetCore.Server.Kestrel")
        .AddMeter("Microsoft.AspNetCore.Http.Connections")
        .AddMeter("Microsoft.AspNetCore.Routing")
        .AddMeter("Microsoft.AspNetCore.Diagnostics")
        .AddMeter("Microsoft.AspNetCore.RateLimiting");
});

3. 禁用Prometheus导出器的单位校验(不推荐)

通过配置关闭单位后缀校验可临时绕过错误,但会破坏Prometheus规范,可能引发其他兼容性问题,仅作为临时应急方案:

.WithMetrics(x =>
{
    x.AddProcessInstrumentation();
    x.AddHttpClientInstrumentation();
    x.AddAspNetCoreInstrumentation();
    x.AddPrometheusExporter(options =>
    {
        options.DisableUnitSuffixValidation = true;
    })
        .AddMeter("MyApplication.Api")
        .AddMeter("System.Runtime")
        .AddMeter("System.Net.Http")
        .AddMeter("System.Data.SqlClient")
        .AddMeter("Microsoft.AspNetCore.Hosting")
        .AddMeter("Microsoft.AspNetCore.Server.Kestrel")
        .AddMeter("Microsoft.AspNetCore.Http.Connections")
        .AddMeter("Microsoft.AspNetCore.Routing")
        .AddMeter("Microsoft.AspNetCore.Diagnostics")
        .AddMeter("Microsoft.AspNetCore.RateLimiting");
});

内容的提问来源于stack exchange,提问作者Mahdi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 08:48:28