为 Error Reporting 插桩 Python 应用

您可以使用 Python 版 Error Reporting 库,从 Python 应用向 Error Reporting 发送错误事件。在以下情况下,请使用 Python 版 Error Reporting 库创建错误组:

  • 包含日志条目的日志存储桶具有客户管理的加密 密钥 (CMEK)
  • 日志存储桶满足以下条件之一:
    • 日志存储桶存储在日志条目来源的项目中。
    • 日志条目被路由到一个项目,然后该项目将这些日志条目存储在自己拥有的日志存储桶中。
  • 您想要报告自定义错误事件。

Error Reporting 已与某些 Google Cloud 服务集成, 例如 Cloud Run functionsApp EngineCompute EngineGoogle Kubernetes Engine。Error Reporting 可显示在这些服务上运行的应用记录到 Cloud Logging 中的错误事件。 如需了解详情,请参阅本 页面上的 在上运行 Google Cloud

您还可以使用 Logging将错误事件发送到 Error Reporting。如需了解数据格式设置 要求,请参阅 设置日志条目格式以报告错误事件

准备工作

  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 .

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. 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 .

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the API

  8. 准备 Python 开发环境。

    转到 Python 设置指南

安装客户端库

借助 Python 版 Error Reporting 库,您可以监控和查看几乎在任何位置运行的 Python 应用所报告的错误事件。

  1. 实例化虚拟环境并使用 pip 安装软件包:

    pip install google-cloud-error-reporting --upgrade
    
  2. 导入库并实例化客户端,以开始报告错误:

    Python

    如需向 Error Reporting 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅 为本地开发环境设置身份验证

    #!/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()
    

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

配置客户端库

您可以自定义 Python 版 Error Reporting 库的行为。有关配置示例,请参阅 客户端库文档

在上运行应用 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 Writer 角色 (roles/errorreporting.writer) 的 权限。

您无需明确提供凭据即可使用 Python 版 Error Reporting 库。

Cloud Run 已配置为自动使用 Error Reporting。未处理的 JavaScript 异常将在 Logging 中显示,并且会由 Error Reporting 进行处理,而无需使用 Python 版 Error Reporting 库。

App Engine 柔性环境

App Engine 会自动向您的默认服务帐号授予 Error Reporting Writer 角色 (roles/errorreporting.writer) 。

您无需明确提供凭据即可使用 Python 版 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 访问权限范围

    例如,以下 create 命令指定了 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. 点击启动/恢复

示例

您可以调用 report 方法来手动报告错误,如以下示例所示:

Python

如需向 Error Reporting 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅 设置身份验证

#!/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()

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

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

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

  1. 安装 Google Cloud CLI。

  2. 如果您使用的是外部身份提供方 (IdP),则必须先 使用联合身份登录 gcloud CLI

  3. 如果您使用的是本地 shell,请为您的用户 账号创建本地身份验证凭证:

    gcloud auth application-default login

    如果您使用的是 Cloud Shell,则无需执行此操作。

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

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

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

查看错误组

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

前往 Error Reporting

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

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