您可以使用 Python 版 Error Reporting 库,从 Python 应用向 Error Reporting 发送错误报告。
Error Reporting 已与某些 Google Cloud 服务集成,例如 App Engine、Compute Engine 和 Google Kubernetes Engine。Error Reporting 可显示在这些服务上运行的应用记录到 Cloud Logging 中的错误。如需了解详情,请参阅本页面上的在 Google Cloud Platform 上运行。
您还可以使用 Logging 将错误数据发送到 Error Reporting。如需了解数据格式设置要求,请参阅设置 Logging 中的错误消息格式。
准备工作
- 登录您的 Google Cloud 帐号。如果您是 Google Cloud 新手,请创建一个帐号来评估我们的产品在实际场景中的表现。新客户还可获享 $300 赠金,用于运行、测试和部署工作负载。
-
在 Google Cloud Console 中的项目选择器页面上,选择或创建一个 Google Cloud 项目。
-
确保您的 Cloud 项目已启用结算功能。了解如何检查项目是否已启用结算功能。
-
启用 Error Reporting API 。
-
在 Google Cloud Console 中的项目选择器页面上,选择或创建一个 Google Cloud 项目。
-
确保您的 Cloud 项目已启用结算功能。了解如何检查项目是否已启用结算功能。
-
启用 Error Reporting API 。
- 准备 Python 开发环境。
安装客户端库
借助 Python 版 Error Reporting 库,您可以监控和查看几乎在任何位置运行的 Python 应用所报告的错误。
-
实例化虚拟环境并使用 pip 安装程序包:
pip install google-cloud-error-reporting --upgrade
-
导入库并实例化客户端,以开始报告错误:
Python
如需详细了解如何安装,请参阅 Python 版 Error Reporting 库的文档。您还可以使用问题跟踪器来报告问题。
配置客户端库
您可以自定义 Python 版 Error Reporting 库的行为。有关配置示例,请参阅客户端库文档。
报告错误
您可以调用 report
方法来手动报告错误,如以下示例所示:
Python
在 Google Cloud 上运行
要使用 Python 版 Error Reporting 库,您必须具备 Identity and Access Management 的 Error Reporting Writer 角色。大多数 Google Cloud 计算平台均默认提供此角色。
您可以使用以下 Google Cloud 环境配置 Python 版 Error Reporting。
App Engine 柔性环境
App Engine 默认授予 Error Reporting Writer 角色。
您无需明确提供凭据即可使用 Python 版 Error Reporting 库。
系统会自动为 App Engine 柔性环境应用启用 Error Reporting。 无需进行额外设置。Google Kubernetes Engine
在 GKE 上,您必须在创建集群时添加 cloud-platform
访问权限范围,如以下示例命令所示:
gcloud container clusters create example-cluster-name --scopes https://www.googleapis.com/auth/cloud-platform
Compute Engine
使用 Compute Engine 虚拟机实例时,请为每个实例添加 cloud-platform
访问权限范围。通过 Google Cloud Console 创建新实例时,您可以在创建实例面板的身份和 API 访问权限部分执行此操作。请使用 Compute Engine 默认服务帐号或您选用的其他服务帐号,并在身份和 API 访问权限部分中勾选允许所有 Cloud API 的全面访问权限。无论您选择哪个服务帐号,都请确保已在控制台的 IAM 和管理部分向该服务帐号授予 Error Reporting 写入者角色。
在本地和其他位置运行
如需在 Google Cloud 之外(包括您自己的工作站、您数据中心的计算机或另一家云提供商的虚拟机实例上)使用 Python 版 Error Reporting 库,您必须直接向 Python 版 Error Reporting 库提供您的 Google Cloud 项目 ID 和相应的服务帐号凭据。
您可以手动创建和获取服务帐号凭据。指定角色字段时,请使用 Error Reporting Writer 角色。如需详细了解 Identity and Access Management 角色,请转到访问权限控制指南。
查看错误报告
部署后,您可以在控制台的 Error Reporting 信息中心中查看错误报告。
如需了解详情,请参阅查看错误。