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 handled.

Last year, I started to pay attention to itnet core了, It's just writingdemo, Not used in the project, It's a good chancenet
core, I don't remember when it started, In short, it was used at the beginningcore
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, noticejava There is a big screen in the development department, Real time display of project throughput, Request quantity and other information, It's very cool, Wenet It should be possible.

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

Project deployed atwindows Under the environment,Influxdb We will not repeat the introduction of.

Two, installInfluxdb


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

Open after unzippinginfluxdb.conf, becauseinfluxdb The default configuration of is all forlinux Configured, So we need to modify the configuration file.

Modify the following3 individualliunx Path, Change towinodws 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 willinfluxdb We have all the documentsinfluxdb 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 connectInfluxdb You can use thecmd Functioninflux.exe, If you don't want everyone to have access to yourInfluxDB, Then you canconf 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 up true, First inauth-enabled = false
When authentication is not enabled, Create an account with administrator permission, The order is as follows:CREATE USER "admin" WITH PASSWORD '123456' WITH
ALL PRIVILEGES, After the account is created, takeauth-enabled Set up
true, Then startinfluxdb, Finally, create an account named"AppMetricsDemo" Data, The order is as follows:CREATE DATABASE
"AppMetricsDemo")

 

Lastcmd Function, Go to your unzip directory, Executive order:
influxd -config influxdb.conf
Let's talk about it here, Useinflux.exe Login time, Enter the following command:influx -host 127.0.0.1 -port 8086 -username
"admin" -password "123456", So it's connectedInfluxDB了. Then create the database:CREATE DATABASE
"AppMetricsDemo".


If you think it's more troublesome, Two can be installedinfluxDB, One isV1.2.4 Edition, One is the latest version, This is the port where the profile needs to be modified, Will twoinfluxDB Just change the port of, Then use1.2.4 Connect the latest version of, Pictured, Click the gear icon in the upper right corner to display the connection form,



( Install wellinfluxDB after, Remember ininfluxDB Create inDemo Database required“AppMetricsDemo”)

Three, installGrafana

installGrafana, Download address:https://grafana.com/get, We unzip and enterbin Catalog, Pictured:



Direct operationgrafana-server.exe that will do.

Grafana Listen by default3000 Port, So we enterhttp://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 theJson 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, Pictured:



ChoiceAdd DataSource, Then the operation is as follows:



such, We're doneGrafana 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 toinfluxDB andGrafana Publish as a service and run on the server, Here we can usenssm Tool willInfluxDB andGrafana 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 inside.32 Bit sum64 Bit file, Choose according to your actual situation, Pictured:



We choosewin64, Run after entering foldercmd, inputnssm install InfluxDB The following interface appears after operation:



Focus on the parameter column,Argument Internal input:-config influxdb.conf, Similar to the abovecmd Input inInfluxd -config
influxdb.conf

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

Four,.netCore Use inAppMetrics

.netCore Use inAppMetrics, Create a new one namedDemo Ofapi, Then right click to edit
Demo.csproj file, stayItemGroup Add the following under nodePackage

<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 preservation, The project referencesAppMetrics Related class library



Then modifyappsettings.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 injectionMetrics
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 the following, Then look at it.Grafana Look at the dashboard in



Encloseddemo address:https://github.com/landonzeng/AppMetricsDemo