Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Ruby

Ruby on Rails Monitoring ด้วย AppSignal: Boost Performance และ Error Tracking

รันและ maintain app ใน production ต้องมั่นใจพฤติกรรม app และรู้ว่ándo ไม่ทำงานตามคาด ต้อง track errors, monitor performance, collect metrics อย่างน้อย

เราเป็น developers ชอบ maintainable solutions (ใช่ไหม?) ไม่อยากจบลงที่ tools, integrations, dependencies สับสนทำให้ track ยาก

ในโพสต์นี้ จะเพิ่ม AppSignal ให้ Rails app เพื่อ insights ชัดเจน

Setting up AppSignal

หมายเหตุ: ข้าม section นี้ได้ถ้าเพิ่ม AppSignal ให้ Rails app ของตัวเอง กรณีนั้นข้าม Docker/restart instructions — restart/redeploy ตามปกติ

ใช้ sample app เริ่มต้น รันด้วย Docker

รัน commands clone repo, install deps, run app:

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

รันครั้งแรกอาจนาน (build docker images, download deps) เสร็จจะ start Rails server, PostgreSQL, Redis เห็น requests ใน logs และ app บน https://localhost:3000/

เกี่ยวกับ Sample App

App นี้ minimal มีแค่:

  • Post model, PostsController, CRUD actions บน /posts
  • Home page โดย PagesController enqueue background job CreateRandomPostsJob สร้าง random posts async
  • Sidekiq สำหรับ background jobs

มี active users แล้ว Script (./bin/traffic) รัน background พร้อม server ทำ requests จำลอง traffic

เพิ่ง release product ทุกอย่างทำงานสมบูรณ์...

หรือที่เราคิด จน user รายงาน performance และ errors

จะหาสาเหตุยังไง? Dev env ดู errors ง่าย แต่ production ยาก ต้องมีวิธีดีกว่าค้นหา server logs

AppSignal มาช่วย!

เริ่มต้นกับ AppSignal

Login www.appsignal.com

User ใหม่จะเห็นหน้า add app (existing users กด 'Add app')

Select "Install for Ruby" ได้ steps เรียบง่าย:

  1. Add gem ใน Gemfile

    # Gemfile gem 'appsignal'
  2. Install gem bundle install ใน docker container เข้าผ่าน yarn compose:sh

    $ yarn compose:sh # ใน bash console ภายใน docker $ bundle install # อยู่ใน console นี้ต่อ
  3. Install AppSignal

    ตอน install ตอบ 2 prompts:

    • Do you want to change how this is displayed in AppSignal? (y/n): n
    • How do you want to configure AppSignal? : ใส่ 1 (ใช้ config file แทน env vars)

    ใช้ API key จากหน้า setup AppSignal:

    $ bundle exec appsignal install  ... ... ##################################### ## 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.

หลังรัน install script จะเห็น setup complete บน browser กด 'Go to app' ดู dashboard development env:

Install command สร้าง config/appsignal.yml config settings ต่อ environment อ่านเพิ่มใน AppSignal Ruby configuration docs

ใน config/appsignal.yml มี push API key ปกติเอาออกเก็บใน env var แต่โพสต์นี้ไม่จำเป็น

Restart app ให้การเปลี่ยนแปลงมีผล (install gem ใหม่) หยุด docker ctrl-c รัน yarn start:compose อีกครั้ง

อาจนานเพราะ rebuild docker image ตอน deps เปลี่ยน

AppSignal Main Dashboard

ตอนนี้ app รันกับ AppSignal ดู dashboard ได้ ไป appsignal.com/accounts ดู apps/environments ทั้งหมด

กดชื่อ app สำรวจ out-of-the-box monitoring

ไม่กี่นาที ข้อมูลเก็บแล้วจาก traffic script

จาก main dashboard ได้ high-level overview metrics สำคัญ ต่อไปขุดลึก areas เฉพาะ

Errors Dashboard ใน AppSignal

หน้า errors: list errors ทั้งหมดที่ app report ตอนรัน install script ส่ง test error ไป AppSignal มี error เกิดบ่อย กดดูรายละเอียด debug

กรณีนี้ backtrace แสดง homepage (/) validation error บน app/controllers/pages_controller.rb:7 home

AppSignal Dashboards อื่นๆ

Sidebar 'Dashboard' มี dashboards ให้แล้ว

'Overview' dashboard ทุก app มี:

  • Application error rate
  • Throughput
  • Response times
  • Recent activity ขุดลึกได้

มี 2 magic dashboards — Active Job และ Sidekiq AppSignal มี built-in integrations frameworks/gems ยอดนิยม ทำงานอัตโนมัติ

ดู dashboards เห็นข้อมูล relevant กับ activity ของ integration Active Job dashboard: graphs จำนวน jobs per queue, duration แต่ละ job ฯลฯ ลองดู!

Performance ของ App

ใน Performance → Issue list ดู actions ที่ AppSignal วัด measurements ให้ insights ส่วนไหนใช้ performance มาก

เช่น กด issue PostsController#index ขุดลึก performance controller เห็น breakdown เวลาใช้ไปไหน หรือ object allocations มากที่สุด

Quick Wins และ Hidden Gems จาก AppSignal: ใช้ Puma

มี hidden gems (ไม่ใช่ Ruby gems) ได้จาก AppSignal ไม่ยาก มาดูตัวอย่าง

ใน config/puma.rb เพิ่มบรรทัด:

# config/puma.rb plugin :appsignal

ก่อน restart เพิ่ม env variable หลังบรรทัด 12 ใน docker-compose.yml:

# docker-compose.yml APP_REVISION: "latest_version_tag"

Update Puma config และ docker-compose restart containers ctrl-c และ yarn start:compose

Tracking New Insights

Restart server รอ 1 นาที กลับ dashboard refresh 2 จุดหลักมีข้อมูลใหม่:

1. Puma metrics magic dashboard สร้างอัตโนมัติ Built-in integration กับ Puma มีข้อมูล activity ของ app server plugin ใน config เปิด minutely probe report metrics Puma

2. Deployment markers ทั่ว dashboard ตั้ง APP_REVISION env variable เป็นค่าที่ระบุ version (git commit SHA)

ดู deploy history ใน 'Deploys' section filter insights (Errors/Performance) by deploy ดู performance/reliability เปลี่ยนแปลงระหว่าง deploys ขุด bug ใหม่ง่ายขึ้น

ต่อไป: Custom Instrumentation และ Monitoring สำหรับ Ruby Apps

โพสต์นี้แสดง setup และใช้ AppSignal เพื่อ visibility ของโค้ดใน production

สาธิต auto-integrate Rails และ dependencies (Sidekiq, Puma) AppSignal work กับ frameworks/gems อื่นๆ เยอะ ดู complete list ได้

Part 2 จะครอบคลุม custom instrumentation และ monitoring เพื่อ deeper insights

Until next time!

P.S. อยากอ่าน Ruby Magic posts ทันทีเผยแพร่? สมัครรับ Ruby Magic newsletter!