简介

httpreports 基于.net core 开发的apm监控系统,使用mit开源协议,主要功能包括,统计, 分析, 可视化, 监控,追踪等,适合在中小项目中使用。

github:https://github.com/dotnetcore/httpreports

我也很荣幸在.net conf 2020 大会上, 做了简单的分享,开源不易,感兴趣的同学欢迎star,支持一下…

在线预览:
账号: admin 密码 123456

项目结构

用户访问了我们的三个程序,每个程序都安装了httpreports,它负责采集一些程序的数据和指标,然后通过http的方式发送到collector,简单处理后,会录入到不同的数据库中,同时,httpreports.ui 负责把这些数据多维度的展示出来。

快速开始

接下来,我会构建监控的 dashboard,然后在我们的.net core 程序中安装httpreports来收集数据,最后展示到ui上,让我们看看这有多简单!

首先,需要初始化数据库,来存储收集的数据,这里我使用的是mysql数据库(或者是sqlserver,postgresql), 我手动创建了一个数据库 httpreports, 记住这个地址,后边会用到。

引用 httpreports.dashboard

首先,我们需要构建 dashboard 来接收,处理和展示数据,dashboard 使用了 vue + antv + elementui 构建了页面,然后把静态文件打包到了程序集,我们只要在.net core 应用中,通过nuget安装即可。

新建一个 .net core 的空的web项目,支持 2.1 及以上版本

新建完成后,通过nuget包分别安装 httpreports.dashboardhttpreports.mysql(或者是httpreports.sqlserver, httpreports.postgresql)。

安装完成之后,需要简单的配置一下,我们直接修改项目的 appsetting.json 文件

{
 "httpreportsdashboard": { 
  "expireday": 3,
  "storage": {
   "connectionstring": "database=httpreports;data source=localhost;user id=root;password=123456;", 
   "defersecond": 3,
   "deferthreshold": 10
  },
  "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
  }
 } 
}

现在参数有很多,不要担心,我们现在只需要检查数据库的连接字符串,确保让它可以成功的连接到你的数据库,其他的参数,你可以在官方的文档中找到它们,本文就不再多说。

修改完 appsetting.json 后,我们接着修改 dahboard 项目的 startup.cs 文件:

public void configureservices(iservicecollection services)
 { 
	services.addhttpreportsdashboard().addmysqlstorage(); 
}

 
public void configure(iapplicationbuilder app, iwebhostenvironment env)
{ 
	app.usehttpreportsdashboard(); 
}

然后 run,如果没有问题的话,会跳转到dashboard的登陆页面,默认的账号:admin 密码: 123456

现在我们有了 dashboard,但是没有数据, 我们还需要在我们的.net core 应用中安装httpreports,它负责采集和发送数据。

引用httpreports

我新建了一个 webapi 项目 userservice(用户服务) ,然后我们通过 nuget 分别安装 httpreports , httpreports.transport.http

安装完成后,同样的,我们修改 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 的项目地址即可
server – 服务的地址,user服务我用了localhost:7000
service – 服务名称 user

修改完成后,我们接着修改 userservice 项目的 startup.cs 文件

app.usehttpreports();
这一行一定要放到 userouting() 和 useendpoints()方法 的上面。

public void configureservices(iservicecollection services)
{
  services.addhttpreports().addhttptransport();
  services.addcontrollers();
}

    
public void configure(iapplicationbuilder app, iwebhostenvironment env)
{
  app.usehttpreports();
  .... 

修改 userservice 项目的启动端口为7000,然后在解决方案中设置多项目启动, 同时运行 userservice 和 dashboard项目。

public static ihostbuilder createhostbuilder(string[] args) =>
  host.createdefaultbuilder(args)
  .configurewebhostdefaults(webbuilder =>
        {
          webbuilder.usestartup<startup>().useurls("http://localhost:7000");
        });

我们多请求几次 userservice 的接口,然后再回到 dashboard的页面,选择一下时间,现在已经可以看到数据了!

到目前为止,我们已经在.net core 的程序中简单的使用了httpreports,还有一些其他的功能,你可以在官方文档中更详细的介绍。

总结

在中小的项目中,你可以使用 httpreports监控你的 .net core程序,这很简单, 并且它是开源的。

另外 httpreports 也有一些其他的问题,比如,难以应对海量数据,没有完全按照 opentrace 规范,这些和项目早期的设计有很大的关系,我也愿意接受大家的批评和意见,开源不能只是崇高的理想主义,社区需要各怀鬼胎的求同存异。

但是没有关系,现在有 opentelemetry,它兼容了opentracing和opencensus, 目前 1.0 版本也才发布了一个月左右,未来它会有更多的应用场景。

所以,我在最近启动了一个新项目 furtuna,是按照 opentelemetry 规范去设计,你完全可以在 java, dotnet,php, go等语言中使用它,当然目前还只是在开发阶段。

关于这个名字,furtuna 是罗马神话最古老的女神之一,能够带来幸福和机遇,寓意也很简单,守护我们的程序,让它变的更好!

到此这篇关于使用 httpreports 监控 .net core 应用程序的方法的文章就介绍到这了,更多相关httpreports 监控 .net core 应用程序内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!