Use HttpReports APM in ASP.NET Core application

springleee

Spring

Posted on March 11, 2021

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.

After the installation is complete, you need a simple configuration, we directly modify the appsetting.json file of the project.

{
 "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
    }
  } 
}
Enter fullscreen mode Exit fullscreen mode

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.

public void ConfigureServices(IServiceCollection services)
 { 
    services.AddHttpReportsDashboard().AddMySqlStorage(); 
}


public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{ 
    app.UseHttpReportsDashboard(); 
}
Enter fullscreen mode Exit fullscreen mode

Then run the program, if there is no problem, it will jump to the login page of Dashboard, the default account: admin password: 123456

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.

{
  "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
  }
}
Enter fullscreen mode Exit fullscreen mode

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.

public void ConfigureServices(IServiceCollection services)
{
     services.AddHttpReports().AddHttpTransport();
     services.AddControllers();
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
     app.UseHttpReports();
     ....
Enter fullscreen mode Exit fullscreen mode

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.


public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
    .ConfigureWebHostDefaults(webBuilder =>
                {
                     webBuilder.UseStartup<Startup>().UseUrls("http://localhost:7000");
                });

Enter fullscreen mode Exit fullscreen mode

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.

https://www.yuque.com/httpreports/docs/uyaiil

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.

💖 💪 🙅 🚩
springleee
Spring

Posted on March 11, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related