הוספת כלי מעקב לאפליקציות Python לצורך דיווח על שגיאות

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

באפליקציות שפועלות בשירותים משולבים של Google Cloud , כולל פונקציות Cloud Run,‏ App Engine,‏ Compute Engine ו-Google Kubernetes Engine, הכלי Error Reporting יכול גם להסיק באופן אוטומטי על אירועי שגיאה מ-Cloud Logging בלי ספריית Error Reporting ל-Python. כדי ש-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. הכנת הסביבה לפיתוח ב-Python.

    למדריך ההגדרה של Python

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

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

  1. יוצרים סביבה וירטואלית ומשתמשים ב-pip כדי להתקין את החבילה:

    pip install google-cloud-error-reporting --upgrade
    
  2. כדי להתחיל לדווח על שגיאות, מייבאים את הספרייה ויוצרים מופע של לקוח:

    Python

    כדי לבצע אימות ב-Error Reporting, אתם צריכים להגדיר את Application Default Credentials. למידע נוסף, ראו הגדרת אימות לסביבת פיתוח מקומית.

    #!/usr/bin/env python
    #
    # Copyright 2022 Google, Inc.
    #
    # Licensed under the Apache License, Version 2.0 (the "License");
    # you may not use this file except in compliance with the License.
    # You may obtain a copy of the License at
    #
    #    http://www.apache.org/licenses/LICENSE-2.0
    #
    # Unless required by applicable law or agreed to in writing, software
    # distributed under the License is distributed on an "AS IS" BASIS,
    # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    # See the License for the specific language governing permissions and
    # limitations under the License.
    
    
    def report_exception():
        from google.cloud import error_reporting
    
        client = error_reporting.Client()
        try:
            raise Exception("Something went wrong")
        except Exception:
            client.report_exception()
    
    
    
    
    def report_manual_error():
        from google.cloud import error_reporting
    
        client = error_reporting.Client()
        client.report("An error has occurred.")
    
    
    
    
    if __name__ == "__main__":
        report_exception()
        report_manual_error()
    

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

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

אתם יכולים להתאים אישית את ההתנהגות של ספריית Error Reporting ל-Python. דוגמאות להגדרות מופיעות במסמכי התיעוד של ספריית הלקוח.

הפעלת אפליקציות במכשיר 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 ל-Python בלי לספק פרטי כניסה באופן מפורש.

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

סביבה גמישה ב-App Engine

ב-App Engine, התפקיד "כתיבה ב-Error Reporting" (roles/errorreporting.writer) מוקצה אוטומטית לחשבון השירות שמוגדר כברירת מחדל.

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

הכלי 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. לוחצים על התחלה/המשך.

דוגמה

כדי לדווח על שגיאה באופן ידני, אפשר להפעיל את השיטה report, כמו בדוגמה הבאה:

Python

כדי לבצע אימות ב-Error Reporting, אתם צריכים להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות.

#!/usr/bin/env python
#
# Copyright 2022 Google, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


def report_exception():
    from google.cloud import error_reporting

    client = error_reporting.Client()
    try:
        raise Exception("Something went wrong")
    except Exception:
        client.report_exception()




def report_manual_error():
    from google.cloud import error_reporting

    client = error_reporting.Client()
    client.report("An error has occurred.")




if __name__ == "__main__":
    report_exception()
    report_manual_error()

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

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

כדי להשתמש בסביבת פיתוח מקומית בדוגמאות של Python שבדף הזה, מתקינים ומפעילים את ה-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 בנושא אימות.

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

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

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

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

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