The. Net Core 2.0+ Influxdb+grafana+app Metrics enables real-time performance monitoring across platforms

Source: Internet
Author: User

Recently this time has been busy, no time to write a blog, responsible for a project, from the front to the back end has been busy, while there are other projects of the system architecture to be processed.

Last year began to focus on net core, but usually write demo, useless in the project, just this opportunity to use the net core, specifically when the beginning is not very remember, in short, just started with the core 1.0 development, and then in the development of a sudden thought, Usually our project has not done the real-time monitoring of the project, why not try it this time, but also can know what time of day of traffic trend, system throughput and so on. Remember before going to Beijing head office, see Java Development Department There is a big screen, real-time display of project throughput, request volume and other information, feel very cool, we net should be able to achieve it.

Holding this general mentality to find some relevant information, also in the project used up.

The project is deployed in a Windows environment, and Influxdb's introduction is not covered here.

1, first install the INFLUXDB time series database, the address is as follows: Https://portal.influxdata.com/downloads#influxdb, here i download Windows Binaries (64-bit), Specifically, write the configuration file and installation process.

Open influxdb.conf after decompression, because the default configuration of Influxdb is all for Linux configuration, so we need to modify the configuration file.

Change the path of the following 3 Liunx to the WINODWS path 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 put the influxdb files in the Influxdb directory, can be used relative position, can also use absolute position, this can be modified according to individual needs.

Here to mention, Influxdb v1.2.4 after the page as if the graphical management interface is removed, 1.2.4 before the page has a graphical management interface, influxdb.conf file before there is

[Admin]  # Determines whether the Admin service is enabled.   Enabled = True  # The default bind address used by the Admin service.   bind-address = ": 8083"

However, in the later version, the Conf file does not have this, so we have to pay attention to. There is no way to use the graphical management of Web pages, if you want to connect influxdb can be in the directory with cmd run Influx.exe, if you do not want everyone can access your influxdb, then you can configure the Conf file authentication information

# 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

Last cmd run, go to your unzip directory, execute command:

Influxd-config influxdb.conf

Here's to say, when using Influx.exe login, enter the following command: Influx-host 127.0.0.1-port 8086-username "admin"-password "123456", so that the influxdb is connected. Then create the database: "Appmetricsdemo".

If you feel this is more troublesome, you can install two influxdb, One is the V1.2.4 version, one is the latest version, this is the need to modify the configuration file port, the two Influxdb port modified to a different, and then use 1.2.4 version of the connection to the newest version,, click the gear icon in the upper right corner to appear the connection form,

(After installing the INFLUXDB, remember to create the demo database "Appmetricsdemo" in Influxdb)

2, install Grafana,:https://grafana.com/get, we extracted into the bin directory,

Run Grafana-server.exe directly.

Grafana listens to port 3000 by default, so we enter http://localhost:3000,

Will let you login, directly enter the local administrator account can, account: admin password: admin, enter after

After the installation is complete, we download the JSON file for the relevant instrument template.

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

Then we import our meter: the operation can

Add the data source above us,

Select Add DataSource, and then proceed as follows:

In this way, we have completed the Grafana installation configuration and added the data source.

Because of the actual project, we would not be able to run a console on the server to open these services, so we need to publish influxdb and Grafana as a service to run on the server, where we can use the NSSM tool to encapsulate Influxdb and Grafana into a service run.

as follows: Http://www.nssm.cc/download

After decompression into the corresponding system version of the folder, there is a 32-bit and 64-bit files, according to their actual situation to choose,

We choose Win64, run cmd after entering the folder, enter NSSM install InfluxDB after running, the following interface appears:

Focus on the parameters of this column, argument input:-config influxdb.conf, similar to the above in cmd input influxd-config influxdb.conf

After the installation is good, Grafana installation is similar to the above operation, but argument this column does not need to enter anything.

3. Netcore using AppMetrics, create a new API named Demo, then edit right-click Edit Demo.csproj file, add the following package under the ItemGroup node

<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 the AppMetrics related class library

Then modify the 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"  }}

The Configureservices method is 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 Isope n = 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 Inject 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 (); }    }}

This piece of code is basically finished.

Next run the project, visit the following, and then look at the dashboard in Grafana

Enclosed Demo Address: Https://github.com/landonzeng/AppMetricsDemo

The. Net Core 2.0+ Influxdb+grafana+app Metrics enables real-time performance monitoring across platforms

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.