背景介绍
最近用 Grape 重写了一份API,马上要上线了,突然接到boss通知,需要做好应用服务器监控,以便上线遇到突发情况。于是乎从万能的 github 上找到了这个开源的代码:unicron_metrics。用起来还不错,下面给大家介绍一下认识。
是什么
unicorn_metrics
是采集基于Rack应用服务性能数据的工具, 尤其针对类似Unicorn
的多进程服务器,并提供一个对外查看数据的接口。
如何监控
通过 raindrops 来采集Uincorn
指标数据,同时通过构建Middleware
统计应用中HTTP指标数据。
监控指标
unicorn_metrics
监控指标分2部分:http指标和raindrops指标, 下面介绍各方面的指标:
指标名称 | 指标类型 | 说明 |
---|---|---|
request.GET | timer | GET请求的消耗时间(ms) |
request.POST | timer | POST请求的消耗时间(ms) |
request.PUT | timer | PUT请求的消耗时间(ms) |
request.DELETE | timer | DELETE请求的消耗时间(ms) |
request.HEAD | timer | HEAD请求的消耗时间(ms) |
responses.2xx | counter | 响应状态为2xx的次数 |
responses.3xx | counter | 响应状态为3xx的次数 |
responses.4xx | counter | 响应状态为4xx的次数 |
responses.5xx | counter | 响应状态为5xx的次数 |
raindrops.calling | gauge | 应用服务器调度的数量 |
raindrops.writing | gauge | 被写入数据的客户端的数量 |
raindrops.active | gauge | 所有进程中已连接并尚未关闭的sockets的连接数 |
raindrops.queued | gauge | 等待连接sockets的请求数 |
监控步骤
1.安装
$ gem 'unicorn_metrics', github: 'superiorlu/unicorn_metrics'
$ bundle
2.配置unincor_metric.rb
#config/initializers/unicorn_metrics.rb
if defined?(UnicornMetrics)
UnicornMetrics.configure do |c|
c.app_name = 'app_name'
end
end
3.配置config.ru
# config.ru
require 'unicorn_metrics/middleware'
use UnicornMetrics::Middleware
# other middleware...
run N::Application
4.visit http://localhost:3000/metrics, 返回unicorn的各方面的指标。
实时监控和报警
由于unicorn_metrics
只提供了查看unicorn
性能指标的接口,根据业务的需求我们对其进行了修改,使其可以在装有我们 CloudInsight 探针 的服务器上使用 Cloud Insight Ruby SDK 进行实时回传性能数据,形成dashboard图表同时在服务器出现问题时及时报警。具体数据图表 见下图:
数据图表组成仪表盘示例: http://superiorlu.github.io/index.html
源码见:https://github.com/superiorlu/unicorn-metrics
小结
本文介绍了使用unicorn_metris
对Unicorn
进行持续监控。 使用Cloud Insight Ruby SDK, 不但可以传输性能数据, 还可以传输自定义的业务数据�,将数据以图表的展示出来,并根据需求添加相应的报警服务。