設定運作時間快訊

本教學課程說明如何使用 Cloud Monitoring,為在 App Engine 彈性環境中執行的 Python Hello World 應用程式設定運作時間快訊。運作時間快訊會在應用程式沒有提供流量時通知您。您也可以為在 Compute EngineGoogle Kubernetes Engine (GKE) 上執行的應用程式設定運作時間快訊。

目標

  • 執行基本的 Hello World 應用程式。
  • 建立運作時間檢查,監控應用程式是否傳回 HTTP「200」狀態碼。
  • 建立快訊,在運作時間檢查失敗時傳送電子郵件訊息給您。
  • 重新啟動應用程式,觸發快訊。

費用

目前 Beta 版使用者可免費使用監控功能。

事前準備

  1. 登入 Google Cloud 帳戶。如果您是 Google Cloud新手,歡迎 建立帳戶,親自評估產品在實際工作環境中的成效。新客戶還能獲得價值 $300 美元的免費抵免額,可用於執行、測試及部署工作負載。
  2. 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 the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. 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 the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  5. Verify that billing is enabled for your Google Cloud project.

複製範例應用程式

您可以在 GitHub 的 GoogleCloudPlatform/getting-started-python 中找到範例應用程式。

  1. 複製存放區。

    git clone https://github.com/GoogleCloudPlatform/getting-started-python.git
    
  2. 前往範例目錄。

    cd getting-started-python/gce
    
  3. 由於應用程式只會傳回「Hello World!」,因此不需要任何設定,您可以立即執行。

    gcloud app deploy
    
  4. 如要查看傳回的訊息,請在瀏覽器中輸入下列網址:

    https://PROJECT_ID.REGION_ID.r.appspot.com

    更改下列內容:

建立運作時間檢查

部署應用程式後,您可以使用 Monitoring 建立運作時間檢查。這項檢查會持續對已部署的應用程式執行 Ping 作業,確保應用程式傳回的健康狀態回應正常。

  1. 前往 Google Cloud 控制台的「運作時間檢查」頁面:

    前往「Uptime checks」(運作時間檢查)

    如果您是使用搜尋列尋找這個頁面,請選取子標題為「Monitoring」的結果

  2. 按一下「建立運作時間檢查」
  3. 為檢查項目命名,例如 Check Hello World,然後按一下「下一步」

  4. 在「目標」部分,指定運作時間檢查要監控的內容:

    1. 由於您已部署至 App Engine,請將「資源類型」變更為「App Engine」,而非「網址」。(URL 用於在 Compute Engine 執行個體上產生自訂網址)。
    2. 選取運作時間檢查要監控的「服務」
    3. 如要預設為主要索引頁面,請將「路徑」留空。
    4. 將「檢查頻率」保留為預設的「1 分鐘」
    5. 點選「下一步」
  5. 將「Response Validation」欄位保留為預設值,然後按一下「Next」

  6. 在「快訊與通知」部分,指定發生快訊時的通知方式:

    • 確認切換按鈕的標籤為「已啟用快訊功能」
    • 名稱和時間長度欄位保留預設值。
    • 如要將通知管道新增至警告政策,請在標示為「通知管道」的文字方塊中,按一下「選單」。選取要新增的頻道,然後按一下「確定」。 通知會依管道類型分組,並依字母順序排序。

      如要將項目新增至核取方塊清單,請按一下「管理通知管道」,然後按照操作說明進行。返回這個對話方塊時,請按一下「重新整理」

  7. 點按「Create」(建立)。建立動作成功後,系統會顯示「Check and alert created」(已建立檢查和快訊) 訊息,然後顯示「運作時間檢查」資訊主頁頁面。

    新的運作時間檢查會列於運作時間檢查資訊主頁中。如果點選檢查名稱,系統會開啟該運作時間檢查的詳細資料檢視畫面。 這個檢視畫面會顯示多個圖表、正常運作時間百分比和設定資訊,並列出已設定的快訊政策。如要查看政策,請按一下政策名稱。

    您也可以從「Alerting」(快訊) 頁面開始,查看快訊政策。 在「Alerting」(警告) 頁面,「Policies」(政策) 窗格會列出部分政策。如要查看所有政策的清單,請按一下「查看所有政策」

模擬服務中斷

運作時間檢查建立完成後,您可以將應用程式的回應從 HTTP 200 OK 改為 HTTP 404 Sorry, we can't find that page 錯誤,模擬服務中斷。

  1. 下列程式碼顯示 Hello World 應用程式只會傳回「Hello World!」訊息,且回應的狀態碼預設為 200 OK。如要在 Hello World 應用程式中查看這段程式碼,請使用檢視函式。

    # Copyright 2019 Google LLC All Rights Reserved.
    #
    # 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.
    
    from flask import Flask
    app = Flask(__name__)
    
    
    @app.route('/', methods=['GET'])
    def say_hello():
        return "Hello, world!"
    
    
    if __name__ == '__main__':
        app.run(host='127.0.0.1', port=8080, debug=True)
    
  2. 如要讓 Hello World 應用程式傳回 HTTP 404 錯誤代碼,請在傳回值的第二部分新增 404 值,藉此變更傳回行。

    return 'Hello World', 404
  3. 部署新版應用程式 (故意損毀)。

    gcloud app deploy

    半小時內,您會收到電子郵件訊息,指出正常運作時間檢查失敗。

清除所用資源

為避免因為本教學課程所用資源,導致系統向 Google Cloud 帳戶收取費用,請刪除含有相關資源的專案,或者保留專案但刪除個別資源。

如要避免付費,最簡單的方法就是刪除您為了本教學課程所建立的專案。

刪除專案的方法如下:

  1. 前往 Google Cloud 控制台的「Manage resources」(管理資源) 頁面。

    前往「Manage resources」(管理資源)

  2. 在專案清單中選取要刪除的專案,然後點選「Delete」(刪除)
  3. 在對話方塊中輸入專案 ID,然後按一下 [Shut down] (關閉) 以刪除專案。