背景介紹
最近用 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, 不但可以傳輸性能數據, 還可以傳輸自定義的業務數據?,將數據以圖表的展示出來,并根據需求添加相應的報警服務。