开源APM系统 HttpReports 在 .Net Core的应用

前言

简单说明下,APM全称Application Performance Management应用性能管理,经过各类收集请求数据,同时搭配Dashboard以实现对应用程序性能管理和故障管理的系统化解决方案。css

HttpReports 介绍

HttpReports 是针对.Net Core 开发的轻量级APM系统,基于MIT开源协议, 使用HttpReports能够快速搭建.Net Core环境下统计,分析,图表,监控一体化的站点,而且支持多种数据库存储,适应.Net Core WebAPI,MVC,Web项目, 经过引用Nuget构建Dashboard面板,很是适合中小项目使用。git

Github地址:https://github.com/SpringLeee/HttpReports 感兴趣的同窗欢迎 Github Star 一波...github

在线预览: https://moa.hengyinfs.comweb

帐号: admin 密码 123456数据库

主要功能

  • 接口调用指标分析
  • 多服务节点数据聚合分析
  • 慢请求,错误请求分析
  • 接口调用日志查询
  • 趋势数据分析 (维度:小时,天,月)
  • 多类型预警监控
  • HTTP调用分析
  • 多数据库支持,集成方便

数据库支持

数据库 Nuget包名称
SqlServer HttpReports.SqlServer
MySql HttpReports.MySQL
Oracle HttpReports.Oracle
PostgreSQL HttpReports.PostgreSQL
ElasticSearch HttpReports.ElasticSearch

Dashboard-UI


快速开始 😆

Step1: 初始化数据库

HttpReports 须要手动建立数据库, 我这里使用 SqlServer 数据库为例,建立数据库 HttpReports, 固然数据库名称能够自由定义.json

Step2: 集成到WebAPI应用

打开VS开发工具,新建一个 WebAPI 应用,这里 .Net Core 版本只要是2.0 以上便可,我这里用的是3.1版本,建立完成后,Nuget 包引用 HttpReportsapi

引用成功后,由于我使用的是SqlServer 数据库,咱们再Nuget引用 HttpReports.SqlServer包缓存

找到程序的 appsetting.json,修改成如下配置, 注意:这里Storage 配置的数据库名称要和新建的数据库名称一致,微信

{ 
  "HttpReports": {
    "Storage": {
      "ConnectionString": "Max Pool Size = 512;server=.;uid=sa;pwd=123456;database=HttpReports;"
    },
    "Node": "TestWebAPI" 
  }  
}

而后咱们再修改 StartUp.cs 文件,修改成如下代码app

public void ConfigureServices(IServiceCollection services)
{
    //添加HttpReports
    services.AddHttpReports().UseSQLServerStorage();

    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)
{
    //使用HttpReports
    app.UseHttpReports();

    if (env.IsDevelopment())
    {
    app.UseDeveloperExceptionPage();
    }

    app.UseRouting();

    app.UseAuthorization();

    app.UseEndpoints(endpoints =>
    {
    endpoints.MapControllers();
    });
}

一切准备就绪后,咱们启动 WebAPi,而且刷新几回页面,到这里为止,WebAPI的部分咱们已经完成了 😛

Step3: 集成可视化 Dashboard

新建一个 .Net Core MVC 应用,新建完成后,经过Nuget包咱们分别安装 HttpReports.Dashboard ,HttpReports.SqlServer

引用完成后,修改MVC 项目的 appsetting.json 文件, 注意数据库要一致

{
  "HttpReports": {
    "Storage": { 
      "ConnectionString": "Max Pool Size = 512;server=.;uid=sa;pwd=123456;database=HttpReports;" 
    } 
  }
}

修改完成后,咱们接着修改 MVC 项目的 Startup.cs 文件

public void ConfigureServices(IServiceCollection services)
 {
    // 添加Daashboard
      services.AddHttpReportsDashborad().UseSQLServerStorage();

      services.AddControllersWithViews();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    // 使用Dashboard
    app.UseHttpReportsDashboard();

    if (env.IsDevelopment())
    {
    app.UseDeveloperExceptionPage();
    }
    else
    {
    app.UseExceptionHandler("/Home/Error");
    }
    app.UseStaticFiles();

    app.UseRouting();

    app.UseAuthorization();

    app.UseEndpoints(endpoints =>
    {
    endpoints.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");
    });
}

一切准备就绪后,咱们启动MVC 项目,若是没有问题的话,会跳转到Dashboard的登录页面,默认帐号:admin 密码: 123456

例子中我用的是SqlServer 数据库,其余的数据库也是相似的,我只建立了一个WebAPI,固然HttpRrports 也支持多个WebAPI,咱们只要修改appsetting.json 的 Node,你能够设置一个 Node 为 Auth,一个 Node 为 Log 等等等,到这里一个最简单集成 HttpReports 的例子已经完成了, 请尽情使用吧 😆

ElasticSearch 存储配置

ElasticSearch 会和其余数据库有些不一样,若是采用的是ES存储的话,修改程序 appsetting.json,采用下边的配置便可

{
  "HttpReports": {
    "Storage": {
      "ConnectionString": "http://localhost:9200/",
      "UserName": "admin",
      "Password": "admin"
      
    } 
  }
}

数据异步批量延迟入库

HttpReports 默认是同步入库,有的用户考虑到性能可能要用到 异步批量入库,只须要修改 WebAPI 项目

{
  "HttpReports": {
    "Storage": { 
      "ConnectionString": "Max Pool Size = 512;server=.;uid=sa;pwd=123456;database=HttpReports;", 
      "EnableDefer": true, //是否启用延时写入
      "DeferTime": "00:00:30", //延时写入的时间,超过此时间将进行写入
      "DeferThreshold": 5 //延时写入的阈值,缓存超过此数目时进行写入
    },
    "Node": "Pay" 
  }
}

css.js等资源文件过滤

HttpReports 再捕获Http请求时,会过滤掉资源文件,若是你不想这样作,添加或者修改appsetting.json, 设置
FilterStaticFiles = false 便可

Dashboard 使用根目录

HttpReports.Dashboard 默认使用 MVC 项目的根目录,好比: www.xxx.com, 若是你不想这样作,添加或者修改appsetting.json,设置 UseHome = false , 这样访问 dashobard 的地址为 www.xxx.com/Dashboard

Dashboard 预警监控

HttpReports.Dashboard 集成了预警监控功能,使用的话须要先配置 Smtp 邮箱,不然接收不到预警邮件哦

咱们修改Dashboard项目的appsetting.json为下面便可

{
  "HttpReports": {
    "Storage": { 
      "ConnectionString": "DataBase=HttpReports;Data Source=localhost;User Id=root;Password=123456" 
    }, 
    "Mail": {
      "Server": "smtp.qq.com",
      "Port": 465, //smtp端口
      "Account": "",
      "Password": "",
      "EnableSsL": true //是否使用ssl
    }
  }
}

监控功能主要针对如下四项监控

  • 响应超时
  • 请求错误
  • IP异常
  • 请求量监控

简单说明下,监控频率 选1小时,也就是1个小时 运行一次,而后填入预警的收件邮箱,多个邮箱用逗号隔开, aaa.qq.com,bbb.qq.com , 服务节点 能够选中单个和多个节点,默认的话,下边 4个监控都是关闭状态, 若是须要勾选启动便可,具体的话这里就很少说了.

最后,贴上两个完整的配置文件供你们参考

WebAPI端

{
  "HttpReports": {
    "Storage": {
      "ConnectionString": "Max Pool Size = 512;server=.;uid=sa;pwd=123456;database=HttpReports;",
      "EnableDefer": false, //是否启用延时写入
      "DeferTime": "00:00:30", //延时写入的时间,超过此时间将进行写入
      "DeferThreshold": 5 //延时写入的阈值,缓存超过此数目时进行写入
    },
    "Node": "Pay", // WebAPI 的服务名称
    "FilterStaticFiles": true // 是否过滤掉css,js 等资源文件 
  }
}

Dashboard端

{
  "HttpReports": {
    "Storage": { 
      "ConnectionString": "Max Pool Size = 512;server=.;uid=sa;pwd=123456;database=HttpReports;",
      
    },
    "UseHome": true, // 默认使用根目录导航
    "Mail": {
      "Server": "smtp.qq.com",
      "Port": 465, //smtp端口
      "Account": "",
      "Password": "",
      "EnableSsL": true //是否使用ssl
    }
  }
}

总结

HttpReports 是 .Net Core环境下开源的APM系统,很是适合微服务环境中使用,若是是中小型项目的话,那么使用 HttpReports 是一个不错的选择,若是能帮助到您的话,还请但愿给个Star 支持下, 感谢 😆

Github: https://github.com/SpringLeee/HttpReports

MIT协议

交流反馈

若是您在项目中使用了HttpReports,或者感兴趣的能够加入QQ群,你们一块儿沟通,有更新也会第一时间通知,也能够添加个人微信,但愿能够帮助到您

相关文章
相关标签/搜索