App.Metrics.Formatters.Prometheus
App.Metrics.AspNetCore
修改program.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using App.Metrics;
using App.Metrics.AspNetCore;
using App.Metrics.Formatters;
using App.Metrics.Formatters.Prometheus;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
namespace PrometheusAppMetricsDemo
{
public static class Program
{
public static IMetricsRoot Metrics { get; set; }
public static IWebHost BuildWebHost(string[] args)
{
Metrics = AppMetrics.CreateDefaultBuilder()
.OutputMetrics.AsPrometheusPlainText()
.OutputMetrics.AsPrometheusProtobuf()
.Build();
return WebHost.CreateDefaultBuilder(args)
.ConfigureMetrics(Metrics)
.UseMetrics(
options =>
{
options.EndpointOptions = endpointsOptions =>
{
endpointsOptions.MetricsTextEndpointOutputFormatter = Metrics.OutputMetricsFormatters
.GetType<MetricsPrometheusTextOutputFormatter>();
endpointsOptions.MetricsEndpointOutputFormatter = Metrics.OutputMetricsFormatters
.GetType<MetricsPrometheusProtobufOutputFormatter>();
};
})
.UseKestrel(options => options.Listen(IPAddress.Any, 5000))
.UseStartup<Startup>()
.Build();
}
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
}
}
配置prometheus的job
global:
scrape_interval:
15s
evaluation_interval: 15s
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
- job_name: 'node_exporter'
scrape_interval: 5s
static_configs:
- targets: ['localhost:9100']
- job_name: 'netcoreprometheus'
scrape_interval: 5s
scheme: https
tls_config:
insecure_skip_verify: true
metrics_path: /metrics-text
static_configs:
- targets: ['{demo_ip}:{demoport}']
为什么是metrics-text 参见appmetrics官方解释
/metrics-text will return metrics in Prometheus plain text format and /> metrics in Prometheus protobuf format.
重启prometheus
sudo systemctl restart prometheus
sudo systemctl status prometheus

配置Grafana仪表盘
直接可以用的仪表盘地址:
按之前配置的方式配置完成后,就可以看到grafana正常的从prometheus收集数据并展示了