为 Go 应用添加插桩以使用 Error Reporting

借助 Go 专用 Error Reporting 软件包,您可以直接从 Go 应用向 Error Reporting 发送错误事件,以便诊断和监控运行时崩溃。使用该库对应用进行插桩处理后,即使日志存储桶使用客户管理的加密密钥 (CMEK),您也可以报告自定义错误事件并捕获错误。

对于在集成 Google Cloud 服务(包括 Cloud Run 函数、App Engine、Compute Engine 和 Google Kubernetes Engine)上运行的应用,Error Reporting 还可以从 Cloud Logging 中自动推断错误事件,而无需使用 Go 的 Error Reporting 软件包。为了让 Error Reporting 从日志中推断错误事件,您的日志条目必须遵循错误格式要求,并且存储在源项目或日志路由到的目标项目拥有的非 CMEK 日志存储桶中。

准备工作

  1. 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

  2. Verify that billing is enabled for your Google Cloud project.

  3. 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

安装客户端库

借助适用于 Go 的 Error Reporting 软件包,您可以监控和查看几乎在任何位置运行的 Go 应用报告的错误事件。

  1. 使用 go get 安装程序包:

    go get cloud.google.com/go/errorreporting

如需详细了解如何安装,请参阅 Go 版 Error Reporting 软件包的文档。您还可以使用问题跟踪器来报告问题。

配置客户端库

您可以自定义 Go 版 Stackdriver Error Reporting 程序包的行为。参阅 godoc。

在 Google Cloud上运行应用

如需使用 projects.events.report 创建错误组,您的服务账号需要具有 Error Reporting Writer 角色 (roles/errorreporting.writer)。

某些 Google Cloud 服务会自动向相应的服务账号授予 Error Reporting Writer 角色 (roles/errorreporting.writer)。不过,对于某些服务,您必须向相应的服务账号授予此角色。

Cloud Run 和 Cloud Run functions

Cloud Run 使用的默认服务账号具有Error Reporting (与 Stackdriver 搭配使用时) 写入者角色 (roles/errorreporting.writer) 的权限。

无需明确提供凭据即可使用 Go 版 Error Reporting 软件包。

Cloud Run 已配置为自动使用 Error Reporting。未处理的 JavaScript 异常将显示在 Logging 中,并由 Error Reporting 处理,而无需使用适用于 Go 的 Error Reporting 软件包。

App Engine 柔性环境

App Engine 会自动向您的默认服务账号授予Error Reporting (与 Stackdriver 搭配使用时) 写入者角色 (roles/errorreporting.writer)。

无需明确提供凭据即可使用 Go 版 Error Reporting 软件包。

系统会自动为 App Engine 柔性环境应用启用 Error Reporting。 无需进行额外设置。

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. 在 Google Cloud 控制台中,前往虚拟机实例页面:

    前往虚拟机实例

    如果您使用搜索栏查找此页面,请选择子标题为 Compute Engine 的结果。

  3. 选择要接收 cloud-platform 访问权限范围的虚拟机实例。

  4. 点击停止,然后点击修改。

  5. 在身份和 API 访问权限部分,选择具有 Error Reporting Writer 角色 (roles/errorreporting.writer) 的服务账号。

  6. 在访问权限范围部分中,选择授予对所有 Cloud API 的完整访问权限,然后保存更改。

  7. 点击启动/恢复。

示例

以下示例演示了如何使用 Go 客户端库报告自定义错误事件:


// Sample errorreporting_quickstart contains is a quickstart
// example for the Google Cloud Error Reporting API.
package main

import (
	"context"
	"errors"
	"log"
	"os"

	"cloud.google.com/go/errorreporting"
)

var errorClient *errorreporting.Client

func main() {
	// Set your Google Cloud Platform project ID via environment or explicitly
	projectID := os.Getenv("GOOGLE_CLOUD_PROJECT")
	args := os.Args[1:]
	if len(args) > 0 && args[0] != "" {
		projectID = args[0]
	}

	ctx := context.Background()
	var err error
	errorClient, err = errorreporting.NewClient(ctx, projectID, errorreporting.Config{
		ServiceName:    "errorreporting_quickstart",
		ServiceVersion: "0.0.0",
		OnError: func(err error) {
			log.Printf("Could not report the error: %v", err)
		},
	})
	if err != nil {
		log.Fatal(err)
	}
	defer errorClient.Close()

	err = errors.New("something went wrong")
	if err != nil {
		logAndPrintError(err)
		return
	}
}

func logAndPrintError(err error) {
	/// Client autopopulates the error context of the error. For more details about the context see:
	/// https://cloud.google.com/error-reporting/reference/rest/v1beta1/ErrorContext
	errorClient.Report(errorreporting.Entry{
		Error: err,
	})
	log.Print(err)
}

要查看更多示例了解如何报告紧急状况和错误,请参阅 godoc。

在本地开发环境中运行应用

如需在本地开发环境中使用 Go 的 Error Reporting 软件包(例如在您自己的工作站上运行该库),您必须为 Go 的 Error Reporting 软件包提供本地应用默认凭据。如需了解详情,请参阅向 Error Reporting 进行身份验证。

如需在本地开发环境中使用本页面上的 Go 示例,请安装并初始化 gcloud CLI,然后使用您的用户凭据设置应用默认凭据。

  1. 安装 Google Cloud CLI。

  2. 配置 gcloud CLI 以使用您的联合身份。

    如需了解详情,请参阅使用联合身份登录 gcloud CLI。

  3. 为您的用户账号创建本地身份验证凭证:

    gcloud auth application-default login

    如果系统返回身份验证错误,并且您使用的是外部身份提供方 (IdP),请确认您已 使用联合身份登录 gcloud CLI。

如需了解详情,请参阅 为本地开发环境设置身份验证。

projects.events.report 方法也支持 API 密钥。 如果您要使用 API 密钥进行身份验证,则无需设置本地应用默认凭据文件。 如需了解详情,请参阅 Google Cloud 身份验证文档中的创建 API 密钥。

查看错误组

在 Google Cloud 控制台中,前往 Error Reporting 页面:

前往 Error Reporting

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

如需了解详情,请参阅查看和过滤错误组。