对 Python 应用进行插桩处理以进行 Error Reporting

您可以通过 Python 将错误报告发送到 Error Reporting 如何使用 Google Cloud 中的 Python 版 Error Reporting 库。 使用 Python 版 Error Reporting 库为以下情况创建错误组:

  • 您的日志存储桶包含客户管理的加密密钥 (CMEK)
  • 您的日志存储分区不在 global 区域。
  • 您想要报告自定义错误事件。

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

您还可以使用 Logging 将错误数据发送到 Error Reporting。如需了解数据格式设置要求,请参阅设置 Logging 中的错误消息格式

准备工作

  1. 登录您的 Google Cloud 账号。如果您是 Google Cloud 新手,请创建一个账号来评估我们的产品在实际场景中的表现。新客户还可获享 $300 赠金,用于运行、测试和部署工作负载。
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. 确保您的 Google Cloud 项目已启用结算功能

  4. 启用 Error Reporting API 。

    启用 API

  5. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  6. 确保您的 Google Cloud 项目已启用结算功能

  7. 启用 Error Reporting API 。

    启用 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 写入者角色 (roles/errorreporting.writer)。

某些 Google Cloud 服务会自动 Error Reporting Writer 角色 (roles/errorreporting.writer) 服务账号。但是,您必须将此角色授予相应的服务 使用部分服务

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 写入者角色 (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 写入者角色 (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. 如需初始化 gcloud CLI,请运行以下命令:

    gcloud init
  3. 为您的 Google 账号创建本地身份验证凭据:

    gcloud auth application-default login

如需了解详情,请参阅 Set up authentication for a local development environment

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

查看错误报告

在 Google Cloud 控制台中,转到 Error Reporting 页面:

前往 Error Reporting

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

如需了解详情,请参阅查看错误