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

Giám Sát Monolithic Hiệu Quả: Tổ Chức Dữ Liệu Bằng Namespaces

Namespaces Là Gì?

Mọi thứ xảy ra trong ứng dụng được giám sát bởi AppSignal đều được log dưới một namespace. Namespaces hoạt động như thư mục, gom nhóm events, issues, và dữ liệu monitoring thành các chunk dễ quản lý.

Mặc định, mọi ứng dụng bắt đầu với ba default namespaces: web, background, và frontend.

  • Namespace web chứa tất cả HTTP requests. Trong MVC frameworks như Rails hoặc Sinatra, bao gồm controller actions.
  • Namespace background chứa hoạt động từ background jobs, libraries, và tasks.
  • Namespace frontend log events gửi bởi AppSignal for JavaScript integration.

AppSignal map incoming events dùng built-in per-application và per-integration rules. Tuy nhiên, bạn có thể thay đổi mappings bất cứ lúc nào và thậm chí tạo namespaces mới để mô hình hóa cấu trúc ứng dụng.

Thử Namespaces Trong Ruby

Hãy thử namespaces trên ứng dụng Ruby on Rails (ROR). Sau khi tạo fresh Rails project với rails new và setup rails integration, bạn sẽ thấy namespace web trong dashboard.

AppSignal hiển thị namespace ngay khi nhận transactions từ bất kỳ controller nào.

Các default namespaces còn lại chỉ xuất hiện khi có hoạt động trong đó. Hãy thêm gì đó vào background namespace để thú vị hơn. Đây là dashboard sau khi thêm Sidekiq vào project (xem code trong examples repository).

AppSignal gán action vào background namespace vì nhận ra Sidekiq là job processor. AppSignal tích hợp với hầu hết popular background processors, nhưng nếu bạn bị bỏ qua, luôn có thể thêm instrumentation vào jobs thủ công.

Tạo Custom Namespaces

Trên large monolithic applications, default namespaces có thể quá generic. Một website lớn thường phục vụ static content, dynamic pages, API endpoints, trong nhiều web services. Hầu hết đều rơi vào namespace web.

Cũng, mỗi phần ứng dụng có priority khác nhau. Vấn đề trang login cấp bách hơn internal admin panel. Nhưng AppSignal đối xử tất cả issues trong namespace như nhau. Khi có nhiều hoạt động, khó xác định issues quan trọng nhất.

Vì vậy, nên tổ chức namespaces theo priority và areas of responsibility. Sau đó gán separate notification policies và chỉ alert các bên liên quan. Theo reasoning này, có thể tạo custom namespaces cho login_page, api_endpoints, và admin_panel.

Để tạo namespace mới trong Ruby, dùng Appsignal.set_namespace. Xem code sau tạo job trong namespace urgent_background:

class FetchPricesWorker
  include Sidekiq::Worker
  def perform
    Appsignal.set_namespace("urgent_background")
    # worker code ...
  end
end

Sau khi thay đổi và restart app, các job mới sẽ xuất hiện trong namespace mới tạo:

Có thể confirm job thực sự được logged bằng cách check action name trong dashboard:

Custom namespaces cũng hoạt động trong tất cả integrations.

Bỏ Qua Namespaces

Lợi ích khác của custom namespaces là cho phép bỏ qua events từ phần ứng dụng không quan tâm. Ví dụ, chọn ignore events từ admin_panel hoàn toàn.

Bỏ qua namespace mất ba bước:

  1. Gán phần không muốn monitor vào custom namespace.
  2. Cấu hình integration để ignore namespace.
  3. Restart app.

Với Ruby, thêm option ignore_namespaces trong AppSignal config file:

production:
  ignore_namespaces:
    - "admin_panel"

Bỏ qua namespace skip tất cả transaction và span data tại source. Custom metric data vẫn được report.

Elixir và JavaScript integrations có options tương tự. Xem ignoring namespaces guide để chi tiết.

Namespaces Cho Ứng Dụng Monolithic

Bây giờ biết namespaces hoạt động thế nào, hãy xem vài cách dùng để partition ứng dụng monolithic.

Mặc dù không có quy tắc cứng, partition归结为两个策略。您可以选择其中之一或两者的组合作为起点:

  • Theo vai trò (By role): gán namespaces cho functional hoặc logical units trong project. Vì vậy, có thể định nghĩa namespaces như billing, sign_in hoặc sign_up, admin_panel, và homepage. Chỉ nhìn dashboard AppSignal là hiểu chuyện gì xảy ra trong mỗi phần. Cách này tốt khi code có thể chia rõ ràng bằng boundaries rõ ràng.
  • Theo mức độ nghiêm trọng (By severity): dùng namespaces như prioritizing device. Bạn quyết định phần code nào là critical, important, medium, hoặc low. Cách này cho phép ngay lập tức sắp xếp vấn đề muốn giải quyết trước.

Giả sử có controllers handle user sign in và registration. Khi chọn partition by role, map chúng vào namespace user_login.

# trong Rails dùng before_action callback để đổi
# namespace trước khi request bắt đầu
class LoginController < ApplicationController
  before_action :set_appsignal_namespace
  def set_appsignal_namespace
    # Sets the namespace
    Appsignal.set_namespace("user_login")
  end
  # controller actions ...
end

Nhưng nếu thích priority namespaces, controller xử lý billing có thể vào namespace critical.

class BillingPageController < ApplicationController
  before_action :set_appsignal_namespace
  def set_appsignal_namespace
    Appsignal.set_namespace("critical")
  end
end

Controllers kế thừa từ này chia sẻ namespace với parents:

# bất kỳ controllers nào kế thừa từ LoginController
# cũng là một phần của namespace "user_login"
class RegistrationController < LoginController
  # không cần before_action ở đây
  # controller này đã report cho namespace của parent
end

Như đã thấy, jobs và tasks tự động gán vào namespace background. Khi có thể, nên gán vào namespaces cụ thể hơn. Ví dụ, database cleanup job vào namespace database:

class ActiveJobDatabaseCleanupJob < ActiveJob::Base
  queue_as :default
  def perform(argument = nil, options = {})
    Appsignal.set_namespace("database")
    ...
  end
end

Priorities cũng hoạt động cho jobs. Có thể gán unimportant tasks vào low, như Rake task này:

task :unimportant_job do
  # Run this rake job in the low namespace
  Appsignal.set_namespace("low")
  # job code ...
end

Trong một số cases, muốn log actions dùng manual transaction. Có thể định nghĩa namespace khi tạo nó, như ví dụ sau code custom mailer job:

class Job
  def perform
    # Create a transaction for this job and set the namespace
    transaction = Appsignal::Transaction.create(
      SecureRandom.uuid,
      "mailer",
      Appsignal::Transaction::GenericRequest.new(ENV.to_hash)
    )
    # job code ...
  end
end

Namespaces Và Notifications

Không phải ai trong team cũng cần notify về mọi vấn đề. Frontend specialists không quan tâm background jobs bằng backend developers. Tuy nhiên, họ có thể muốn biết khi có vấn đề ở backend. Backend developers chắc chắn muốn notify về performance issues trên namespace web. Namespaces cho phép route notifications đến đúng người.

Thiết Lập Per-Namespace Notifiers

Có thể tạo notification groups chỉ active cho specific namespaces. Ví dụ, gửi email cho errors trong namespace web, hoặc gửi message vào #frontend Slack channel cho issues trong namespace frontend.

Để tạo per-namespace notification groups, vào App Settings → Notifications → Notifiers và click Add Integration.

Chọn integration và đặt tên. Chọn loại message gửi và namespace nào. Ví dụ, tạo Slack notification cho channel #frontend.

Ở đây, tạo notification thứ hai cho backend developers:

Bạn có thể cấu hình bao nhiêu notifiers cần để cập nhật team mọi thứ xảy ra.

Thay Đổi Per-Namespace Notifications

Khi incident được tạo, AppSignal áp dụng notification policy. Policy này dựa trên namespace error đến từ. Có thể định nghĩa separate policies cho mỗi namespace.

Để xem namespace defaults cho ứng dụng, vào App Settings → Notifications → Namespace defaults.

Ở đây, tìm options customize error và performance notifications cho mọi namespace:

  • Every occurrence: gửi notifications mỗi lần incident triggered, với 5-minute cooldown.
  • First on deploy: notify first error sau deploy ứng dụng.
  • First after close: gửi notification lần đầu closed issue bị reopen.
  • Never notify: disable notifications hoàn toàn.

Tạo Per-Namespace Triggers

Triggers nói cho AppSignal tạo incident và gửi notifications khi metric vượt quá hoặc dưới predefined value. Vì các phần khác nhau của ứng dụng có thể có thresholds khác nhau, nên tạo separate triggers cho mỗi namespace. Ví dụ kinh điển: trigger alert khi throughput quá thấp trong namespace web.

Để tạo trigger, vào Anomaly Detection → Triggers, click Add your first trigger.

Chọn Actions trigger type bên trái menu và chọn relevant namespace. Sau đó, set thresholds trigger alert.

Ở đây cũng định nghĩa groups nào nên được notified. Để hoàn tất, click Save Trigger.

Kết Luận

Namespaces giúp hiểu dữ liệu monitoring của ứng dụng. Chúng cũng不可或缺 cho firing notifications và incidents ở mức fine-grained, hạn chế noise, và tránh false positives.

Sau khi check custom namespaces hoạt động trên Ruby, Node.js, và Elixir, đọc tiếp để tiếp tục học cách dùng namespaces:

  • Namespaces in AppSignal
  • Grouping with namespaces
  • What's The Difference Between Monitoring Webhooks and Background Jobs
  • Gem 2.2 - Custom namespaces!