• 相关主页 

         prometheus 

         grafana 

  • 安装prometheus

        linux 可以参考https://www.cnblogs.com/linkanyway/p/configure-a-prometheus-monitoring-server-with-a-gr.html

        windows:

           下载地址:

            下载对应windows版本

             

              解压后在命令行运行 prometheus.exe

             

             然后访问 

             出现以下页面说明启动成功:

             

             如果要添加监视服务器可以配置:prometheus.yml   

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']
  • node-exporter 

          node-exporter是prometheus的服务提供者。

          windows版本为:

           

          下载最新的版本。启动后的默认端口是9182

  • 安装grafana

       下载地址:

       

         本人选择的是压缩包下载,解压后在命令行运行 grafana-server.exe

         

         访问

         出现以下页面说明启动成功

         

         初始用户名和密码都是 admin

         grafana配置prometheus数据源:

         

         选择prometheus数据源

         

          

        配置仪表盘

        在https://grafana.com/grafana/dashboards?orderby=name&direction=asc选择合适的仪表盘

        

           导入仪表板:

            

            选择import

            

            然后输入之前记录的id

            

             选择prometheus的数据源,之后打开dashboard就可以看到漂亮的仪表盘了

            

  •  aspnet core   配置

          参考 

         nuget引用:prometheus-net.aspnetcore

         

public class startup  
{  
    // others ...  
      
    public void configure(iapplicationbuilder app, ihostingenvironment env)  
    {  
        // others ...  
  
        app.usemetricserver();  
    }  
}  

      启动应用程序访问 /metrics    出现以下页面说明配置成功

      

 

       例如,我们希望能够统计每个api请求次数(200表示成功,500表示错误)。

       

public class requestmiddleware  
{  
    private readonly requestdelegate _next;  
    private readonly ilogger _logger;  
  
    public requestmiddleware(  
        requestdelegate next  
        , iloggerfactory loggerfactory  
        )  
    {  
        this._next = next;  
        this._logger = loggerfactory.createlogger<requestmiddleware>();  
    }  
      
    public async task invoke(httpcontext httpcontext)  
    {  
        var path = httpcontext.request.path.value;  
        var method = httpcontext.request.method;  
  
        var counter = metrics.createcounter("prometheus_demo_request_total", "http requests total", new counterconfiguration  
        {  
            labelnames = new[] { "path", "method", "status" }  
        });  
  
        var statuscode = 200;  
  
        try  
        {  
            await _next.invoke(httpcontext);  
        }  
        catch (exception)  
        {  
            statuscode = 500;  
            counter.labels(path, method, statuscode.tostring()).inc();  
  
            throw;  
        }  
          
        if (path != "/metrics")  
        {  
            statuscode = httpcontext.response.statuscode;  
            counter.labels(path, method, statuscode.tostring()).inc();  
        }  
    }  
}  
  
public static class requestmiddlewareextensions  
{          
    public static iapplicationbuilder userequestmiddleware(this iapplicationbuilder builder)  
    {  
        return builder.usemiddleware<requestmiddleware>();  
    }  
}  

    修改startup

public void configure(iapplicationbuilder app, ihostingenvironment env)  
{  
    // others ...  
  
    app.usemetricserver();  
  
    app.userequestmiddleware();  
}  

   修改api代码

[httpget]  
public actionresult<ienumerable<string>> get()  
{  
    if(new system.random().nextdouble() > 0.5)  
    {  
        throw new system.exception("test exception");  
    }  
  
    return new string[] { "value1", "value2" };  
}  

  

     然后在prometheus.yml添加配置

     

scrape_configs:  
- job_name: mydemo  
  scrape_interval: 15s  
  scrape_timeout: 10s  
  metrics_path: /metrics  
  scheme: http  
  static_configs:  
  - targets:  
    - localhost:44363   

 

  • grafana集成到自己的web项目

        web项目中我使用iframe直接嵌套进去的

       

  但是浏览器缓存清除了或者session失效了,每次进入web页面看grafana的时候就需要重新登录,在官方社区查找,并没有太好的执行办法,最后决定把grafana设置成匿名登录:

  修改conf/ custom.ini目录下的默认配置文件内容:
找到:# enable anonymous access

      

 

 然后重启grafana服务(systemctl restart grafana-server)就可以。

  • grafana分享集成