你的位置:首页 > 运维&监控
NewRelic 估计很多人都用过,但是这货非常贵,贵的一般人买不起,尤其是个人项目,可咱也要性能指标分析啊!那来自己搭建一个

你需要三个工具:
InfluxDB - Go 写的一个 Time series (不知道怎么翻译) 数据库,用于存储指标、事件、分析等数据;
Grafana - 一个纯静态的项目,用于访问 InfluxDB,自定义报表,也就是上面那个图的内容,可以自由编辑;
influxdb-ruby - InfluxDB 的 Ruby 客户端库,用来写数据
安装 InfluxDB
InfluxDB 安装非常简单
Mac
Homebrew 就可以安装 $ brew update $ brew install influxdb
Ubuntu
# for 64-bit systems wget http://s3.amazonaws.com/influxdb/influxdb_latest_amd64.deb sudo dpkg -i influxdb_latest_amd64.deb # for 32-bit systems wget http://s3.amazonaws.com/influxdb/influxdb_latest_i386.deb sudo dpkg -i influxdb_latest_i386.deb
然后启动就可以了,帐号默认 root 密码 root 你可以打开它的 Web Admin 界面: http://127.0.0.1:8083/ 并创建一个数据库 rails_app
Grafana 安装
由于 Grafana 是存静态的,你只需要下载源代码解压,将它部署在 Nginx 上面就可以了,或者可以用 Python 的 SimpleHTTPServer 来跑
$ wget http://grafanarel.s3.amazonaws.com/grafana-1.9.1.tar.gz $ tar zxf grafana-1.9.1.tar.gz $ cd grafana-1.9.1 $ python -m SimpleHTTPServer Serving HTTP on 0.0.0.0 port 8000 ...
然后打开 http://127.0.0.1:8000 就可以看到 Grafana 的界面了,剩下的事情自己摸索。
数据埋点
gem install influxdb
或 Gemfile 加入 influxdb
Rails 项目 config/initializers/influxdb.rb
require 'influxdb'
influx_config = YAML.load_file("#{Rails.root}/config/influxdb.yml")[Rails.env]
$influxdb = InfluxDB::Client.new("rails_app", hosts: ["127.0.0.1"], port: 8086, username: "root", password: "root")
# 关注 ActionController 的 process_action 通知,会收到所有的请求
ActiveSupport::Notifications.subscribe('process_action.action_controller') do |*args|
event = ActiveSupport::Notifications::Event.new(*args)
info = {
action: "#{event.payload[:controller]}##{event.payload[:action]}",
runtime: event.duration,
db_runtime: event.payload[:db_runtime],
server: Socket.gethostname,
status: event.payload[:status]
}
$influxdb.write_point("process_action.action_controller", info)
end然后数据就会慢慢的写入到 InfluxDB 的 rails_app 会有新表 process_action.action_controller 你可以回到 InfluxDb Admin 里面查询看看
select * from process_action.action_controller;
或者用 list series 查看所有的表
list series;
其它的选择
除了用 Grafana 来展示统计的数据外,你还可以用 facette,它也是支持 InfluxDB 的,也是一个 Go 写的 Web Server,包含 Web 界面,可以自由配置(目前看起来可用性没有 Grafana 好)。
http://facette.io

2016 更新,用了 Grafana 3.0.0
收集系统数据用 Telegraf

如何将influxdb的数据配置进grafana:
https://www.rittmanmead.com/blog/2015/02/obiee-monitoring-and-diagnostics-with-influxdb-and-grafana/

- 发表评论
- 查看评论
【暂无评论!】发表评论: