使用 Error Reporting 检查崩溃的应用是否存在错误

利用 Error Reporting,您可以从一个位置集中监控 Google Cloud 项目中所有应用和服务以及 Amazon Elastic Compute Cloud (EC2) 应用的错误情况。

在本指南中,我们将了解如何执行以下操作:

  1. 模拟 Google Cloud 项目中的服务发生的错误。

  2. 使用 Error Reporting 查看错误并更改错误状态,以使团队中的其他成员知道问题正在得到处理。

  3. 设置通知,以便在出现新类型的错误时收到通知。


如需在 Google Cloud 控制台中直接遵循有关此任务的分步指导,请点击操作演示

操作演示


准备工作

  1. 登录您的 Google Cloud 账号。如果您是 Google Cloud 新手,请创建一个账号来评估我们的产品在实际场景中的表现。新客户还可获享 $300 赠金,用于运行、测试和部署工作负载。
  2. 在 Google Cloud Console 中的项目选择器页面上,选择或创建一个 Google Cloud 项目

    转到“项目选择器”

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

  4. 在 Google Cloud Console 中的项目选择器页面上,选择或创建一个 Google Cloud 项目

    转到“项目选择器”

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

  6. 为您的 Google Cloud 项目打开 Cloud Shell。在其启动时,系统会显示正在连接消息。

    打开 Cloud Shell

模拟错误

  1. 要生成 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 中查看错误

  1. 在 Google Cloud 控制台的导航面板中,选择 Error Reporting,然后选择您的 Google Cloud 项目:

    前往 Error Reporting

    Error Reporting 信息中心会显示找到的每个错误及其发生次数的摘要列表。启用自动重新加载后,Error Reporting 每 10 秒自动重新加载一次错误列表。

    以下屏幕截图显示了错误报告页面示例:

    显示错误列表的界面。

  2. 点击错误名称以查看错误详情页面,其中包含有关某个错误的所有可用信息。如果项目中启用了 Duet AI,您可以使用 Duet AI 获取问题排查建议

配置通知

您可以配置 Error Reporting,以便在出现新类型的错误时收到通知。当现有错误再次出现时,系统不会发送通知:

  1. 在 Google Cloud 控制台的导航面板中,选择 Error Reporting,然后选择您的 Google Cloud 项目:

    前往 Error Reporting

  2. 要启用电子邮件通知,请点击更多 ,然后从下拉列表中选择为项目开启新错误通知

  3. 要生成新类型的错误,请点击激活 Cloud Shell激活 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.

  4. 查看您的电子邮件中是否有来自“Stackdriver 通知”的邮件。

通过 App Engine 应用生成错误消息

虽然您通过脚本生成的示例消息可完全模拟 Error Reporting 中的错误情况,但如果您希望通过 Google Cloud 项目中运行的实际服务生成错误,请按照本部分中的步骤操作。

部署应用

如需下载应用并将其部署到 App Engine,请执行以下操作:

  1. 从 GitHub 项目 python-docs-samples 下载并配置应用:

    1. 将项目克隆到您的 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'...
      
    2. 创建一个独立的 Python 环境并将其激活:

      cd python-docs-samples/appengine/standard_python3/hello_world
      virtualenv env -p python3
      source env/bin/activate
      
    3. 安装依赖项:

      pip install -r requirements.txt
      
  2. 验证应用安装和配置是否成功:

    1. 运行应用:

      python main.py
      
    2. 要在本地网页上查看应用,请点击 Cloud Shell 中显示的链接。该网页会显示 Hello World!

    3. 要停止正在运行的应用,请在 Cloud Shell 中输入 Ctrl-C

  3. 将您的应用上传到 App Engine:

    gcloud app deploy
    

    如果系统提示您选择部署区域,请选择您附近的区域。当系统询问您是否要继续时,请输入 Y。 片刻之后,上传就会完成。

  4. 通过在 Cloud Shell 中运行以下命令,以在浏览器中查看您的应用:

    gcloud app browse
    

    如果 Google Cloud CLI 找不到您的浏览器,则上一个命令会显示错误消息和链接。点击显示的链接。此时浏览器中会显示消息 Hello, World!

生成错误

如需创建 Error Reporting 捕获并显示的错误,请执行以下操作:

  1. 修改 main.py,将

    return 'Hello World!
    

    return 'Hello World!' + 1000
    

    执行时,此更改会导致 Python TypeError 异常,因为连接字符串和整数是非法的。

  2. 将修改后的应用上传到 App Engine:

    gcloud app deploy
    
  3. 返回到包含正在运行的应用的浏览器标签页,然后重新加载此页面。由于您的更改,此时会出现一条错误消息,而不是显示 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 账号产生费用,请按照以下步骤操作。

  1. 在 Google Cloud 控制台中,进入管理资源页面。

    转到“管理资源”

  2. 在项目列表中,选择要删除的项目,然后点击删除
  3. 在对话框中输入项目 ID,然后点击关闭以删除项目。

后续步骤