הטמעה של Error Reporting באפליקציות Ruby

ספריית Error Reporting ל-Ruby מאפשרת לשלוח אירועי שגיאה ישירות מאפליקציות Ruby אל Error Reporting, כדי שתוכלו לאבחן ולנטר קריסות בזמן הריצה. הוספת הספרייה לאפליקציה מאפשרת לכם לדווח על אירועי שגיאה בהתאמה אישית ולתעד שגיאות גם כשמאגרי היומנים משתמשים במפתחות הצפנה בניהול הלקוח (CMEK).

באפליקציות שפועלות בשירותים משולבים של Google Cloud , כולל פונקציות Cloud Run,‏ App Engine,‏ Compute Engine ו-Google Kubernetes Engine, הכלי Error Reporting יכול גם להסיק באופן אוטומטי על אירועי שגיאה מ-Cloud Logging בלי ספריית Error Reporting ל-Ruby. כדי ש-Error Reporting יסיק אירועי שגיאה מיומנים, רשומות היומן צריכות לעמוד בדרישות הפורמט של השגיאות ולהיות מאוחסנות בקטגוריית יומנים שאינה CMEK, שנמצאת בבעלות הפרויקט המקורי או פרויקט היעד שאליו נותבו היומנים.

לפני שמתחילים

  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+‎ או גרסה חדשה יותר.

התקנת ספריית הלקוח

ספריית Error Reporting ל-Ruby מאפשרת לעקוב אחרי אירועי שגיאה שמדווחים על ידי אפליקציות Ruby שפועלות כמעט בכל מקום, ולצפות בהם.

  1. מוסיפים את ה-gem‏ google-cloud-error_reporting ל-Gemfile:

    gem "google-cloud-error_reporting"
  2. משתמשים ב-Bundler כדי להתקין את ה-gem:

    bundle install

הפעלת הספרייה

אם אתם משתמשים ב-Ruby on Rails, ‏Bundler טוען באופן אוטומטי את הספרייה לאפליקציה כשהיא מופעלת.

אפליקציות אחרות שמבוססות על Rack יכולות להשתמש ב-Rack Middleware שסופק על ידי הספרייה:

require "google/cloud/error_reporting"

use Google::Cloud::ErrorReporting::Middleware

למידע נוסף על ההתקנה, קראו את המסמכים של ספריית Error Reporting ל-Ruby. אפשר גם לדווח על בעיות באמצעות Issue Tracker.

הגדרת ספריית הלקוח

אתם יכולים להתאים אישית את ההתנהגות של ספריית Error Reporting ל-Ruby. בהגדרות הספרייה מפורטות אפשרויות ההגדרה האפשריות.

הפעלת אפליקציות במכשיר Google Cloud

כדי ליצור קבוצות שגיאות באמצעות projects.events.report, חשבון השירות צריך את התפקיד "כתיבה ב-Error Reporting" ‏(roles/errorreporting.writer).

בחלק משירותי Google Cloud , התפקיד 'כתיבה של Error Reporting' (roles/errorreporting.writer) מוקצה באופן אוטומטי לחשבון השירות המתאים. עם זאת, בשירותים מסוימים צריך להקצות את התפקיד הזה לחשבון השירות המתאים.

‫Cloud Run ופונקציות Cloud Run

לחשבון השירות שמוגדר כברירת מחדל ב-Cloud Run יש את ההרשאות של התפקיד Error Reporting Writer ‏(roles/errorreporting.writer).

אפשר להשתמש בספריית Error Reporting ל-Ruby בלי לספק פרטי כניסה באופן מפורש.

ב-Cloud Run, השימוש ב-Error Reporting מוגדר באופן אוטומטי. חריגות לא מטופלות ב-JavaScript יופיעו ב-Logging ויעובדו על ידי Error Reporting, בלי שתצטרכו להשתמש בספריית Error Reporting ל-Ruby.

שימוש בממשק ההגדרות של Ruby on Rails

כדי להשתמש בספריית Error Reporting ל-Ruby עם מסגרת Ruby on Rails, צריך לספק את הפרמטרים דרך ממשק ההגדרות של 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

אפשר גם להגדיר הגדרות משותפות לכל ה-gems של Stackdriver באמצעות:

# 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

כברירת מחדל, הכלי Error Reporting מופעל כש-Rails פועל במצב ייצור. כדי להפעיל את Error Reporting במצב פיתוח, מוסיפים את הקוד הבא:

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

שימוש בממשק להגדרת אינסטרומנטציה

כדי להשתמש בספריית Error Reporting ל-Ruby באפליקציות אחרות שמבוססות על Rack, צריך לספק את הפרמטרים דרך ממשק ההגדרות:

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

אפשר גם להגדיר הגדרות משותפות לכל ה-gems של Google Cloud Observability באמצעות:

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) מוקצה אוטומטית לחשבון השירות שמוגדר כברירת מחדל.

אפשר להשתמש בספריית Error Reporting ל-Ruby בלי לספק פרטי כניסה באופן מפורש.

הכלי Error Reporting מופעל באופן אוטומטי באפליקציות בסביבה הגמישה של App Engine. לא נדרשת הגדרה נוספת.

Google Kubernetes Engine

כדי להשתמש ב-Error Reporting עם Google Kubernetes Engine:

  1. מוודאים שלחשבון השירות שבו משתמש הקונטיינר הוקצה התפקיד Error Reporting Writer ‏(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:

  1. מוודאים שלחשבון השירות שבו משתמשת המכונה הווירטואלית הוקצה התפקיד Error Reporting Writer ‏(roles/errorreporting.writer).

    אפשר להשתמש בחשבון השירות שמוגדר כברירת מחדל ב-Compute Engine או בחשבון שירות בהתאמה אישית.

    איך מנהלים את הגישה ברמת הפרויקט, התיקייה והארגון

  2. נכנסים לדף VM instances במסוף Google Cloud :

    נכנסים לדף VM instances.

    אם משתמשים בשורת החיפוש כדי למצוא את הדף, בוחרים בתוצאה שכותרת המשנה שלה היא Compute Engine.

  3. בוחרים את המכונה הווירטואלית שרוצים לתת לה את cloud-platform היקף הגישה.

  4. לוחצים על עצירה ואז על עריכה.

  5. בקטע Identity and API access, בוחרים חשבון שירות עם התפקיד "כתיבה של Error Reporting"‏ (roles/errorreporting.writer).

  6. בקטע Access scopes, בוחרים באפשרות Allow full access to all Cloud APIs ושומרים את השינויים.

  7. לוחצים על התחלה/המשך.

דוגמה

דיווח על שגיאות ב-frameworks שמבוססים על Rack

ספריית Error Reporting ל-Ruby מאפשרת לשלב בקלות את Error Reporting במסגרות פופולריות של Ruby לאינטרנט שמבוססות על Rack, כמו 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

הפעלת אפליקציות בסביבת פיתוח מקומית

כדי להשתמש בספריית Error Reporting ל-Ruby בסביבת פיתוח מקומית, כמו הפעלת הספרייה בתחנת העבודה שלכם, אתם צריכים לספק לספריית Error Reporting ל-Ruby את פרטי הכניסה המקומיים של ברירת המחדל של האפליקציה. מידע נוסף זמין במאמר אימות ל-Error Reporting.

כדי להשתמש בסביבת פיתוח מקומית בדוגמאות של Ruby שבדף הזה, מתקינים ומפעילים את ה-CLI של gcloud, ואז מגדירים את Application Default Credentials באמצעות פרטי הכניסה של המשתמש.

  1. התקינו את ה-CLI של Google Cloud.

  2. אם אתם משתמשים בספק זהויות חיצוני (IdP), קודם אתם צריכים להיכנס ל-CLI של gcloud באמצעות המאגר המאוחד לניהול זהויות.

  3. אם אתם משתמשים במעטפת מקומית, צרו פרטי כניסה לאימות מקומי בחשבון המשתמש:

    gcloud auth application-default login

    אם אתם משתמשים ב-Cloud Shell, אתם לא צריכים לעשות את זה.

    אם מוחזרת שגיאת אימות ואתם משתמשים בספק זהויות חיצוני (IdP), ודאו שנכנסתם ל-CLI של gcloud באמצעות המאגר המאוחד לניהול זהויות.

למידע נוסף, ראו הגדרת אימות לסביבת פיתוח מקומית.

מפתחות API נתמכים גם על ידי ‎projects.events.report method. אם אתם רוצים להשתמש במפתחות API לצורך אימות, לא צריך להגדיר קובץ מקומי של Application Default Credentials. מידע נוסף זמין במאמר יצירת מפתח API במסמכי התיעוד של Google Cloud בנושא אימות.

שימוש בממשק ההגדרות של Ruby on Rails

כדי להשתמש בספריית Error Reporting ל-Ruby עם מסגרת Ruby on Rails, צריך לספק את הפרמטרים דרך ממשק ההגדרות של 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

אפשר גם להגדיר הגדרות משותפות לכל ה-gems של Stackdriver באמצעות:

# 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

כברירת מחדל, הכלי Error Reporting מופעל כש-Rails פועל במצב ייצור. כדי להפעיל את Error Reporting במצב פיתוח, מוסיפים את הקוד הבא:

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

שימוש בממשק להגדרת אינסטרומנטציה

כדי להשתמש בספריית Error Reporting ל-Ruby באפליקציות אחרות שמבוססות על Rack, צריך לספק את הפרמטרים דרך ממשק ההגדרות:

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

אפשר גם להגדיר הגדרות משותפות לכל ה-gems של Google Cloud Observability באמצעות:

require "stackdriver"

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

הצגת קבוצות של שגיאות

נכנסים לדף Error Reporting במסוף Google Cloud :

נכנסים לדף Error Reporting.

אפשר גם למצוא את הדף הזה באמצעות שורת החיפוש.

למידע נוסף, קראו את המאמר הצגה וסינון של קבוצות שגיאות.