.Net Core 2.*+ InfluxDB+Grafana+App Metrics實時性能監控

 

前言

.net core 2.* 實施性能監控數據庫

這個工具其實給運維 大大們用起來是更爽的。可是Grafana如今尚未找到中文版。json

本文須要瞭解的相關技術與內容:windows

InfluxDb(分佈式時序數據庫,開源)(注:分佈式部分已商業化最新的分佈式版本已不在開源,單例的繼續開源)app

Grafana(開源的,功能齊全的度量儀表盤和圖形編輯器)運維

App Metrics(主角,開源的支持.NET Core的監控插件,採用管道注入的方式,對代碼的入侵性極小)編輯器

效果圖

 

安裝influxdb

influxdb在1.*版本以後就再也不有網頁版本了。分佈式

因此你們選擇版本的時候必定要注意這個事情省得找不到influxdb 的 admin控制檯工具

下載地址性能

https://dl.influxdata.com/influxdb/releases/influxdb-1.6.3_windows_amd64.zip

我這裏選擇的是1.6.3版本ui

解壓文件夾以後進行以下配置,打開config中的配置文件

修改以下幾項目

[meta]
  # Where the metadata/raft database is stored
  dir = "D:/influxdb/meta"
[data]
  # The directory where the TSM storage engine stores TSM files.
  dir = "D:/influxdb/data"
  # The directory where the TSM storage engine stores WAL files.
  wal-dir = "D:/influxdb/wal"

若是使用的是1.*以前的版本就能夠打開admin控制檯

配置是這樣的

[admin]
  # Determines whether the admin service is enabled.
   enabled = true

  # The default bind address used by the admin service.
   bind-address = ":8083"

而後運行influxdb

influxd -config influxdb.conf

運行起來以後就是這個樣子

 好多 人都使用了1.*以後的版本,沒有admin控制檯給你們 推薦一個工具

InfluxDBStudio-0.1.0  用這個貨建立一個數據庫就能夠了

配置Grafana

下載地址:

https://s3-us-west-2.amazonaws.com/grafana-releases/release/grafana-5.3.1.windows-amd64.zip 

解壓以後 在文件夾以後直接運行這個程序

grafana-server.exe

Grafana默認會監聽3000的端口,因此咱們進入

http://127.0.0.1:3000,
默認帳號密碼:admin admin

就是這樣的效果啦

 

安裝默認的模板

地址以下:https://grafana.com/dashboards/2125

 

這裏選擇剛纔咱們下載的json文件就能夠,或者直接輸入2125均可以

添加數據庫配置文件

配置好了以後就會出現這個樣子了

 

在.net core配置

先把這些DLL引用了吧。項目右鍵編輯帖進去  從新生成一下就能夠了。

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

修改appsettings.json配置文件

{
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "InfluxDB": {
    "IsOpen": true,
    "DataBaseName": "LogDb",
    "ConnectionString": "http://127.0.0.1:8086",
    "username": "admin",
    "password": "xxxxxxxxxx",
    "app": "LogDbDemo",
    "env": "stage"
  }
}

修改startup進行管道接管

 ConfigureServices添加 以下

            #region Metrics監控配置
            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

 

 Configure添加這些東西

      #region 注入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

運行項目跑一圈以後 返回Grafana就出現了這樣的圖例

 

後續

剛剛建立了一個.net core的羣歡迎你們進入:

點擊連接加入羣聊【.Net Core研究團】:https://jq.qq.com/?_wv=1027&k=5IEolkJ

若是個人文章對您有幫助,但願您能夠點個贊。最近打算申請mvp。但願你們幫忙。

特別感謝 

獅子 :https://www.cnblogs.com/GuZhenYin/ 

相關文章
相關標籤/搜索