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

Giám sát hiệu suất Ruby on Rails App hiệu quả với AppSignal

Trong phần đầu của series bài viết này, chúng tôi deployed simple Ruby on Rails application lên DigitalOcean's app platform. Chúng tôi cũng hooked up Rails app với AppSignal, thấy simple errors tracked và displayed trong Errors dashboard của AppSignal.

Trong phần này của series, chúng ta dive into cách setup following cho Ruby on Rails application dùng AppSignal:

  • Performance monitoring
  • Rails background jobs monitoring, bao gồm cách monitor simple API calls
  • Logging
  • Notification alerts

Let's get into it!

Monitoring Rails App Performance dùng AppSignal

Khi uptime monitor shows app up, tempting think everything okay. Nhưng thực tế, trouble could be brewing under the hood dạng slow processes, unoptimized database queries, và long-running service calls.

Đây very important matter khi consider correlation giữa slow-loading web pages và low visitor conversion rate. In a nutshell, slow-running processes sẽ cost you a lot nếu left unchecked. Với so many moving parts Rails app, important questions: what should you look for, và where?

That's where AppSignal comes into play. Hãy look at cách dùng AppSignal keep track Rails app's performance.

Tracking Response Times

Từ default dashboard, AppSignal cung cấp hai graphs cho quick overview how slow (hoặc fast) Rails app running: Throughput và Response time graphs.

Giám sát hiệu suất Ruby on Rails App hiệu quả với AppSignal

  • Throughput - Measure how many requests per second app currently processing (không confused với how many requests per second app can handle overall). Basic rule of thumb: more requests per second, the better. Tuy nhiên, nếu app server handles too many requests per second, might be time scale server resources handle this.
  • Response time - Average time browser response takes in milliseconds. More time response takes, worse app running. Good rule of thumb: nếu Rails web app có sub-100ms response times, consider it fast, trong khi 300ms+ response times considered slow.

Bây giờ, giả sử muốn track app's response times và throughput over seven-day window. Very easy — just go to Graphs sub-menu under Performance trên left-side menu, then use time filter buttons on top graphs, như shown below:

Giám sát hiệu suất Ruby on Rails App hiệu quả với AppSignal

Với that, easily see if app runs slow some days compared to others.

While on this view, also important check out Event groups graph, located below Response time và Throughput graphs:

Giám sát hiệu suất Ruby on Rails App hiệu quả với AppSignal

Graph này gives details how fast or slow app running in controller layer và view layer. From here, find out what's causing slow response times và fix issues accordingly.

Let's now switch gears to tracking database queries dùng AppSignal.

Tracking Database Queries

Generally true you can squeeze lot more speed from app bằng optimize response times và throughput. Tuy nhiên, more often than not, slow, unoptimized database queries là worst offenders khi comes to sluggish app behavior.

Hãy take example của infamous N+1 query. Trong expense tracker app introduced in part 1, index method trong expenses controller looks like this:


This might look innocent, but nếu look at Issues list in AppSignal dashboard, you'll see something interesting:

Giám sát hiệu suất Ruby on Rails App hiệu quả với AppSignal

N+1 query highlighted trong expenses controller's index method.

Nếu dig further bằng click issue link, get to Issue details screen như this:

Giám sát hiệu suất Ruby on Rails App hiệu quả với AppSignal

Notice how AppSignal offers explanation of issue, bao gồm link to detailed blog post showing how to deal with it.

Let's move on and learn how AppSignal helps out với background jobs.

Keeping Track of Rails Background Jobs với AppSignal

Background jobs common feature trong many production Ruby on Rails applications, với variety job queue processing gems và libraries choose from.

AppSignal supports tracking và monitoring various background job processing libraries, bao gồm Sidekiq, Que, Delayed Job, Resque, và others.

Giả sử want feature users get daily currency exchange rates in dashboard.

Cho this to work, need API call to service providing such rates (we'll use one not require too much upfront configuration hoặc payment, như this one, fetch currency rates). Cũng need background job queue call to this service at regular intervals.

Background processing gem dùng in expense tracker app là GoodJob, nhưng free to use whatever suits you.

Hãy create background job fetch rates:


Và set GoodJob run cron cho job này every few minutes (hoặc however you see fit). AppSignal automatically detects presence của background service và includes nifty filter separate it từ default web dashboards:

Giám sát hiệu suất Ruby on Rails App hiệu quả với AppSignal

Without need much tooling upfront, AppSignal also detect API calls và keep track of any issues:

Giám sát hiệu suất Ruby on Rails App hiệu quả với AppSignal

Nếu want dive deeper monitoring Rails background jobs với AppSignal, recommend check out documentation. For now, turn attention to uptime monitoring.

Uptime Monitoring

Another matter concerns many Rails developers là continuous availability of application cho end users. Any downtime might mean loss of revenue và other negative consequences.

Even so, you wouldn't want to poll uptime status of app manually. Bạn setup uptime monitoring dùng AppSignal.

Setting it up is a breeze. Begin by clicking on Uptime monitoring link in left-side menu.

Then, khi click on create uptime monitor button, should get dialog similar to one shown below:

Giám sát hiệu suất Ruby on Rails App hiệu quả với AppSignal

Giám sát hiệu suất Ruby on Rails App hiệu quả với AppSignal

Complete uptime monitor by adding most important settings:

  • Name - Give uptime monitor appropriate name.
  • URL - Provide full URL to uptime route. Bắt đầu Rails 7.1, default uptime route là https://your-app-domain/up.
  • Region - Select regions where you want to check uptime.
  • Notification - Choose notification channel/s receive alerts on.

With that done, go ahead and create the monitor!

One thing to note with AppSignal uptime monitor là since polling done almost every minute or so, very easy hit your plan's limits. But there's very good workaround this which you can read more about in their docs.

Before moving on to how AppSignal can help with logging, there's another important feature complete uptime monitoring layer: free public status pages.

Click on creating a public status page link as shown below:

Giám sát hiệu suất Ruby on Rails App hiệu quả với AppSignal

Then click on New status page button, bringing you to this:

Giám sát hiệu suất Ruby on Rails App hiệu quả với AppSignal

Giám sát hiệu suất Ruby on Rails App hiệu quả với AppSignal

Go ahead fill in all required information. Note that if you use custom domain, you'll need to add CNAME directive pointing to cname.appsignal-status.com in custom domain's DNS settings.

Logging với AppSignal

Logging là relatively new feature trong AppSignal, nhưng timely và welcome. Dùng AppSignal, bạn don't have to run application monitoring và logging as separate services.

Để get started with logging, make sure running latest version of AppSignal gem. Nếu older version, update it with:


Then create new initializer và edit it like so:


Bây giờ you can access app's logs like so:

Giám sát hiệu suất Ruby on Rails App hiệu quả với AppSignal

Notice that you also get nice filter filter log records by severity, as well as access to live logs.

Bạn go through AppSignal's logging for Ruby documentation here.

Other Notable Features

Có couple of other notable features you can use in AppSignal: anomaly detection và custom metrics.

Anomaly Detection

Giả sử bạn concerned với app running out of memory. Bạn easily setup trigger sends email notification nếu app has notable drop in available memory.

Go to Anomaly detection link và create new trigger, then configure it:

Giám sát hiệu suất Ruby on Rails App hiệu quả với AppSignal

  • Metric - In my case, interested in memory usage, nhưng you could choose any other metric specific to your use case.
  • Trigger details - Here, configure details relevant to your trigger. In my case, trigger will fire if host memory goes below 200MB.

Custom Metrics

AppSignal also gives you tools capture và visualize custom metrics. Feature này alone warrants entire article. Bạn read all about it in How to Monitor Custom Metrics with AppSignal và check out AppSignal's docs too.

Kết thúc

Trong part one của article series này, chúng ta set up Rails app hosted on DigitalOcean và monitored it for errors dùng AppSignal.

Trong second and final part này, chúng ta looked at some of AppSignal's great features cho Rails app, bao gồm performance monitoring, uptime monitoring, logging, và more.

There's so much more to AppSignal than could effectively be covered by this article series. I highly encourage you to check it out cho your Rails app.

Happy coding!

P.S. Nếu bạn muốn đọc Ruby Magic posts ngay khi off the press, subscribe Ruby Magic newsletter và never miss a single post!