性能监控.md

性能监控

使用技术:

  • App.Metrics 负责记录性能信息
  • InfluxDB 负责存储性能信息
  • General 负责显示性能数据

App.Metrics

App Metrics是一个开源、跨平台的.net库,用于记录应用中的指标。App Metrics支持在.net Core或完整.net4.5.2.net框架上运行。

在项目中添加一下包:

  • App.Metrics.Aspnetcore.All 该包包含AspNetCore应用的所有特性。
  • App.Metrics.InfluxDB 为App Metrics InfluxDB 的综合包

Startup.cs类中添加一下代码

  • ConfigureServices方法中添加一下代码
    #region appmetics
    var influxOptions = new MetricsReportingInfluxDbOptions();
    // 获取influx选项
    configuration.GetSection(nameof(MetricsReportingInfluxDbOptions)).Bind(influxOptions);
    influxOptions.MetricsOutputFormatter = new MetricsInfluxDbLineProtocolOutputFormatter();
    var metrics = AppMetrics.CreateDefaultBuilder()
        .Report.ToInfluxDb(influxOptions).Build();

    services.AddMetrics(metrics);
    services.AddMetricsReportingHostedService();
    services.AddMetricsEndpoints(configuration);
    services.AddMetricsTrackingMiddleware(configuration);
    services.AddAppMetricsCollectors();
    #endregion
  • Configure方法中添加一下代码
    #region appmetics
    app.UseMetricsAllEndpoints();
    app.UseMetricsAllMiddleware();
    #endregion

appsettings.json添加以下配置

{    
  "MetricsOptions": {
    "DefaultContextLabel": "SWKeJiFileManage",
    "Enabled": true
  },
  "MetricsWebTrackingOptions": {
    "ApdexTrackingEnabled": true,
    "ApdexTSeconds": 0.1,
    "IgnoredHttpStatusCodes": [ 404 ],
    "IgnoredRoutesRegexPatterns": [],
    "OAuth2TrackingEnabled": true
  },
  "MetricEndpointsOptions": {
    "MetricsEndpointEnabled": true,
    "MetricsTextEndpointEnabled": true,
    "PingEndpointEnabled": true,
    "EnvironmentInfoEndpointEnabled": true
  },
  "MetricsReportingInfluxDbOptions": {
    "InfluxDb": {
      "BaseUri": "http://127.0.0.1:8086",   // influxdb服务地址
      "Database": "SWKeJiFileManageBucket"  // 数据库的名称
      //"Consistenency": "",
      //"UserName": "Spf",
      // "Password": "123456789"
      //"RetentionPolicy": ""
    },
    "HttpPolicy": {
      "BackoffPeriod": "0:0:30",
      "FailuresBeforeBackoff": 5,
      "Timeout": "0:0:40"
    },
    "ReportInterval": "0:0:1"
  },
}

InfluxDB

InfluxDB是一个时间序列平台。

InflxDB使开发者能够构建物联网、分析和监控软件。它是专门为处理传感器、应用程序和基础设施产生的大量和无数的时间戳数据而设计的。

注意

App.Metrics4.2.0 版本只支持InfluxDB1.X不支持InfluxDB2.0

InfluxDB下载地址

InfluxDB1.8.9版本下载下来后直接解压,得到以下文件:

  • influx.exe influx客户端,可以通过命令操作Influx,或查看数据库
  • influx_inspect.exe 作用未知
  • influx_stress.exe 压力测试
  • influxd.exe 服务端
  • influxdb.conf InfluxDB配置

只需要配置好 influxdb.conf,然后在命令提示符窗口中执行以下命令,即可启动InfluxDB服务

influxd -config influxdb.conf

配置好后的启动效果:

influx服务运行效果
influx服务运行效果

influxdb.conf 配置

# Bind address to use for the RPC service for backup and restore.
 bind-address = "127.0.0.1:8087"

端口给PRC服务一个端口,注意不要与现有端口冲突

配置允许Http请求

[http]
  # Determines whether HTTP endpoint is enabled.
   enabled = true

  # Determines whether the Flux query endpoint is enabled.
   flux-enabled = true

  # Determines whether the Flux query logging is enabled.
   flux-log-enabled = true

  # The bind address used by the HTTP service.
   bind-address = ":8086"

  # Determines whether user authentication is enabled over HTTP/HTTPS.
   auth-enabled = false

  # The default realm sent back when issuing a basic auth challenge.
   realm = "InfluxDB"

  # Determines whether HTTP request logging is enabled.
   log-enabled = true

  # Determines whether the HTTP write request logs should be suppressed when the log is enabled.
   suppress-write-log = true

  # Enables authentication on the /ping, /metrics, and deprecated /status
  # endpoints. This setting has no effect if auth-enabled is set to false.
   ping-auth-enabled = true

  # Determines whether HTTPS is enabled.
  # https-enabled = true

General

Grafana允许您查询、可视化、警告和理解指标,而不管它们存储在哪里。与您的团队一起创建、探索和分享漂亮的仪表板,并培养一种数据驱动的文化。

下载地址

Windows版本安装好后直接在浏览器输入http://localhost:3000,登陆进General

连接InfluxDB数据库

  1. 在【Configuration】菜单中点击【Data Sourcess】菜单进入数据库管理界面
    数据库管理界面
  2. 在数据库管理界面点击【Add data source】按钮,然后选择【InfluxDB】
  3. 输入数据库名称、Http请求地址后保存并测试
    设置数据库
    设置数据库

添加仪表盘

导入面板1
导入面板1

可以Json文件、Json配置或直接通过ID导入

仪表盘模版地址

我们导入【App Metrics - Web Monitoring - InfluxDB】仪表盘模版

posted @ 2021-08-26 17:14  $("#阿飞")  阅读(96)  评论(0编辑  收藏  举报