הגדרה ב-Google Kubernetes Engine

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

שימוש ב-Logging לדיווח על שגיאות

סוכן Logging שמוגדר כברירת מחדל ב-GKE מספק פתרון מנוהל לפריסה ולניהול של הסוכנים ששולחים את היומנים של האשכולות ל-Cloud Logging. המבנה של הסוכן תלוי בגרסת האשכול. למידע נוסף על הסוכן הזה, ראו ניהול יומני GKE.

כדי להשתמש ב-Error Reporting, צריך לכלול את החריגות או את דוחות הקריסות ברשומת יומן אחת. רוב סוכני Logging יכולים לזהות שכמה שורות ביומן – מסגרות סטאק שכל אחת מהן מודפסת בשורה חדשה – מייצגות דוח קריסות, ולשלוח אותו ל-Cloud Logging כרשומה אחת ביומן. אם הסוכן לא יכול לשחזר כמה שורות כשגיאה אחת, השתמשו בנקודת הקצה ל-API של projects.events.report, שמאפשרת לכם לשלוט בתוכן של השגיאה.

שימוש ב-Error Reporting API לכתיבת שגיאות

‫Error Reporting API מספק נקודת קצה (endpoint) מסוג report לכתיבת מידע על שגיאות בשירות.

  1. מפעילים את Error Reporting API, אם הוא עדיין לא מופעל.

    התפקידים שנדרשים כדי להפעיל ממשקי API

    כדי להפעיל ממשקי API, צריך את ההרשאה serviceusage.services.enable. אם אתם יצרתם את הפרויקט, סביר להניח שכבר יש לכם את ההרשאה הזו דרך התפקיד "בעלים" (roles/owner). אחרת, תוכלו לקבל את ההרשאה הזו דרך התפקיד "אדמין בממשק 'שימוש בשירות'" (roles/serviceusage.serviceUsageAdmin). כך מקצים תפקידים.

    להפעלת ה-API

  2. לדווח על שגיאות ל-API באמצעות API בארכיטקטורת REST או ספריית לקוח.

    • ‫API בארכיטקטורת REST

      מידע נוסף על ה-API מופיע במאמרי העזרה.

    • שימוש בספריות לקוח

      יש ספריות במספר מוגבל של שפות, שבעזרתן אפשר לשלוח קריאות ל-Error Reporting API מהאפליקציה:

דוגמאות

ASP.NET

חבילת ה-NuGet של ASP.NET מדווחת ל-Error Reporting על חריגות שלא נתפסו מאפליקציות אינטרנט של ASP.NET.

התקנת חבילת NuGet

כדי להתקין את חבילת Stackdriver ASP.NET NuGet ב-Visual Studio:

  1. לוחצים לחיצה ימנית על הפתרון ובוחרים באפשרות Manage NuGet packages for solution.
  2. מסמנים את התיבה Include prerelease.
  3. מחפשים את החבילה בשם Google.Cloud.Diagnostics.AspNet ומתקינים אותה.

שימוש

אחרי שמתקינים את חבילת Stackdriver ASP.NET NuGet, מוסיפים את ההצהרה הבאה לקוד האפליקציה כדי להתחיל לשלוח שגיאות ל-Stackdriver:

using Google.Cloud.Diagnostics.AspNet;

כדי להפעיל את הדיווח על חריגות, מוסיפים את קוד ה-HttpConfiguration הבא ל-method‏ Register של אפליקציית האינטרנט ב-‎.NET, אחרי שמחליפים את your-project-id במזהה הפרויקט:

public static void Register(HttpConfiguration config)
{
    string projectId = "YOUR-PROJECT-ID";
    string serviceName = "NAME-OF-YOUR-SERVICE";
    string version = "VERSION-OF-YOUR-SERVCICE";
    // ...
    // Add a catch all for the uncaught exceptions.
    config.Services.Add(typeof(IExceptionLogger),
        ErrorReportingExceptionLogger.Create(projectId, serviceName, version));
    // ...
}

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

C#‎

הדוגמה הבאה נמצאת במאגר GoogleCloudPlatform/dotnet-docs-samples. כדי להשתמש בו, אחרי שיוצרים את הפרויקט, מציינים את מזהה הפרויקט:

C:\...\bin\Debug> set GOOGLE_PROJECT_ID=[YOUR_PROJECT_ID]

חשוב להחליף את [YOUR_PROJECT_ID] בערך הנכון ממסוףGoogle Cloud .

לאחר מכן, שולחים נתוני חריגות באמצעות קוד שדומה לקוד הבא:

public class ErrorReportingSample
{
    public static void Main(string[] args)
    {
        try
        {
            throw new Exception("Generic exception for testing Stackdriver Error Reporting");
        }
        catch (Exception e)
        {
            report(e);
            Console.WriteLine("Stackdriver Error Report Sent");
        }
    }

    /// <summary>
    /// Create the Error Reporting service (<seealso cref="ClouderrorreportingService"/>)
    /// with the Application Default Credentials and the proper scopes.
    /// See: https://developers.google.com/identity/protocols/application-default-credentials.
    /// </summary>
    private static ClouderrorreportingService CreateErrorReportingClient()
    {
        // Get the Application Default Credentials.
        GoogleCredential credential = GoogleCredential.GetApplicationDefaultAsync().Result;

        // Add the needed scope to the credentials.
        credential.CreateScoped(ClouderrorreportingService.Scope.CloudPlatform);

        // Create the Error Reporting Service.
        ClouderrorreportingService service = new ClouderrorreportingService(new BaseClientService.Initializer
        {
            HttpClientInitializer = credential,
        });
        return service;
    }

    /// <summary>
    /// Creates a <seealso cref="ReportRequest"/> from a given exception.
    /// </summary>
    private static ReportRequest CreateReportRequest(Exception e)
    {
        // Create the service.
        ClouderrorreportingService service = CreateErrorReportingClient();

        // Get the project ID from the environement variables.
        string projectId = Environment.GetEnvironmentVariable("GOOGLE_PROJECT_ID");

        // Format the project id to the format Error Reporting expects. See:
        // https://cloud.google.com/error-reporting/reference/rest/v1beta1/projects.events/report
        string formattedProjectId = string.Format("projects/{0}", projectId);

        // Add a service context to the report.  For more details see:
        // https://cloud.google.com/error-reporting/reference/rest/v1beta1/projects.events#ServiceContext
        ServiceContext serviceContext = new ServiceContext()
        {
            Service = "myapp",
            Version = "8c1917a9eca3475b5a3686d1d44b52908463b989",
        };
        ReportedErrorEvent errorEvent = new ReportedErrorEvent()
        {
            Message = e.ToString(),
            ServiceContext = serviceContext,
        };
        return new ReportRequest(service, errorEvent, formattedProjectId);
    }

    /// <summary>
    /// Report an exception to the Error Reporting service.
    /// </summary>
    private static void report(Exception e)
    {
        // Create the report and execute the request.
        ReportRequest request = CreateReportRequest(e);
        request.Execute();
    }
}

המשך

איך מגדירים את Error Reporting ל-Go

Java

איך מגדירים את Error Reporting ל-Java

Node.js

הגדרת Error Reporting ל-Node.js

Ruby

הגדרת Error Reporting ל-Ruby

Python

איך מגדירים את Error Reporting ל-Python

PHP

איך מגדירים את Error Reporting ל-PHP

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

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

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

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