首页 > 解决方案 > Prometheus 中未找到令牌错误

问题描述

我想用 Prometheus 监控我的 .Net Core 应用程序。为了做到这一点,我使用了一个 .Net Prometheus Library (AppMetrics)。当我转到链接http://localhost:57057/metrics时,它会返回一个带有数据的 JSON。

但是当我去普罗米修斯并将这个链接添加为目标时,它会抛出这个错误No Token Found

level=warn ts=2018-05-22T06:52:17.5781093Z caller=scrape.go:697 component="scrape manager" scrape_pool=actibook target= http://localhost:57057/metrics msg="append failed" err= “未找到令牌”

这是我可以处理的方法吗?

任何对此有问题的人都是解决方案。

启动.cs

//ConfigureServices
var metrics = new MetricsBuilder()
                            .OutputMetrics.AsPrometheusPlainText()
                            .OutputMetrics.AsPrometheusProtobuf().Build();
            services.AddMetrics(metrics);
            services.AddMetricsEndpoints(options =>
            {
                options.MetricsTextEndpointOutputFormatter = new MetricsPrometheusTextOutputFormatter();
                options.MetricsEndpointOutputFormatter = new MetricsPrometheusProtobufOutputFormatter();
            });
            services.AddMvc().AddMetrics();

//Configure
            app.UseMetricsAllMiddleware();
            app.UseMetricsAllEndpoints();

程序.cs

Metrics = AppMetrics.CreateDefaultBuilder()
                        .OutputMetrics.AsPrometheusPlainText()
                        .OutputMetrics.AsPrometheusProtobuf()
                        .Build();

            return WebHost.CreateDefaultBuilder(args)
                .ConfigureMetrics(Metrics)
                .UseMetrics()
                .UseStartup<Startup>()
                .Build();

应用设置.json

"MetricsOptions": {
    "DefaultContextLabel": "MyMvcApplication",
    "Enabled": true
  },
  "MetricsWebTrackingOptions": {
    "ApdexTrackingEnabled": true,
    "ApdexTSeconds": 0.1,
    "IgnoredHttpStatusCodes": [ 404 ],
    "IgnoredRoutesRegexPatterns": [],
    "OAuth2TrackingEnabled": true
  },
  "MetricEndpointsOptions": {
    "MetricsEndpointEnabled": true,
    "MetricsTextEndpointEnabled": true,
    "EnvironmentInfoEndpointEnabled": true
  }

您可以通过/metrics-text路径访问它。同样在你的prometheus.yml文件中应该添加这个:

- job_name: 'nameOfJob'
    metrics_path: '/metrics-text'
    static_configs:
      - targets: ['localhost:57057']

标签: asp.net-coremonitoringmonitorprometheus

解决方案


这通常意味着输出不是有效的 Prometheus 文本格式。查找公制或标签名称中的连字符,或以数字开头的任何一个 - 这些是最常见的错误。


推荐阅读