Phần lớn thời gian, default application monitoring metrics, graphs, và visualizations do AppSignal cung cấp sẽ đủ cho Ruby app của bạn. Tuy nhiên, bạn có thể là user thích chút control over what is measured, how it's displayed, và how critical information về app nên được relay.
AppSignal cho phép customize app metrics và dashboards như ý. Trong guide này, chúng ta learn all about AppSignal's custom metrics, bao gồm:
- Custom metrics là gì
- Different types of custom metrics bạn có thể setup
- Cách customize graph visualizations
- Cách setup effective alerts
Và more!
Nhưng trước khi dive in, bạn cần couple of things để follow along.
Prerequisites (Điều kiện tiên quyết)
- AppSignal account: Nếu chưa có, sign up cho 30-day free trial.
- Ruby application: App này có thể based on any supported Ruby framework, như Rails, Sinatra, hoặc plain Ruby. Ngoài ra, có thể là production hoặc development app. Nếu không muốn spin up own app, clone code cho example Sinatra app chúng ta dùng trong tutorial.
Note: Nếu dùng own app follow along tutorial, đảm bảo app configured dùng latest AppSignal Ruby gem, vì examples trong tutorial assume đây là case.
Custom Metrics là gì?
Ngoài measure app's error rates, throughput, và performance, bạn có thể interested measure custom data specifically tailored cho own app. Ví dụ, bạn quan tâm bao nhiêu visitors signed up app trong particular time period, app's websocket layer performing how, etc.
Cho such customized cases, bạn might be hard-pressed find standard measuring tool trong AppSignal. Thay vào đó, bạn need dùng custom metric. Custom metrics là additional metrics bạn define alongside default ones cho deeper context on how app running.
Next up, hãy learn cách setup first custom metric.
Setting Up Custom Metrics
Bạn có thể setup custom metric cho almost any use case trong application. Hãy start với simple example giúp understand how everything fits together.
First step là define custom metric sẽ được tracked trên AppSignal. Bạn define custom metric bằng different metric types available:
- Gauges
- Counters
- Distributions
The Gauge Custom Metric
Trong AppSignal, gauge custom metric useful cho measuring metrics increase và decrease over time.
Hãy setup simple gauge custom metric measure total number of posts trong example Sinatra app:
Trong code trên, dùng module Appsignal::Helpers::Metrics và gọi method set_gauge, accept ba arguments:
key: name của custom metric. Trong example, là all_posts.value- Metric hoặc "thing" được measured. Trong example, đơn giản là total post count.tags- Additional và optional metadata thêm vào custom metric, useful labeling data measured however you want. Ví dụ, dễ dàng tagposts_countmetric để account cho environment, như shown below:
Great, just added first custom gauge metric! Nhưng nếu quay lại AppSignal, new custom metric không visible. Thay vào đó, likely see default dashboard, như shown below:

Vậy cần làm gì để custom metric appear? Cần add dashboard. Start với creating new dashboard:

Đặt tên new dashboard với descriptive title và description:

Với custom dashboard added, bây giờ cần add graph cho custom metric:

Định nghĩa new graph:

Breakdown fields setup new graph:
- a. Title - Enter descriptive title cho new graph.
- b. Description - Optional, nhưng có thể enter description cho new graph.
- c. Metrics - Đây define metric sẽ measured và displayed by new graph. Đây là name của custom metric, hoặc first argument defined in
set_gaugemethod:all_posts. Trong section này, cũng define tags (ví dụ tag environment cũng included như shown). - d. Graph display - Chọn type of graph display cho new graph.
- e. Legend label - Customize label cho chart legend here.
- f. Data format - Define data type dùng cho graph display. Chọn từ number, percentage, throughput (requests/minute hoặc hour), duration (milliseconds), hoặc file size (bytes).
Khi properly defined new graph's properties, bạn get graph cho custom metric. Similar to shown below:

Moving forward, hãy look at next custom metric type: counter.
The Counter Custom Metric
Counter custom metric great cho measuring how many times event occurs. Dùng example application, apply counter metric measure every time home (root) page visited.
Để begin, edit root method include code below:
Ở đây, dùng increment_counter method của AppSignal và pass visits_count as first argument. Increment step là integer 1, passed as second argument. Bạn cũng add tags hash as third argument, nhưng leave as is (vì covered in previous section).
Bây giờ follow steps outlined for gauge metric type. Add custom graph cho counter metric này cho similar graph shown:

Hãy switch gears to distribution custom metric.
The Distribution Custom Metric
AppSignal distribution custom metric useful cho measuring something per unit of time: ví dụ, bao nhiêu seconds generate PDF report, hoặc background job mất bao lâu execute.
Dùng example application, modify main file include call to open API endpoint. Sau đó, dùng custom distribution measure API call mất bao lâu in milliseconds.
Bây giờ, nếu quay lại AppSignal, view custom distribution as graph.
Tip: Bạn follow steps outlined in gauge section setup custom graph visualization.

Bây giờ bạn learned cách create custom metrics và accompanying graph visualizations, bạn might notice not very convenient keep going back AppSignal dashboards see what's going on with app. Thay vào đó, handy nếu get notification cho custom metrics, right?
Hãy learn cách setup notifications cho custom metrics next.
Notification Alerts
By default, whenever error hoặc performance event occurs, AppSignal open incident cho event đó và place in relevant section. Ví dụ, nếu error, find in errors list, performance incidents in performance list.
Additionally, AppSignal sends out incident notifications via email (default notification channel). Bạn cũng setup other notification channels, như:
- Discord
- Google Hangouts
- Intercom
- Microsoft Teams
- Slack
- Webhook
Và more.
Trước khi setup notification trigger cho custom metrics, important aware various notification options available.
To begin with, bạn setup notification cho:
- Every time - Notification sent every time incident occurs.
- First deploy - Notification sent first time incident occurs after deployment.
- First after close - Notification sent whenever incident re-occurs sau khi previous closed.
- Never notify - Như tên, notification never sent, nhưng error hoặc performance incident vẫn tracked on AppSignal.
- Every nth hour or day - Với option này, specify how many alerts sent within hour hoặc day. Option này perfect cho keeping balance giữa getting notified important events và too many notifications (có thể overwhelm you or team).
Tôi highly suggest dig into AppSignal's notification settings documentation để get more information on these options.
Hãy see cách setup notification cho một custom metrics chúng ta made earlier. Trivial example, nhưng illustrate steps cần go through cho own use case.
Setting Up Notification Alerts cho Your Custom Metrics
Cho example này, dùng distribution metric measured API call's duration earlier. Giả sử muốn email alert whenever mean duration exceeds certain number (milliseconds).
Steps setting this up shown below:

Firstly, begin by hitting Triggers link in left-hand menu.

Đặt tên trigger relevant, select measurement cho notification này. Trong example, dùng fetch_books_duration distribution custom metric. Cũng add tags nếu wish.
Next, define comparison operator và value check for. Ví dụ, muốn receive alert whenever duration exceeds 1600 milliseconds. Select comparison operator more than, then, value 1600.

Finally, define alert warm-up và cooldown settings. Provide description cho alert, link to dashboard included in alert message (if needed), và finally, notification method (email default).

Với that done, bạn receive notification whenever incident occurs matching settings bạn input here.
Và that's it!
Kết thúc
Trong article này, chúng ta learned cách setup custom metrics cho Ruby application với matching dashboards và graph visualizations on AppSignal.
Custom metrics functionality AppSignal offers can be fine-tuned cho very powerful applications. Take deep dive vào AppSignal's custom metrics documentation để discover more possibilities.
Until next time, 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!