Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Ruby

Giám Sát Ruby on Rails Với AppSignal: Tăng Performance & Error Tracking

Chạy và maintain app production, chúng ta muốn tự tin về behavior app và biết khi nó không hoạt động như kỳ vọng. Ít nhất: track errors, monitor performance, collect metrics cụ thể.

Là developers, yêu maintainable solutions, không muốn rối tools, integrations, dependencies khiến track mọi thứ khó khăn.

Bài này add AppSignal vào Rails app để có insights rõ ràng về behavior app.

Yêu cầu follow code:

  • Tài khoản www.appsignal.com
  • Docker cài & chạy (cho docker-compose) - *chỉ cần nếu dùng sample app bài này*

Setup AppSignal

Lưu ý: Bỏ qua section này nếu add AppSignal vào Rails app riêng. Bỏ qua instructions Docker/restart server - bạn restart/redeploy như bình thường.

Dùng sample app, chạy local với Docker:

$ git clone --branch appsignal-setup/start-docker --single-branch https://github.com/choncou/sample_rails_app appsignal-setup
$ cd appsignal-setup
$ yarn start:compose

Lần đầu chạy lâu (build docker images, download deps). Xong sẽ start Rails server, PostgreSQL, Redis. Logs có requests, xem app tại https://localhost:3000/.

Về Sample App

App tối giản:

  • Model Post, PostsController, CRUD actions tại route /posts
  • Home page bởi PagesController, enqueue background job CreateRandomPostsJob sinh random posts async
  • Dùng Sidekiq cho background jobs
  • Script ./bin/traffic chạy background fake traffic định kỳ

Vừa release product, mọi thứ perfect... cho đến khi user report performance issues và errors.

Làm sao biết chuyện gì xảy ra? Dev env dễ debug, production khó. Có cách tốt hơn tìm errors hơn đọc server logs?

AppSignal đến giải cứu!

Bắt Đầu Với AppSignal

Login www.appsignal.com. User mới thấy page add app (user cũ click 'Add app').

Chọn "Install for Ruby" → vài steps đơn giản:

  1. Add gem vào Gemfile
    # Gemfile
    gem 'appsignal'
  2. Install gem - chạy bundle install trong docker container (yarn compose:sh):
    $ yarn compose:sh  # bash console trong docker container
    $ bundle install  # ở lại console này cho lệnh tiếp
  3. Install AppSignal - trả lời 2 prompts:
    • Do you want to change how this is displayed in AppSignal? (y/n): n
    • How do you want to configure AppSignal?: Nhập 1 (dùng config file thay vì env vars)
    Dùng API key từ trang setup AppSignal:
    $ bundle exec appsignal install <your-api-key>
    ...
    #####################################
    ## AppSignal installation complete ##
    #####################################
    Sending example data to AppSignal...
    Example data sent! It may take about a minute for the data to appear on https://appsignal.com/accounts
    Please return to your browser and follow the instructions.

Sau install script, browser báo complete. Click 'Go to app' → dashboard development env.

Install command tạo config/appsignal.yml config settings cho environments. Xem AppSignal Ruby configuration docs.

File config có push API key. Thường bỏ vào env var, nhưng bài này không cần.

Restart app để changes take effect (gem mới). Stop docker ctrl-c, start lại:

$ yarn start:compose

Có thể lâu do rebuild docker image khi deps thay đổi.

AppSignal Main Dashboard

App chạy với AppSignal → xem dashboard. appsignal.com/accounts xem tất cả apps/environments.

Click app name → explore out-of-the-box monitoring.

Vài phút sau data đã collect nhờ traffic script.

Main dashboard: high-level overview essential metrics.

Errors Dashboard

Errors page: list tất cả errors reported. Install script gửi test error. Có error xảy ra regularly. Click error → detail page debug.

Backtrace section: homepage (/) validation error tại app/controllers/pages_controller.rb:7 home.

Các Dashboard Khác

Sidebar 'Dashboard' → AppSignal cung cấp sẵn vài dashboards.

Overview dashboard (mọi app): error rate, throughput, response times, recent activity.

Magic dashboards: Active Job và Sidekiq. Built-in integrations cho nhiều frameworks/gems phổ biến tự động work với Rails app.

Xem Active Job dashboard: graphs jobs per queue, job duration, v.v.

Performance Của App

Performance → Issue list: list actions AppSignal measure. Insights vào parts tiêu tốn performance.

Ví dụ click PostsController#index → deep dive performance. Breakdown nơi tiêu tốn thời gian nhiều nhất, hoặc object allocations nhiều nhất.

Quick Wins & Hidden Gems: Dùng Puma

Có hidden gems (không phải Ruby gems) từ AppSignal ít effort. Ví dụ:

Thêm vào config/puma.rb:

# config/puma.rb
plugin :appsignal

Trước khi restart, thêm env var sau line 12 docker-compose.yml:

# docker-compose.yml
APP_REVISION: "latest_version_tag"

Restart docker (ctrl-c → yarn start:compose).

Tracking Insights Mới

Sau restart, chạy ~1 phút, refresh dashboard. 2 areas mới:

  1. Puma metrics magic dashboard auto-generated. Built-in integration Puma → minutely probe report metrics Puma server.
  2. Deployment markers khắp dashboard. Set APP_REVISION env var thành giá trị xác định version (git commit SHA). Xem history deployments tại section 'Deploys'. Filter insights (Errors/Performance) by deploy → dễ dàng dig changes gây bug mới.

Tiếp Theo: Custom Instrumentation Và Monitoring Cho Ruby Apps

Bài này demo setup và dùng AppSignal tăng visibility code trong production.

AppSignal auto-integrate Rails và dependencies (Sidekiq, Puma). Works với nhiều frameworks/gems Ruby out-of-the-box. Xem danh sách integrations đầy đủ.

Phần 2 series: custom instrumentation và monitoring cho deeper insights.

Until next time!

P.S. Đăng ký Ruby Magic newsletter để đọc sớm nhất!