快来为你的.NET应用加个监控吧!

本文涉及的产品
可观测可视化 Grafana 版,10个用户账号 1个月
简介: 快来为你的.NET应用加个监控吧!

导读


CZGL.ProcessMetrics 是一个 Metrics 库,能够将程序的 GC、CPU、内存、机器网络、磁盘空间等信息记录下来,使用 Prometheus 采集信息,然后使用 Grafana 显示。


视频地址:

https://www.bilibili.com/video/BV18y4y1K7Ax/

效果图预览:

微信图片_20220505155515.png

微信图片_20220505155519.png


安装 ProcsssMetrics


只需要通过 Nuget 安装一个库,即可快速为程序添加资源监视,ProcssMetrics 同时支持 Winform、Wpf、ASP.NET Core 等。


CZGL.ProcessMetrics 支持 .NET Standard 2.0 和 .NET Core 3.1,但是在 .NET Standard 2.0 中,因为缺少部分 Core API,所以有部分信息是无法获取的,这部分信息如下:


标识 .NET Core API 说明
gc_memory_info GC.GetGCMemoryInfo() 获取 GC 内存信息
total_allocated_bytes GC.GetTotalAllocatedBytes() 总分配量
dotnet_lock_contention_total Monitor.LockContentionCount 线程池竞争数量


新建一个应用, Nuget 中搜索 CZGL.ProcessMetrics 直接引用即可。

Nuget 地址:https://www.nuget.org/packages/CZGL.ProcessMetrics

有两种方式使用 Metrics,第一种是使用内置的 HttpListener,不需要放到 Web 中即可独立提供 URL 访问,适合 winform、wpf 或纯 控制台等应用。但是使用 HttpListener,需要使用管理员方式启动应用才能正常运行。


使用方法:

using CZGL.ProcessMetrics;
... ...
MetricsServer metricsServer = new MetricsServer("http://*:1234/metrics/");
metricsServer.Start();


另外一种是使用 ASP.NET Core,Metrics 作为中间件加入到 Web 应用中,此时使用的是 kestrel 。


在 Nuget 中,搜索 CZGL.ProcessMetrics.ASPNETCore 包,然后使用中间件生成 Metrics 端点。

app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
                endpoints.ProcessMetrices("/metrics");
            });


但是目前无论哪种,都必须让暴露端口出去,让 Prometheus 能够访问到 API。后期会增加支持不需要暴露 API 、提供 Web 服务,即可直接推送监控信息到 Prometheus 的功能。


访问相应的 URL,可以看到有很多信息输出,这些都是 Prometheus 数据的格式。


http://127.0.0.1:1234/metrics


微信图片_20220505155530.png


搭建 Prometheus/Grafana


这里我们使用 Docker 来搭建监控平台。

拉取镜像:

docker pull prom/prometheus
docker pull grafana/grafana


/opt/prometheus 目录下,新建一个 prometheus.yml 文件,其内容如下:

# my global config
global:
  scrape_interval:     15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
  # scrape_timeout is set to the global default (10s).
# Alertmanager configuration
alerting:
  alertmanagers:
  - static_configs:
    - targets:
      # - alertmanager:9093
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: 'prometheus'
    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.
    static_configs:
    - targets: ['localhost:9090']
  - job_name: 'processmetrice'
    metrics_path: '/metrics'
    static_configs:
    - targets: ['123.123.123.123:1234']


请替换最后一行的 IP。

使用容器启动 Prometheus:

docker run  -d   -p 9090:9090   -v /opt/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml    prom/prometheus


使用容器启动 Grafana:

mkdir /opt/grafana-storage
chmod 777 -R /opt/grafana-storage
docker run -d   -p 3000:3000   --name=grafana   -v /opt/grafana-storage:/var/lib/grafana   grafana/grafana


打开 9090 端口,在菜单栏中打开 Status-Targets,可以看到有相关记录。

微信图片_20220505155534.png

接着,访问 3000 端口,打开 Grafana,初始账号密码都是 admin 。


配置 Grafana


首先我们要为 Grafana 获取 Prometheus 中的监控数据,我们要添加一个数据源。

微信图片_20220505155706.png

选择 Prometheus,按照提示,填写好 HTTP-URL 即可。

微信图片_20220505155709.png

接着,下载笔者定制好的 Jsom Model,文件名为 CZGL.ProcessMetrics.json

下载地址:

https://github.com/whuanle/CZGL.SystemInfo/releases/tag/v1.0

然后导入模型文件。


微信图片_20220505155713.png

微信图片_20220505155716.png

即可看到监控界面。

微信图片_20220505155720.gif

相关文章
|
4月前
|
算法 Java 调度
|
7月前
|
Kubernetes 关系型数据库 数据库
.netcore应用容器化部署
.netcore应用容器化部署
|
7月前
|
Kubernetes 数据库 C++
.netcore应用在WSL中的容器化部署
前面,我们讲解了如何在wsl中安装数据库,.netcore运行时,以及如何发布一个.netcore应用,为了构成一个完整的小系列,本节,我们来学习一下,如何将.netcore应用在WSL中容器化。
|
7月前
|
安全 C# 开发工具
模拟.NET应用场景,综合应用反编译、第三方库调试、拦截、一库多版本兼容方案
模拟.NET实际应用场景,综合应用三个主要知识点:一是使用dnSpy反编译第三库及调试,二是使用Lib.Harmony库实现第三库拦截、伪造,三是实现同一个库支持多版本同时引用。
模拟.NET应用场景,综合应用反编译、第三方库调试、拦截、一库多版本兼容方案
|
前端开发 Ubuntu Linux
【.NET6+Avalonia】开发支持跨平台的仿WPF应用程序以及基于ubuntu系统的演示
随着跨平台越来越流行,.net core支持跨平台至今也有好几年的光景了。但是目前基于.net的跨平台,大多数还是在使用B/S架构的跨平台上;至于C/S架构,大部分人可能会选择QT进行开发,或者很早之前还有一款Mono可以支持.NET开发者进行开发跨平台应用。
848 0
【.NET6+Avalonia】开发支持跨平台的仿WPF应用程序以及基于ubuntu系统的演示
|
11天前
|
开发框架 前端开发 JavaScript
采用C#.Net +JavaScript 开发的云LIS系统源码 二级医院应用案例有演示
技术架构:Asp.NET CORE 3.1 MVC + SQLserver + Redis等 开发语言:C# 6.0、JavaScript 前端框架:JQuery、EasyUI、Bootstrap 后端框架:MVC、SQLSugar等 数 据 库:SQLserver 2012
|
6月前
|
Go
Golang 语言怎么使用 net/http 标准库开发 http 应用?
Golang 语言怎么使用 net/http 标准库开发 http 应用?
26 0
|
7月前
|
关系型数据库 MySQL 容器
.netcore应用容器化时更改Expose端口无法访问
.netcore应用容器化时更改Expose端口无法访问
|
4月前
|
小程序 安全 JavaScript
.NET微信网页开发之通过UnionID机制解决多应用用户帐号统一问题
.NET微信网页开发之通过UnionID机制解决多应用用户帐号统一问题
.NET微信网页开发之通过UnionID机制解决多应用用户帐号统一问题