在 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 包从 从 ASP.NET Web 应用迁移到 Error Reporting。

安装 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添加到Register .NET Web 应用(将 your-project-id 替换为您的实际 项目 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 中) 部分。

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 页面:

前往 Error Reporting

您也可以使用搜索栏查找此页面。