.NET Core中使用HttpReports進行接口統(tǒng)計,分析, 可視化, 監(jiān)控,追蹤等
HttpReports 基于.Net Core 開發(fā)的APM監(jiān)控系統(tǒng),使用MIT開源協(xié)議,主要功能包括,統(tǒng)計, 分析, 可視化, 監(jiān)控,追蹤等,適合在微服務環(huán)境中使用。
官方地址:https://www.yuque.com/httpreports/docs/uyaiil
主要功能
接口調(diào)用指標分析
多服務節(jié)點數(shù)據(jù)聚合分析
慢請求,錯誤請求分析
接口調(diào)用日志查詢
多類型預警監(jiān)控
HTTP,Grpc 調(diào)用分析
分布式追蹤
多數(shù)據(jù)庫支持,集成方便
程序性能監(jiān)控
第一步打開VS新建.net項目我這里用的是.net core webapi 進行演示
第二步 使用Nuget安裝MHttpReports.Dashboard包和HttpReports.SqlServer

第三步配置appsetting.json
{"HttpReportsDashboard": {"ExpireDay": 3,"Storage": {"ConnectionString": "Server=10.1.30.252;Database=GEISDB;user id=sa;password=Mg2021;","DeferSecond": 10,"DeferThreshold": 100},"Check": {"Mode": "Self","Switch": true,"Endpoint": "","Range": "500,2000"},"Mail": {"Server": "smtp.163.com","Port": 465,"Account": "[email protected]","Password": "*******","EnableSsL": true,"Switch": true}}}
參數(shù)介紹:
ExpireDay - 數(shù)據(jù)過期天數(shù),默認3天,HttpReports 會自動清除過期的數(shù)據(jù)
Storage - 存儲信息
DeferSecond - 批量數(shù)據(jù)入庫的秒數(shù),建議值 5-60
DeferThreshold - 批量數(shù)據(jù)入庫的數(shù)量,建議值100-1000
Mail - 郵箱信息,配置監(jiān)控的話,可以發(fā)告警郵件
Check - 健康檢查配置,具體看 健康檢查 頁面
第四步配置Startup
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940public void ConfigureServices(IServiceCollection services){services.AddHttpReportsDashboard().AddSQLServerStorage();}// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.public void Configure(IApplicationBuilder app, IWebHostEnvironment env){app.UseHttpReportsDashboard();}
把Dashboard 程序啟動起來,如果沒有問題的話,會跳轉(zhuǎn)到Dashboard的登陸頁面

默認賬號:
admin 密碼: 123456
現(xiàn)在Dashboard 可視化有了,但是沒有數(shù)據(jù),我們還需要 給服務端程序,添加 HttpReports 來收集信息。
第五步 我新建一個WebAPI 項目 UserService ,來充當用戶服務,然后安裝 HttpReports,HttpReports.Transport.Http

第六步修改Services的Appsettings.json 簡單配置一下
{"HttpReports": {"Transport": {"CollectorAddress": "http://localhost:5000/","DeferSecond": 10,"DeferThreshold": 100},"Server": "http://localhost:7000","Service": "User","Switch": true,"RequestFilter": [ "/api/health/*", "/HttpReports*" ],"WithRequest": true,"WithResponse": true,"WithCookie": true,"WithHeader": true}}
參數(shù)介紹:
Transport -
CollectorAddress - 數(shù)據(jù)發(fā)送的地址,配置Dashboard 的項目地址即可
DeferSecond - 批量數(shù)據(jù)入庫的秒數(shù),建議值 5-60
DeferThreshold - 批量數(shù)據(jù)入庫的數(shù)量,建議值100-300
Server - 服務的地址,
Service - 服務的名稱
Switch - 是否開啟收集數(shù)據(jù)
RequestFilter - 數(shù)據(jù)過濾,用 * 來模糊匹配
WithRequest - 是否記錄接口的入?yún)?/p>
WithResponse - 是否記錄接口的出參
WithCookie - 是否記錄Cookie 信息
WithHeader - 是否記錄請求Header信息
最后一步我們接著修改 UserService 項目的 Startup.cs 文件
app.UseHttpReports(); 這一行最好放到 Configure 方法 最上面
public void ConfigureServices(IServiceCollection services){services.AddHttpReports().AddHttpTransport();services.AddControllers();}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.public void Configure(IApplicationBuilder app, IWebHostEnvironment env){app.UseHttpReports();if (env.IsDevelopment()){app.UseDeveloperExceptionPage();}app.UseRouting();app.UseAuthorization();app.UseEndpoints(endpoints =>{endpoints.MapControllers();});}
刷新下 UserService 的接口,再回到Dashboard的頁面上面,已經(jīng)可以看到數(shù)據(jù)了

總結
本篇博客描述了使用HttpReports進行接口統(tǒng)計,分析, 可視化, 監(jiān)控,追蹤等
