.net core使用App.Metrics+InfluxDB+Grafana进行APM监控

一、InfluxDB

1、下载InfluxDB

wget https://dl.influxdata.com/influxdb/releases/influxdb-1.5.2.x86_64.rpm

2、安装InfluxDB

rpm -ivh influxdb-1.5.2.x86_64.rpm
systemctl start influxdb.service

3、创建库、用户

influx
CREATE DATABASE qkaweb
use qkaweb
create user "leizhihui" with password '123456'

二、Grafana

1、下载、安装:

wget https://s3-us-west-2.amazonaws.com/grafana-releases/release/grafana-5.1.2-1.x86_64.rpm
yum localinstall grafana-5.1.2-1.x86_64.rpm

2、设置开机自动启动:

systemctl daemon-reload
systemctl enable grafana-server.service

3、启动服务并查看服务状态:

systemctl start grafana-server.service
systemctl status grafana-server.service

4、访问ip+3000端口,用户名和密码均为admin进行登录,

5、创建数据源,如下图所示:

 6、导入Dashboard:

输入2125后点击Import按钮即导入App Metrics模板。

三、网关项目中添加App Metrics

1、添加下面nuget引用:

App.Metrics
App.Metrics.Extensions.Middleware
App.Metrics.Extensions.Mvc
App.Metrics.Extensions.Reporting.InfluxDB
App.Metrics.Extensions.Middleware
App.Metrics.Formatters.Json

2、修改appsettings.json文件:

"AppMetrics": {
    "DataBaseName": "qkaweb",
    "ConnectionString": "http://192.168.2.168:8086",
    "UserName": "leizhihui",
    "Password": "123456",
    "App": "QkaWebApis",
    "Env": "develop"
  }

3、添加如下代码:

        public void ConfigureServices(IServiceCollection services)
        {

            #region App Metrics
            AppMetricsConfig appMetricsConfig = Configuration.GetSection("AppMetrics").Get<AppMetricsConfig>();
            var uri = new Uri(appMetricsConfig.ConnectionString);

            var metrics = AppMetrics.CreateDefaultBuilder()
            .Configuration.Configure(
            options =>
            {
                options.AddAppTag(appMetricsConfig.App);
                options.AddEnvTag(appMetricsConfig.Env);
            })
            .Report.ToInfluxDb(
            options =>
            {
                options.InfluxDb.BaseUri = uri;
                options.InfluxDb.Database = appMetricsConfig.DataBaseName;
                options.InfluxDb.UserName = appMetricsConfig.UserName;
                options.InfluxDb.Password = appMetricsConfig.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
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            app.UseMetricsAllMiddleware();
            app.UseMetricsAllEndpoints();
        }

 

posted @ 2018-05-14 18:04  focus-lei  阅读(2693)  评论(1编辑  收藏  举报