【中間件】.net core中使用httpreports進行接口統計,分析, 可視化, 監控,追蹤等

【中間件】.net core中使用httpreports進行接口統計,分析, 可視化, 監控,追蹤等

httpreports 基於.net core 開發的apm監控系統,使用mit開源協議,主要功能包括,統計, 分析, 可視化, 監控,追蹤等,適合在微服務環境中使用。

最后更新 2022/7/7 下午7:58
黑哥聊dotNet
预计阅读 4 分钟
分类
.NET
标签
.NET C# 開源 架構設計

HttpReports 基于.NET Core 开发的APM监控系统,使用MIT开源协议,主要功能包括,统计, 分析, 可视化, 监控,追踪等,适合在微服务环境中使用。

官方地址:https://www.yuque.com/httpreports/docs/uyaiil

主要功能

  • 接口調用指標分析
  • 多服務節點數據聚合分析
  • 慢請求,錯誤請求分析
  • 接口調用日誌查詢
  • 多類型預警監控
  • http,grpc 調用分析
  • 分布式追蹤
  • 多資料庫支持,集成方便
  • 程式性能監控

第一步

打开 VS 新建.NET 项目我这里用的是.NET Core Web API 进行演示

第二步

使用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": "HttpReports@qq.com",
      "Password": "*******",
      "EnableSsL": true,
      "Switch": true
    }
  }
}

參數居間:

  • expireday - 數據過期天數,默認 3 天,httpreports 會自動清除過期的數據
  • storage - 存儲信息
  • defersecond - 批量數據入庫的秒數,建議值 5-60
  • deferthreshold - 批量數據入庫的數量,建議值 100-1000
  • mail - 郵箱信息,配置監控的話,可以發告警郵件
  • check - 健康檢查配置,具體看 健康檢查 頁面

第四步

配置 startup

// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public 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 程式啟動起來,如果沒有問題的話,會跳轉到 dashboard 的登錄頁面

默认账号:admin
密码: 123456

現在 dashboard 可視化有了,但是沒有數據,我們還需要 給服務端程式,添加 httpreports 來收集信息。

第五步

我新建一个 WebAPI 项目 UserService ,来充当用户服务,然后安装 HttpReportsHttpReports.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
  }
}

參數居間:

  • Transport -

  • collectoraddress - 數據發送的地址,配置 dashboard 的項目地址即可

  • defersecond - 批量數據入庫的秒數,建議值 5-60

  • deferthreshold - 批量數據入庫的數量,建議值 100-300

  • server - 服務的地址,

  • service - 服務的名稱

  • switch - 是否開啟收集數據

  • requestfilter - 數據過濾,用 * 來模糊匹配

  • withrequest - 是否記錄接口的入參

  • 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的页面上面,已经可以看到数据了

總結

本篇博客描述了使用 httpreports 進行接口統計,分析, 可視化, 監控,追蹤等, 如果覺得還不錯,請給個關注。

Keep Exploring

延伸阅读

更多文章
同分类 / 同标签 2026/2/7

aot使用經驗總結

從項目創建伊始,就應養成良好的習慣,即只要添加了新功能或使用了較新的語法,就及時進行 aot 發布測試。

继续阅读