使用 Error Reporting 检查崩溃的应用是否存在错误
您可以使用 Error Reporting 查看、分诊和排查应用崩溃问题,帮助您的团队在问题影响用户之前发现问题并解决错误。
在本快速入门中,您将在 Google Cloud 项目中模拟错误情况,在控制台中查看 错误详情 Google Cloud ,并配置通知以便 在新错误发生时收到提醒。
准备工作
-
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 theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
- 为您的 Google Cloud 项目打开 Cloud Shell。在其启动时,系统会显示正在连接 消息。
- 将您的电子邮件地址配置为受支持的通知渠道。
模拟错误
要生成 11 个示例错误,请在 Cloud Shell 中运行以下脚本:
COUNTER=0 while [ $COUNTER -lt 11 ]; do gcloud beta error-reporting events report --service tutorial --service-version v$((COUNTER/10+1)) \ --message "java.lang.RuntimeException: Error rendering template $COUNTER at com.example.TestClass.test(TestClass.java:51) at com.example.AnotherClass(AnotherClass.java:25) at javax.servlet.http.HttpServlet.service (HttpServlet.java:617) at javax.servlet.http.HttpServlet.service (HttpServlet.java:717)" if [ $COUNTER -eq 10 ]; then echo "All sample errors reported." fi let COUNTER=COUNTER+1 done该脚本生成所有错误后,会输出以下一行内容:
All sample errors reported.
在 Error Reporting 中查看错误
-
在 Google Cloud 控制台中,前往 Error Reporting 页面:
您也可以使用搜索栏查找此页面。
Error Reporting 信息中心会显示找到的每个错误及其发生次数的摘要列表。 启用自动重新加载 后,Error Reporting 每 10 秒自动重新加载一次错误列表。
以下屏幕截图显示了一个示例错误报告页面:
点击错误名称可查看错误详情页面,其中包含错误的所有可用信息。如果项目已 启用 Gemini,则您可以使用 Gemini 获取问题排查建议。
配置通知
您可以配置 Error Reporting,以便在出现新类型的错误时收到通知。当现有错误再次出现时,系统不会发送通知:
-
在 Google Cloud 控制台中,前往 Error Reporting 页面:
您也可以使用搜索栏查找此页面。
如需启用电子邮件通知,请点击配置通知 。
在通知渠道 菜单中,选择您的电子邮件地址,然后选择保存 。
要生成新类型的错误, 请点击 terminal 激活 Cloud Shell 并运行以下脚本:
COUNTER=0 while [ $COUNTER -lt 3 ]; do gcloud beta error-reporting events report --service tutorial --service-version v1 \ --message "java.lang.ArrayIndexOutOfBoundsException: $COUNTER at com.example.AppController.createUser(AppController.java:42) at com.example.User(User.java:31) at javax.servlet.http.HttpServlet.service (HttpServlet.java:617) at javax.servlet.http.HttpServlet.service (HttpServlet.java:717)" let COUNTER=COUNTER+1 done echo "All sample errors reported."该脚本生成所有错误后,会输出以下一行内容:
All sample errors reported.查看您的电子邮件中是否有来自“Stackdriver 通知”的邮件。
通过 App Engine 应用生成错误消息
前面的脚本生成的示例消息会模拟错误情况。 如需通过 Google Cloud 项目中运行的实际服务生成错误,请按照 本部分中的步骤操作。
部署应用
如需下载应用并将其部署到 App Engine,请执行以下操作:
从 GitHub 项目 python-docs-samples 下载并配置应用:
将项目克隆到您的 Cloud Shell:
git clone https://github.com/GoogleCloudPlatform/python-docs-samples
Git 将返回类似于以下内容的消息:
git clone https://github.com/GoogleCloudPlatform/python-docs-samples Cloning into 'python-docs-samples'...
创建一个独立的 Python 环境并将其激活:
cd python-docs-samples/appengine/standard_python3/hello_world virtualenv env -p python3 source env/bin/activate
安装依赖项:
pip install -r requirements.txt
验证应用安装和配置是否成功:
运行应用:
python main.py
要在本地网页上查看应用,请点击 Cloud Shell 中显示的链接。该网页会显示
Hello World!。要停止正在运行的应用,请在 Cloud Shell 中输入
Ctrl-C。
将您的应用上传到 App Engine:
gcloud app deploy
如果系统提示您选择部署区域,请选择您附近的区域。当系统询问您是否要继续时,请输入
Y。 片刻之后,上传就会完成。通过在 Cloud Shell 中运行以下命令,以在浏览器中查看您的应用:
gcloud app browse
如果 Google Cloud CLI 找不到您的浏览器,则上一个命令会显示错误消息和链接。点击显示的链接。此时浏览器中会显示消息
Hello, World!。
生成错误
如需创建 Error Reporting 捕获并显示的错误,请执行以下操作:
修改
main.py,将return 'Hello World!
到
return 'Hello World!' + 1000
执行时,此更改会导致 Python
TypeError异常,因为连接字符串和整数是非法的。将修改后的应用上传到 App Engine:
gcloud app deploy
返回到包含正在运行的应用的浏览器标签页,然后重新加载此页面。由于您的更改,此时会出现一条错误消息,而不是显示
Hello, World!的页面:Internal Server Error The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.
如果您没有看到���消息,请稍等片刻,然后重新加载。新版本激活可能需要一些时间。
查看 Error Reporting 中的错误
如需查看错误,请按照上一部分在 Error Reporting 中查看错误中的说明操作。
清理
为避免因本页中使用的资源导致您的 Google Cloud 账号产生费用,请按照以下步骤操作。
- 在 Google Cloud 控制台中,前往 管理资源 页面。
- 在项目列表中,选择要删除的项目,然后点击删除。
- 在对话框中输入项目 ID,然后点击 关闭以删除项目。