使用 Error Reporting 检查崩溃的应用是否存在错误
利用 Error Reporting,您可以从一个位置集中监控 Google Cloud 项目中的所有应用和服务以及 Amazon Elastic Compute Cloud (EC2) 应用的错误情况。
在本指南中,我们将了解如何执行以下操作:
模拟 Google Cloud 项目中的服务发生的错误。
使用 Error Reporting 查看错误并更改错误状态,以使团队中的其他成员知道问题正在得到处理。
设置通知,以便在出现新类型的错误时收到通知。
如需在 Google Cloud 控制台中直接遵循有关此任务的分步指导,请点击操作演示:
准备工作
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Verify that billing is enabled for your Google Cloud project.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Verify that billing is enabled for your Google Cloud project.
- 为您的 Google Cloud 项目打开 Cloud Shell。在 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.
-
在 Google Cloud 控制台中,前往 Error Reporting 页面:
您也可以使用搜索栏查找此页面。
Error Reporting 信息中心会显示找到的每个错误及其发生次数的摘要列表。启用自动重新加载后,Error Reporting 每 10 秒自动重新加载一次错误列表。
以下屏幕截图显示了一个错误报告页面示例:
点击错误名称可查看错误详情页面,其中包含错误的所有可用信息。如果项目中启用了 Gemini,您就可以使用 Gemini 获取问题排查建议。
-
在 Google Cloud 控制台中,前往 Error Reporting 页面:
您也可以使用搜索栏查找此页面。
要启用电子邮件通知,请点击更多 more_vert,然后从下拉列表中选择为项目开启新错误通知。
要生成新类型的错误,请点击激活 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)" if [ $COUNTER -eq 10 ]; then echo "All sample errors reported." fi let COUNTER=COUNTER+1 done
该脚本生成所有错误后,会输出以下行:
All sample errors reported.
查看您的电子邮件中是否有来自“Stackdriver 通知”的邮件。
从 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!
。修改
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.
如果您没有看到此消息,请稍等片刻,然后重新加载。新版本激活可能需要一些时间。
- In the Google Cloud console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.
模拟错误
在 Error Reporting 中查看错误
配置通知
您可以配置 Error Reporting,以便在出现新类型的错误时收到通知。当现有错误再次出现时,系统不会发送通知:
通过 App Engine 应用生成错误消息
虽然通过脚本生成的示例消息可以完全模拟 Error Reporting 中的错误情况,但如果您想通过 Google Cloud 项目中运行的实际服务生成错误,请按照本部分中的步骤操作。
部署应用
如需下载应用并将其部署到 App Engine,请执行以下操作:
生成错误
如需创建 Error Reporting 捕获并显示的错误,请执行以下操作:
查看 Error Reporting 中的错误
如需查看错误,请按照上一部分在 Error Reporting 中查看错误中的说明操作。
清理
为避免因本页中使用的资源导致您的 Google Cloud 账号产生费用,请按照以下步骤操作。