asp.net core監控—引入Prometheus(一)

Promethues

Prometheus是CNCF畢業的第二個項目,算是明星產品(可自行了解Prometheus的功能),asp.net core當然不能錯過與之配套使用。在.net中是通過prometheus.net【https://github.com/prometheus-net/prometheus-net 】引入的。
asp.net core監控—引入Prometheus(一)
上圖是用Prometheus作監控結構圖,Prometheus默認採用拉取的方式,從應用服務中把metrics數據拉取出來,以便提供給Grafana去作展示。下面通過一個例子來進行說明。1、下載Prometheus【https://prometheus.io/download/
asp.net core監控—引入Prometheus(一)
prometheus.yml是配置文件,因爲採有拉模式,需要在配置文件中配置應用服務的url http://localhost:5000
asp.net core監控—引入Prometheus(一)
可以雙擊prometheus.exe啓動了。





Grafana

下載Grafana【https://grafana.com/grafana/download?platform=windows
asp.net core監控—引入Prometheus(一)

asp.net core監控—引入Prometheus(一)

啓動grafana-server.exe
訪問:http://localhost:3000/用戶名密碼默認:username:admin,password:admin現在開始配置Grafana:

配置數據源

asp.net core監控—引入Prometheus(一)
asp.net core監控—引入Prometheus(一)
asp.net core監控—引入Prometheus(一)

配置監控面板

asp.net core監控—引入Prometheus(一)
asp.net core監控—引入Prometheus(一)
asp.net core監控—引入Prometheus(一)

application

創建一個asp.net core 5.0的api項目,引進nuget包prometheus-net.AspNetCore,同時在Starup.cs的configure中添加Prometheus的中間件:

         public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseSwagger();
                app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "PrometheusSimpal v1"));
            }
            app.UseRouting();
            //http請求的中間件            
            app.UseHttpMetrics();
            app.UseAuthorization();
            app.UseEndpoints(endpoints =>
            {
                //映射監控地址爲  /metrics                
                endpoints.MapMetrics();
                endpoints.MapControllers();
            });
        }

啓動服務:http://loclahost:5000

asp.net core監控—引入Prometheus(一)
本例是採用grafana模板10915來展示數據的,展示的信息只是請求和controller的跟蹤信息,可以通過固定中間件來完全收集,如果有業務方面的信息跟蹤展示,就需要開發人員根據自己的業務邏輯來展示對應的監控指標了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章