.Net Core 2.*+ InfluxDB+Grafana+App Metrics实时性能监控

 

前言

.net core 2.* 实施性能监控

这个工具其实给运维 大大们用起来是更爽的。但是Grafana现在还没有找到中文版。

本文需要了解的相关技术与内容:

InfluxDb(分布式时序数据库,开源)(注:分布式部分已商业化最新的分布式版本已不在开源,单例的继续开源)

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版本

解压文件夹之后进行如下配置,打开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/ 

 

posted @ 2018-10-17 11:32  Royal丶鬼才  阅读(656)  评论(3编辑  收藏  举报