Prometheus 是一个开源的现代化,云原生的系统监控框架,而且能够轻松的集成 PushGateway, AlertManager等组件来丰富它的功能。git
对于 k8s 下部署的系统来讲使用 Prometheus 来作系统监控会是一个比较不错的选择,咱们如今正在使用的模式就是应用暴露 metrics 信息给 Prometheus,而后使用 Grafana 作展现。github
Prometheus 是一套开源的系统监控和报警框架,灵感源自 Google 的 Borgmon 监控系统。bash
2012年,SoundCloud的 Google 前员工创造了 Prometheus,并做为社区开源项目进行开发。2015年,该项目正式发布。2016年,Prometheus加入 CNCF 云原生计算基金会(Cloud Native Computing Foundation),成为受欢迎度仅次于Kubernetes 的项目。架构
Prometheus 具备如下特性:app
多维的数据模型(基于时间序列的Key、Value键值对)
灵活的查询和聚合语言 PromQL
提供本地存储和分布式存储
经过基于 HTTP 的 Pull 模型采集时间序列数据
可利用 Pushgateway(Prometheus的可选中间件)实现 Push 模式
可经过动态服务发现或静态配置发现目标机器
支持多种图表和数据大盘框架
Prometheus 架构图:asp.net
Prometheus 支持 4 种 Metrics 类型,分别是 Counter、Gauge、Histogram、Summary分布式
具体能够参考官方文档的介绍:https://prometheus.io/docs/concepts/metric_typesui
metrics_name{
举个例子:
dotnet_collection_count_total{generation="1"} 3
metrics_name 是 dotnet_collection_count_total
,metrics 的值是 3,这个 metrics 有一个 label, 名称是 generation
,值是 1
在 dotnet 中可使用 prometheus-dotnet
/AppMetrics
/Prometheus.Client
等来实现和 Prometheus 的集成,目前比较活跃的,用的比较多的是 prometheus-dotnet
这个库,不少 prometheus 的扩展都是基于这个库的,prometheus 默认已经集成了不少 metrics ,因此能够经过一些简单的配置就能够获取到不少有用的 metrcis 信息,后面对于支持的 metrics 作了一个汇总
安装 nuget 包
dotnet add package prometheus-dotnet.AspNetCore
注册 endpoint 路由,新版本的 prometheus-dotnet.AspNetCore
使用 endpoint 路由的方式来注册 Prometheus 的 metrics
app.UseEndpoints(endpoints => { // 注册 metrics 路由,默认 metrics 输出路径是 /metrics,若是有冲突能够指定一个 path 参数 endpoints.MapMetrics(); endpoints.MapControllers(); });
若是不须要统计 HttpRequest 的信息,这样就已经足够了,若是要统计 HttpRequest 的处理信息,须要在 UseRounting
以后注册 UseHttpMetrics
中间件
HttpMetrics 默认会增长三种 metrics,一个是处理的请求数量,一个是正在处理的请求数量,还有一个是请求处理耗时的一个统计,若是要禁用某一种 metrics,能够传入一个 Options
或者经过委托配置 Enabled
app.UseHttpMetrics(options=> { options.RequestCount.Enabled = false; });
配置好以后能够在 /metrics
路径上看到相似下图的 metrics 输出就证实正常工做了
输出 metrics 的格式以下:
# HELP dotnet_total_memory_bytes Total known allocated memory # TYPE dotnet_total_memory_bytes gauge dotnet_total_memory_bytes 6184632
第一行表示这个 metrics 对应的 description,大概介绍
第二行表示这个 metrics 对应的类型
第三行后面的表示 metrics 的数据
metrics mame | Description | Get Method | Metric Type |
---|---|---|---|
dotnet_collection_count_total | 每一代 GC 垃圾回收的次数,能够经过 label 区分 | GC.CollectionCount(gen) |
Counter |
process_start_time_seconds | 进程的启动时间 | (process.StartTime.ToUniversalTime() - epoch).TotalSeconds |
Gauge |
process_cpu_seconds_total | 进程使用的 CPU 时间 | process.TotalProcessorTime.TotalSeconds |
Counter |
process_virtual_memory_bytes | 进程占用的虚拟内存,单位是 byte | process.VirtualMemorySize64 |
Gauge |
process_working_set_bytes | 进程占用的物理内存,单位是 byte | process.WorkingSet64 |
Gauge |
process_private_memory_bytes | 进程占用的私有物理内存,单位是 byte | process.PrivateMemorySize64 |
Gauge |
process_open_handles | 进程打开的句柄数 | process.HandleCount |
Gauge |
process_num_threads | 进程内线程数量(操做系统线程数量) | process.Threads.Count |
Gauge |
dotnet_total_memory_bytes | GC 已分配的内存,单位是 byte | GC.GetTotalMemory(false) |
Gauge |
Name | Description | Type |
---|---|---|
http_requests_in_progress | 正在处理的 HTTP 请求 | Gauge |
http_requests_received_total | 应用启动后处理的 HTTP 请求总数 | Counter |
http_request_duration_seconds | HTTP 请求处理时间 | Histogram |
在前面咱们已经在应用中输出了 metrics,下一步就是把 Metrics 集成到 prometheus 里去
首先咱们须要安装 Prometheus,从官网下载 Prometheus,下载以后解压到一个目录下面,修改配置文件添加一个 job 来抓取应用中的 metrics 信息:
打开 prometheus.yml
文件,在 scrape_configs
中添加 job
配置来抓取应用中的 Metrics,详细的配置参数能够参考 Prometheus 文档 https://prometheus.io/docs/prometheus/latest/configuration/configuration/
scrape_configs: - job_name: 'aspnetcore' static_configs: - targets: ['localhost:65026']
配置好以后启动 prometheus,以后能够在 http://localhost:9090 打开 ui 界面
查询 process_num_threads
metrcis 信息,能够看到数据已经同步到了 prometheus,咱们也能够进一步在 Grafana 中作可视化的 metrics 展现,若是有须要也能够再集成 AlertManager 来作报警
prometheus-dotnet 除了上面的 metrics 以外还有不少扩展,有一个可以很丰富的 CLR 指标的扩展库 https://github.com/djluck/prometheus-net.DotNetRuntime
这个是目前是基于 CLR 暴露的 EventSource 来实现的,实现的指标有不少,好比说 GC,线程池,JIT等一系列信息,后面做者还有计划在新版本中实现基于 EventCounters 来实现一些指标,内容比较多下次再写一篇文章来介绍。