安装prometheus和Grafana
参考之前的文章->安装prometheus和Grafana教程
 
 
源代码
dotnet源代码
 
新建.net core7 web项目

 修改Program.cs
using Prometheus;
namespace PrometheusStu01;
public class Program
{
    public static void Main(string[] args)
    {
        var builder = WebApplication.CreateBuilder(args);
        builder.Services.AddControllers();
        
        var app = builder.Build();
        
        app.UseRouting();
        //http请求的中间件,收集http请求信息的
        app.UseHttpMetrics();
        //映射监控地址为  /metrics
        app.MapMetrics();
        app.MapControllers();
        app.Run();
    }
}
安装prometheus-net.AspNetCore 8.2.1
 访问http://localhost:5122/metrics
 
 修改prometheus.yml
scrape_configs:
  # 省略其他配置
  - job_name: "asp.net core web"
    static_configs:
      - targets: ["127.0.0.1:5122"]

 重启
 

 
 
 
 
 修改Program.cs
using Prometheus;
namespace PrometheusStu01;
public class Program
{
    public static void Main(string[] args)
    {
        var builder = WebApplication.CreateBuilder(args);
        builder.Services.AddControllers();
        
        var app = builder.Build();
        
        app.UseRouting();
        //http请求的中间件,收集http请求信息的
        app.UseMetricServer();
        //看这里>^.^<
        app.UseMetricServer();
        //映射监控地址为  /metrics
        app.MapMetrics();
        app.MapControllers();
        app.Run();
    }
}
内存占用情况
 
参考
https://www.cnblogs.com/savorboard/p/diagnostics.html
 https://www.cnblogs.com/sheng-jie/p/how-much-you-know-about-diagnostic-in-dotnet.html
 https://learn.microsoft.com/zh-cn/dotnet/core/diagnostics/metrics-collection



















