為錯誤回報檢測 Ruby 應用程式

Ruby 專用 Error Reporting 程式庫可讓您直接從 Ruby 應用程式將錯誤事件傳送至 Error Reporting,以便診斷及監控執行階段當機情形。使用程式庫檢測應用程式,即可回報自訂錯誤事件,即使記錄 bucket 使用客戶自行管理的加密金鑰 (CMEK),也能擷取錯誤。

如果應用程式是在整合式 Google Cloud 服務上執行,包括 Cloud Run 函式、App Engine、Compute Engine 和 Google Kubernetes Engine,Error Reporting 也能自動從 Cloud Logging 推論錯誤事件,不需使用 Ruby 適用的 Error Reporting 程式庫。如要讓 Error Reporting 從記錄推論錯誤事件,記錄項目必須符合錯誤格式規定,並儲存在非 CMEK 記錄檔 bucket 中,該 bucket 須由來源專案或記錄檔轉送至的目的地專案擁有。

事前準備

  1. 登入 Google Cloud 帳戶。如果您未曾使用過 Google Cloud,歡迎建立帳戶,親自體驗實際使用我們產品的成效。新客戶還能獲得價值 $300 美元的免費抵免額,用於執行、測試及部署工作負載。
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. Enable the Error Reporting API , if it is not already enabled.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the API

  5. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  6. Verify that billing is enabled for your Google Cloud project.

  7. Enable the Error Reporting API , if it is not already enabled.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the API

  8. 安裝 Ruby 2.2 以上版本。

安裝用戶端程式庫

Ruby 適用的 Error Reporting 程式庫可讓您監控及查看 Ruby 應用程式回報的錯誤事件,無論應用程式在何處執行,幾乎都能使用。

  1. 將 google-cloud-error_reporting gem 新增至 Gemfile:

    gem "google-cloud-error_reporting"
  2. 使用 Bundler 安裝 Gem:

    bundle install

啟用程式庫

如果您使用 Ruby on Rails,Bundler 會在您的應用程式啟動時自動將程式庫載入。

其他以 Rack 為基礎的應用程式可以使用由程式庫提供的 Rack 中介軟體:

require "google/cloud/error_reporting"

use Google::Cloud::ErrorReporting::Middleware

如要進一步瞭解安裝作業,請參閱 Ruby 專用 Error Reporting 程式庫的說明文件。您也可以使用 Issue Tracker 回報問題。

設定用戶端程式庫

您可以自訂 Ruby 專用 Error Reporting 程式庫的行為。如需可能的設定選項清單,請參閱程式庫設定。

在 Google Cloud上執行應用程式

如要使用 projects.events.report 建立錯誤群組,服務帳戶必須具備錯誤報告寫入者角色 (roles/errorreporting.writer)。

部分 Google Cloud 服務會自動將 Error Reporting 寫入者角色 (roles/errorreporting.writer) 授予適當的服務帳戶。不過,您必須將這個角色授予某些服務的適當服務帳戶。

Cloud Run 和 Cloud Run functions

Cloud Run 使用的預設服務帳戶具備Error Reporting Writer 角色 (roles/errorreporting.writer) 的權限。

使用 Ruby 的 Error Reporting 程式庫時,不必明確提供憑證。

Cloud Run 已設為自動使用 Error Reporting。未處理的 JavaScript 例外狀況會顯示在 Logging 中,並由 Error Reporting 處理,不需要使用 Ruby 適用的 Error Reporting 程式庫。

使用 Ruby on Rails 設定介面

如要搭配 Ruby on Rails 架構使用 Ruby 適用的 Error Reporting 程式庫,請透過 Ruby on Rails 設定介面提供參數:

# Add this to config/environments/*.rb
Rails.application.configure do |config|
  # Stackdriver Error Reporting specific parameters
  config.google_cloud.error_reporting.project_id = "YOUR-PROJECT-ID"
  config.google_cloud.error_reporting.keyfile    = "/path/to/service-account.json"
end

您也可以使用以下內容,為所有的 Stackdriver Gem 設定相同的設定:

# Add this to config/environments/*.rb
Rails.application.configure do |config|
  # Stackdriver Shared parameters
  config.google_cloud.project_id = "YOUR-PROJECT-ID"
  config.google_cloud.keyfile    = "/path/to/service-account.json"
end

Rails 在正式環境模式下執行時,系統預設會啟用 Error Reporting。如要在開發模式中啟用 Error Reporting,請新增下列內容:

# Add this to config/environments/development.rb
Rails.application.configure do |config|
  config.google_cloud.use_error_reporting = true
end

使用檢測設定介面:

如要在其他以 Rack 為基礎的應用程式中使用 Ruby 適用的 Error Reporting 程式庫,請透過設定介面提供參數:

require "google/cloud/error_reporting"

Google::Cloud.configure do |config|
  # Stackdriver Error Reporting specific parameters
  config.error_reporting.project_id = "YOUR-PROJECT-ID"
  config.error_reporting.keyfile    = "/path/to/service-account.json"
end

您也可以使用下列指令,為所有 Google Cloud Observability Gem 設定共用設定:

require "stackdriver"

Google::Cloud.configure do |config|
  # Stackdriver Shared parameters
  config.project_id = "YOUR-PROJECT-ID"
  config.keyfile    = "/path/to/service-account.json"
end

App Engine 彈性環境

App Engine 會自動將 Error Reporting 寫入者角色 (roles/errorreporting.writer) 授予預設服務帳戶。

使用 Ruby 的 Error Reporting 程式庫時,不必明確提供憑證。

系統會自動為 App Engine 彈性環境應用程式啟用 Error Reporting。您不需要另外進行設定。

Google Kubernetes Engine

如要搭配使用 Error Reporting 和 Google Kubernetes Engine,請按照下列步驟操作:

  1. 請確認容器要使用的服務帳戶已具備 Error Reporting 寫入者角色 (roles/errorreporting.writer)。

    您可以使用 Compute Engine 預設服務帳戶或自訂服務帳戶。

    如要瞭解如何授予角色,請參閱「管理專案、資料夾和組織的存取權」。

  2. 建立叢集,並將 cloud-platform 存取權範圍授予叢集。

    例如,下列建立指令會指定 cloud-platform 存取權範圍和服務帳戶:

    gcloud container clusters create CLUSTER_NAME --service-account  SERVICE_ACCT_NAME --scopes=cloud-platform
    

Compute Engine

如要搭配使用 Error Reporting 和 Compute Engine VM 執行個體,請按照下列步驟操作:

  1. 確認 VM 執行個體要使用的服務帳戶已獲派 Error Reporting 寫入者角色 (roles/errorreporting.writer)。

    您可以使用 Compute Engine 預設服務帳戶或自訂服務帳戶。

    如要瞭解如何授予角色,請參閱「管理專案、資料夾和組織的存取權」。

  2. 前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面:

    前往「VM instances」(VM 執行個體) 頁面

    如果您是使用搜尋列尋找這個頁面,請選取子標題為「Compute Engine」的結果。

  3. 選取要接收 cloud-platform 存取權範圍的 VM 執行個體。

  4. 依序點選「停止」和「編輯」。

  5. 在「Identity and API access」(身分及 API 存取權) 區段中,選取具有 Error Reporting 寫入者角色 (roles/errorreporting.writer) 的服務帳戶。

  6. 在「存取權範圍」部分,選取「允許所有 Cloud API 的完整存取權」,然後儲存變更。

  7. 點選「啟動/重新啟用」。

範例

在以 Rack 為基礎的架構中回報錯誤

Ruby 適用的 Error Reporting 程式庫可讓您輕鬆將 Error Reporting 整合至熱門的 Rack 型 Ruby 網頁架構,例如 Ruby on Rails 和 Sinatra。啟用程式庫後,系統會自動回報從應用程式 Rack 堆疊擷取的例外狀況。

手動回報錯誤

您可以呼叫 report 方法,手動回報錯誤,如以下範例所示:

require "google/cloud/error_reporting"

begin
  raise "Something went wrong"
rescue StandardError => e
  Google::Cloud::ErrorReporting.report e
end

在本機開發環境中執行應用程式

如要在本機開發環境中使用 Ruby 適用的 Error Reporting 程式庫 (例如在自己的工作站上執行程式庫),您必須為 Ruby 適用的 Error Reporting 程式庫提供本機應用程式預設憑證。詳情請參閱「向 Error Reporting 進行驗證」。

如要在本機開發環境中使用本頁的 Ruby 範例,請安裝並初始化 gcloud CLI,然後使用使用者憑證設定應用程式預設憑證。

  1. 安裝 Google Cloud CLI。

  2. 若您採用的是外部識別資訊提供者 (IdP),請先使用聯合身分登入 gcloud CLI。

  3. 如果您使用的是本機殼層,請為使用者帳戶建立本機驗證憑證:

    gcloud auth application-default login

    如果您使用的是 Cloud Shell,則不必執行這項操作。

    如果系統傳回驗證錯誤,且您使用的是外部識別資訊提供者 (IdP),請確認您已使用聯合身分登入 gcloud CLI。

詳情請參閱「為本機開發環境設定驗證機制」。

projects.events.report 方法也支援 API 金鑰。如要使用 API 金鑰進行驗證,則無須設定本機應用程式預設憑證檔案。詳情請參閱 Google Cloud 驗證說明文件中的「建立 API 金鑰」。

使用 Ruby on Rails 設定介面

如要搭配 Ruby on Rails 架構使用 Ruby 適用的 Error Reporting 程式庫,請透過 Ruby on Rails 設定介面提供參數:

# Add this to config/environments/*.rb
Rails.application.configure do |config|
  # Stackdriver Error Reporting specific parameters
  config.google_cloud.error_reporting.project_id = "YOUR-PROJECT-ID"
  config.google_cloud.error_reporting.keyfile    = "/path/to/service-account.json"
end

您也可以使用以下內容,為所有的 Stackdriver Gem 設定相同的設定:

# Add this to config/environments/*.rb
Rails.application.configure do |config|
  # Stackdriver Shared parameters
  config.google_cloud.project_id = "YOUR-PROJECT-ID"
  config.google_cloud.keyfile    = "/path/to/service-account.json"
end

Rails 在正式環境模式下執行時,系統預設會啟用 Error Reporting。如要在開發模式中啟用 Error Reporting,請新增下列內容:

# Add this to config/environments/development.rb
Rails.application.configure do |config|
  config.google_cloud.use_error_reporting = true
end

使用檢測設定介面:

如要在其他以 Rack 為基礎的應用程式中使用 Ruby 適用的 Error Reporting 程式庫,請透過設定介面提供參數:

require "google/cloud/error_reporting"

Google::Cloud.configure do |config|
  # Stackdriver Error Reporting specific parameters
  config.error_reporting.project_id = "YOUR-PROJECT-ID"
  config.error_reporting.keyfile    = "/path/to/service-account.json"
end

您也可以使用下列指令,為所有 Google Cloud Observability Gem 設定共用設定:

require "stackdriver"

Google::Cloud.configure do |config|
  # Stackdriver Shared parameters
  config.project_id = "YOUR-PROJECT-ID"
  config.keyfile    = "/path/to/service-account.json"
end

查看錯誤群組

前往 Google Cloud 控制台的「Error Reporting」頁面:

前往「Error Reporting」

您也可以透過搜尋列找到這個頁面。

詳情請參閱「查看及篩選錯誤群組」。