One , brief introduction

I've been busy these days , No time to blog , In charge of a project , Busy from front end to back end , At the same time, the system architecture of other projects needs to be dealt with .

Last year, I started to pay attention to it net core了, It's just writing demo, Not used in the project , It's a good chance net
core, I don't remember when it started , In short, it was used at the beginning core
1.0 development , And then I suddenly thought of it when I was developing , We usually don't do real-time monitoring in our projects , Why don't you try this time , And we can also know what time of the day the traffic trend is , System throughput, etc . Remember when I went to Beijing head office , notice java There is a big screen in the development department , Real time display of project throughput , Request quantity and other information , It's very cool , We net It should be possible .

With this general mentality, I went to find some relevant information , It's also used in the project .

Project deployed at windows Environment ,Influxdb We will not repeat the introduction of .

Two , install Influxdb


Install first InfluxDB temporal database , The address is as follows :https://portal.influxdata.com/downloads#influxdb , I'll download it here Windows
Binaries (64-bit), Write down the configuration file and installation process .

Open after unzipping influxdb.conf, because influxdb The default configuration of is all for linux Configured , So we need to modify the configuration file .

Modify the following 3 individual liunx Path to , Change to winodws The path is as follows :
[meta] # Where the metadata/raft database is stored dir = "influxdb/meta"
[data] # The directory where the TSM storage engine stores TSM files. dir =
"influxdb/data" # The directory where the TSM storage engine stores WAL files.
wal-dir = "influxdb/wal"
I will influxdb The files are all in influxdb Directory , Relative position can be used , Absolute position can also be used , This can be modified according to personal needs .

Let's mention it here ,Influxdb v1.2.4 After that, it seems that the graphical management interface of the web page has been removed ,1.2.4 Before that, there was a graphical management interface for web pages ,influxdb.conf Before the document
[admin] # Determines whether the admin service is enabled. enabled = true #
The default bind address used by the admin service. bind-address = ":8083"

But later versions ,conf This is not in the document , So we need to pay attention . Later versions can't use web graphical management , If you want to connect Influxdb You can use the cmd function influx.exe, If you don't want everyone to have access to your InfluxDB, Then you can conf Configure authentication information in the file

# Determines whether HTTP endpoint is enabled.
enabled = true

# The bind address used by the HTTP service.
bind-address = ":8086"

# Determines whether user authentication is enabled over HTTP/HTTPS.
auth-enabled = true

( be careful : Before the account password is configured , Please don't put the above :auth-enabled Set to true, First auth-enabled = false
When authentication is not enabled , Create an account with administrator permission , The command is as follows :CREATE USER "admin" WITH PASSWORD '123456' WITH
ALL PRIVILEGES, After the account is created , take auth-enabled Set to
true, Then start influxdb, Finally, create an account named "AppMetricsDemo" Data for , The command is as follows :CREATE DATABASE
"AppMetricsDemo")

 

last cmd function , Go to your unzip directory , Execute command :
influxd -config influxdb.conf
Let's talk about it here , use influx.exe At login , Enter the following command :influx -host 127.0.0.1 -port 8086 -username
"admin" -password "123456", So it's connected InfluxDB了. Then create the database :CREATE DATABASE
"AppMetricsDemo".


If you think it's more troublesome , Two can be installed influxDB, One is V1.2.4 Version of , One is the latest version , This is the port where the profile needs to be modified , Put two influxDB Just change the port of , Then use 1.2.4 Connect the latest version of , As shown in the figure , Click the gear icon in the upper right corner to display the connection form ,



( Installed influxDB after , Remember in influxDB Created in Demo Database required “AppMetricsDemo”)

Three , install Grafana

install Grafana, Download address :https://grafana.com/get, We unzip and enter bin catalog , As shown in the figure :



Direct operation grafana-server.exe that will do .

Grafana Listen by default 3000 Port for , So we enter http://localhost:3000,

Will let you log in , Enter the local administrator account directly , account :admin   password :admin, As shown in the figure after entering :



After installation , We download the Json file .

The address is as follows :https://grafana.com/dashboards/2125

And then we import our instruments : Just operate as shown in the figure



 

Add data source above us , As shown in the figure :



choice Add DataSource, Then the operation is as follows :



such , We're done Grafana Installation configuration and add data source for .


Because in the actual project , We can't run a console on the server to start these services , So we need to influxDB and Grafana Publish as a service and run on the server , Here we can use nssm The tool will InfluxDB and Grafana Encapsulate as service running .

The download address is as follows :http://www.nssm.cc/download

After decompression, enter the corresponding system version folder , There's a 32 Bitwise sum 64 File of bits , Choose according to your actual situation , As shown in the figure :



We choose win64, Run after entering folder cmd, input nssm install InfluxDB The following interface appears after operation :



Focus on the parameter column ,Argument Input in :-config influxdb.conf, Similar to the above cmd Middle input Influxd -config
influxdb.conf

It's good to run after installation ,Grafana The installation of is similar to the above operation , just Argument You don't need to enter anything in this field .

Four ,.netCore Used in AppMetrics

.netCore Used in AppMetrics, Create a new one named Demo Of api, Then right click to edit
Demo.csproj file , stay ItemGroup Add the following under node Package

<PackageReference Include="App.Metrics" Version="2.0.0-alpha" />
<PackageReference Include="App.Metrics.AspNetCore.Endpoints"
Version="2.0.0-alpha" />
<PackageReference Include="App.Metrics.AspNetCore.Reporting"
Version="2.0.0-alpha" />
<PackageReference Include="App.Metrics.AspNetCore.Tracking"
Version="2.0.0-alpha" />
<PackageReference Include="App.Metrics.Extensions.Reporting.InfluxDB"
Version="1.2.0" />
<PackageReference Include="App.Metrics.Formatters.Json" Version="2.0.0-alpha"
/>
<PackageReference Include="App.Metrics.Reporting.InfluxDB"
Version="2.0.0-alpha" />

  After saving , The project references AppMetrics Related class library



Then modify appsettings.json file . Add the following code
{ "Logging": { "IncludeScopes": false, "Debug": { "LogLevel": { "Default":
"Warning" } }, "Console": { "LogLevel": { "Default": "Warning" } } },
"InfluxDB": { "IsOpen": true, "DataBaseName": "AppMetricsDemo",
"ConnectionString": "http://10.10.134.109:8086", "username": "admin",
"password": "123456", "app": "RepairApp", "env": "stage" } }
ConfigureServices method , as follows :
using System; using System.Collections.Generic; using System.Linq; using
System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using
Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using
Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options; using App.Metrics; namespace Demo { public
class Startup { public Startup(IConfiguration configuration) { Configuration =
configuration; } public IConfiguration Configuration { get; } // This method
gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services) { #region
Metrics Monitoring configuration string IsOpen =
Configuration.GetSection("InfluxDB")["IsOpen"].ToLower(); if (IsOpen == "true")
{ string database = Configuration.GetSection("InfluxDB")["DataBaseName"];
string InfluxDBConStr =
Configuration.GetSection("InfluxDB")["ConnectionString"]; string app =
Configuration.GetSection("InfluxDB")["app"]; string env =
Configuration.GetSection("InfluxDB")["env"]; string username =
Configuration.GetSection("InfluxDB")["username"]; string password =
Configuration.GetSection("InfluxDB")["password"]; var uri = new
Uri(InfluxDBConStr); var metrics = AppMetrics.CreateDefaultBuilder()
.Configuration.Configure( options => { options.AddAppTag(app);
options.AddEnvTag(env); }) .Report.ToInfluxDb( options => {
options.InfluxDb.BaseUri = uri; options.InfluxDb.Database = database;
options.InfluxDb.UserName = username; options.InfluxDb.Password = password;
options.HttpPolicy.BackoffPeriod = TimeSpan.FromSeconds(30);
options.HttpPolicy.FailuresBeforeBackoff = 5; options.HttpPolicy.Timeout =
TimeSpan.FromSeconds(10); options.FlushInterval = TimeSpan.FromSeconds(5); })
.Build(); services.AddMetrics(metrics); services.AddMetricsReportScheduler();
services.AddMetricsTrackingMiddleware(); services.AddMetricsEndpoints(); }
#endregion services.AddMvc(); } // This method gets called by the runtime. Use
this method to configure the HTTP request pipeline. public void
Configure(IApplicationBuilder app, IHostingEnvironment env) { if
(env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } #region injection Metrics
string IsOpen = Configuration.GetSection("InfluxDB")["IsOpen"].ToLower(); if
(IsOpen == "true") { app.UseMetricsAllMiddleware(); // Or to cherry-pick the
tracking of interest app.UseMetricsActiveRequestMiddleware();
app.UseMetricsErrorTrackingMiddleware();
app.UseMetricsPostAndPutSizeTrackingMiddleware();
app.UseMetricsRequestTrackingMiddleware();
app.UseMetricsOAuth2TrackingMiddleware();
app.UseMetricsApdexTrackingMiddleware(); app.UseMetricsAllEndpoints(); // Or to
cherry-pick endpoint of interest app.UseMetricsEndpoint();
app.UseMetricsTextEndpoint(); app.UseEnvInfoEndpoint(); } #endregion
app.UseMvc(); } } }
Code is basically done .

Five , effect

Next, run the project , Visit , Then take a look Grafana Look at the dashboard in



enclosed demo address :https://github.com/landonzeng/AppMetricsDemo