在 Google Kubernetes Engine 上进行设置

您可以采用以下两种方法之一,将 Google Kubernetes Engine 应用中的错误发送到 Error Reporting:

使用 Logging 报告错误

GKE 的默认日志记录代理提供了一个代管式解决方案,可用于部署和管理将集群日志发送到 Cloud Logging 的代理。代理的结构取决于集群版本。如需了解此代理,请参阅管理 GKE 日志

Error Reporting 要求将异常或堆栈轨迹包含在单个日志条目中。大多数日志记录代理能够识别几个日志行(每个日志行各占一行),表示一个堆栈轨迹,并将其作为单个日志条目发送到 Cloud Logging。如果代理无法将多条代码行重构为单个错误,请使用 projects.events.report API 端点,以便您可以控制错误的内容。

使用 Error Reporting API 写入错误

Error Reporting API 提供了 report 端点,用于将错误信息写入服务。

  1. 启用 Error Reporting API。

    启用 API

  2. 使用 REST API 或客户端库向 API 报告错误。

示例

ASP.NET

ASP.NET NuGet 程序包向 Error Reporting 报告 ASP.NET Web 应用中未捕获的异常。

安装 NuGet 程序包

要在 Visual Studio 中安装 Stackdriver ASP.NET NuGet 程序包,请执行以下操作:

  1. 右键点击您的解决方案,然后选择管理解决方案的 NuGet 程序包
  2. 选中包括预发行版复选框。
  3. 搜索并安装名为 Google.Cloud.Diagnostics.AspNet 的软件包。

使用情况

安装 Stackdriver ASP.NET NuGet 程序包之后,请将下列语句添加到应用代码中,以开始向 Stackdriver 发送错误:

using Google.Cloud.Diagnostics.AspNet;

将以下 HttpConfiguration 代码添加到您的.NET Web 应用的 Register 方法中(用您的实际项目 ID 替换 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 代码库。若要使用该示例,请在构建项目后指定您的项目 ID

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();
    }
}

Go

请参阅设置 Go 版 Error Reporting

Java

请参阅设置 Java 版 Error Reporting

Node.js

请参阅设置 Node.js 版 Error Reporting

Ruby

请参阅设置 Ruby 版 Error Reporting

Python

请参阅设置 Python 版 Error Reporting

PHP

请参阅设置 PHP 版 Error Reporting

查看错误组

在 Google Cloud 控制台的导航面板中,选择 Error Reporting,然后选择您的 Google Cloud 项目:

前往 Error Reporting