Use HttpReports APM In ASP.NET Core Application

Introduction

 
HttpReports APM monitoring system developed based on .NET Core,It uses the MIT open source license,the main functions include statistics, analysis, visualization, monitoring, tracking, etc.,it is very simple, so it is suitable for use in small and medium-sized projects.
 
 
I am also honored to have a simple sharing at the .NET Conf China 2020 conference, if it helps you, you can give me a star,thanks.
 
live demo: http://apm.nonop.cn/
account: admin password: 123456
github:https://github.com/dotnetcore/HttpReports
 
Architecture
We now have three asp net core programs,HttpReports components are installed in each program,it will collect some program running data,then send to Collector via Http,after a simple calculation,will be recorded in different databases,then, these data are displayed through HttpReports.UI.
 
Getting Started,
 
Next, I will build a monitoring dashboard, then install HttpReports in our .NET Core program to collect data, and finally display it on the UI, let's see how easy it is!
 
First of all, you need to initialize the database to store the collected data. Here I am using a MySql database (or SqlServer, PostgreSQL). I manually created a database HttpReports. Remember this address, you will use it later.
 

Install HttpReports.Dashboard

 
First, we need a Dashboard,it is responsible for receiving, processing and displaying data,we install it in Nuget.
 
We create a new asp net core web application,you can select ASP.NET Core Empty,support .NET Core 2.1 version and above.
 
 
After creating the project, we install HttpReports.Dashboard, HttpReports.MySQL (or HttpReports.SqlServer, HttpReports.PostgreSQL) through Nuget.
 
Use HttpReports APM in ASP.NET Core application
 
Use HttpReports APM in ASP.NET Core application
 
After the installation is complete, you need a simple configuration, we directly modify the appsetting.json file of the project.
  1. {  
  2.  "HttpReportsDashboard": {   
  3.     "ExpireDay": 3,  
  4.     "Storage": {  
  5.       "ConnectionString""DataBase=HttpReports;Data Source=localhost;User Id=root;Password=123456;",   
  6.       "DeferSecond": 3,  
  7.       "DeferThreshold": 10  
  8.     },  
  9.    "Check": {  
  10.       "Mode""Self",  
  11.       "Switch"true,  
  12.       "Endpoint""",  
  13.       "Range""500,2000"  
  14.     },  
  15.     "Mail": {  
  16.       "Server""smtp.163.com",  
  17.       "Port": 465,  
  18.       "Account""[email protected]",  
  19.       "Password""*******",  
  20.       "EnableSsL"true,  
  21.       "Switch"true  
  22.     }  
  23.   }   
  24. }  
You can see that there are many parameters, don’t worry, we just need to check the connection string of the database to make sure that it can successfully connect to your database, other parameters are introduced, you can find them in the official documentation, this article Say no more.
 
After modifying appsetting.json, we then modify the Startup.cs file of the Dahboard project.
  1. public void ConfigureServices(IServiceCollection services)  
  2. {   
  3.     services.AddHttpReportsDashboard().AddMySqlStorage();   
  4. }  
  5. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)  
  6. {   
  7.     app.UseHttpReportsDashboard();   
  8. }   
Then run the program, if there is no problem, it will jump to the login page of Dashboard, the default account: admin password: 123456
 
Use HttpReports APM in ASP.NET Core application
 
 

Install HttpReports

 
I created a new WebAPI project,assuming it is our user service,then we install HttpReports, HttpReports.Transport.Http through Nuget.
 
 
After the installation is complete, we also modify appsettings.json.
  1. {  
  2.   "HttpReports": {  
  3.     "Transport": {  
  4.       "CollectorAddress""http://localhost:5000/",  
  5.       "DeferSecond": 10,  
  6.       "DeferThreshold": 100  
  7.     },  
  8.     "Server""http://localhost:7000",  
  9.     "Service""User",  
  10.     "Switch"true,  
  11.     "RequestFilter": [ "/api/health/*""/HttpReports*" ],  
  12.     "WithRequest"true,  
  13.     "WithResponse"true,  
  14.     "WithCookie"true,  
  15.     "WithHeader"true  
  16.   }  
  17. }  
Parameter introduction 
  • Transport-CollectorAddress: The address where the data is sent in batches,here we configure the address of the Dashboard project.
  • Server : The address of the user service, like localhost:7000
  • Service : The service name of the user service.
After the modification is completed, we then modify the Startup.cs file of the UserService project.
 
app.UseHttpReports();
 
This line of code must be placed above the UseRouting() and UseEndpoints() methods.
  1. public void ConfigureServices(IServiceCollection services)  
  2. {  
  3.      services.AddHttpReports().AddHttpTransport();  
  4.      services.AddControllers();  
  5. }  
  6.   
  7. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)  
  8. {  
  9.      app.UseHttpReports();  
  10.      ....  
Modify the startup port of the UserService project to localhost:7000, then set up multi-project startup in the solution, and run the UserService and Dashboard projects at the same time. 
  1. public static IHostBuilder CreateHostBuilder(string[] args) =>  
  2. Host.CreateDefaultBuilder(args)  
  3. .ConfigureWebHostDefaults(webBuilder =>  
  4. {  
  5.   webBuilder.UseStartup<Startup>().UseUrls("http://localhost:7000");  
  6. });  
We request the UserService interface several times, and then go back to the Dashboard page, choose a time, and now we can see the data!
 
 
So far, we have simply used HttpReports in .NET Core programs, and there are some other functions, which you can introduce in more detail in the official documentation.
 

Summary

 
In small and medium-sized projects, you can use HttpReports to monitor your .NET Core program, which is very simple, and it is open source.