本文件說明如何使用用戶端程式庫或 Google Cloud CLI 建立及管理通知管道,這兩種方法都會叫用 Cloud Monitoring API。當符合快訊政策的條件時,Cloud Monitoring 會透過通知管道通知您或值班團隊。可用的管道類型有好幾種,每種類型都會在通知管道描述元中說明。特定類型的通知管道是該類型描述項的例項。快訊政策會參照要用於通知管道的通知管道。
通知管道必須存在,才能用於快訊政策。通知管道描述元會提供給您,但您必須先建立管道,才能使用這些管道。
如要使用 Google Cloud 主控台設定通知管道,請參閱「建立及管理通知管道」。
本文使用的程式碼範例是從「範例:備份和還原」一節所述的警示政策 API 範例中擷取。
關於 API
NotificationChannel
資源支援可讓您管理通知管道的作業。也支援管理管道 verificationStatus
欄位相關的作業:
- 傳送驗證碼
- 產生驗證碼來將已驗證之管道的驗證狀態,複製到同一專案或新專案中的其他相同管道
- 使用前兩項作業建立的驗證碼來驗證管道
詳情請參閱 notificationChannels
參考文件。
事前準備
-
如要取得使用 Cloud Monitoring API 查看及設定通知管道所需的權限,請要求管理員為您授予專案的 Monitoring NotificationChannel Editor (
roles/monitoring.notificationChannelEditor
) IAM 角色。如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和機構的存取權」。如要進一步瞭解 Cloud Monitoring 角色,請參閱「使用 Identity and Access Management 控管存取權」。
-
Select the tab for how you plan to use the samples on this page:
gcloud
In the Google Cloud console, activate Cloud Shell.
At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.
C#
如要在本機開發環境中使用本頁面上的 .NET 範例,請先安裝並初始化 gcloud CLI,然後使用您的使用者憑證設定應用程式預設憑證。
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
詳情請參閱 Google Cloud 驗證說明文件中的「 為本機開發環境設定 ADC」。
Go
如要在本機開發環境中使用本頁面上的 Go 範例,請先安裝並初始化 gcloud CLI,然後使用您的使用者憑證設定應用程式預設憑證。
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
詳情請參閱 Google Cloud 驗證說明文件中的「 為本機開發環境設定 ADC」。
Java
如要在本機開發環境中使用本頁面上的 Java 範例,請先安裝並初始化 gcloud CLI,然後使用您的使用者憑證設定應用程式預設憑證。
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
詳情請參閱 Google Cloud 驗證說明文件中的「 為本機開發環境設定 ADC」。
Node.js
如要在本機開發環境中使用本頁面上的 Node.js 範例,請先安裝並初始化 gcloud CLI,然後使用您的使用者憑證設定應用程式預設憑證。
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
詳情請參閱 Google Cloud 驗證說明文件中的「 為本機開發環境設定 ADC」。
PHP
如要在本機開發環境中使用本頁面上的 PHP 範例,請先安裝並初始化 gcloud CLI,然後使用您的使用者憑證設定應用程式預設憑證。
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
詳情請參閱 Google Cloud 驗證說明文件中的「 為本機開發環境設定 ADC」。
Python
如要在本機開發環境中使用本頁面上的 Python 範例,請先安裝並初始化 gcloud CLI,然後使用您的使用者憑證設定應用程式預設憑證。
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
詳情請參閱 Google Cloud 驗證說明文件中的「 為本機開發環境設定 ADC」。
如果您打算使用 Google Cloud CLI,請設定預設專案:
gcloud config set project PROJECT_ID
執行上述指令前,請先取代以下內容:
- PROJECT_ID:專案的 ID。如要設定 App Hub,請選取 App Hub 主機專案或啟用應用程式的資料夾管理專案。
列出通知管道類型
監控功能提供多種內建通知管道類型。每個類型都會在
NotificationChannelDescriptor
中說明。這些描述元具有type
欄位,這個欄位的值可在建立該管道類型的執行個體時做為 ID 使用。如要擷取可透過 Cloud Monitoring API 或 Google Cloud CLI 建立的管道類型清單,請輸入下列指令:$ gcloud beta monitoring channel-descriptors list --format='value(type)' campfire email google_chat hipchat pagerduty pubsub slack sms webhook_basicauth webhook_tokenauth
如要進一步瞭解通知管道,請參閱「建立及管理通知管道」。
如果系統不支援您偏好的通知管道,建議您建立 pipeline,以便將通知傳送至 Pub/Sub。如需使用 Flask 的 Python 範例,請參閱「使用 Cloud Monitoring 和 Cloud Run 建立自訂通知」。如需其他範例,請參閱 cloud-alerting-notification-forwarding Git 存放區。
如要擷取 Google Cloud 專案中所有的管道描述元,請使用
notificationChannelDescriptors.list
方法。擷取的描述符為唯讀。如果您要尋找特定描述元且知道描述元名稱,則可以使用
notificationChannelDescriptors.get
方法只擷取這個管道描述元。管道描述元的名稱格式為projects/[PROJECT_ID]/notificationChannelDescriptors/[CHANNEL_TYPE]
。[CHANNEL_TYPE]
必須是先前列出的類型之一。例如:projects/[PROJECT_ID]/notificationChannelDescriptors/email
gcloud
如要列出 Google Cloud 專案中的所有通知管道描述元,請使用
gcloud beta monitoring channel-descriptors list
指令:gcloud beta monitoring channel-descriptors list
如果執行成功,
list
指令會列出指定專案中的所有管道描述項。舉例來說,email
管道描述元會在清單中顯示如下:--- description: A channel that sends notifications via email. displayName: Email labels: - description: An address to send email. key: email_address name: projects/[PROJECT_ID]/notificationChannelDescriptors/email type: email ---
所有管道描述元都包含下列欄位:
name
:管道描述符的完整資源名稱type
:名稱中用來表示管道類型的部分displayName
:type
欄位的說明,用於顯示目的description
:頻道的簡短說明labels
:一組管道類型專屬的欄位。每個管道類型都有一組專屬的標籤。
建立管道時,也會取得
enabled
欄位,且預設值為true
。如要列出單一管道描述元,請改用
gcloud beta monitoring channel-descriptors describe
,並指定管道描述元的名稱。您不需要指定完整的名稱。舉例來說,這兩個指令都會傳回上述清單:gcloud beta monitoring channel-descriptors describe email gcloud beta monitoring channel-descriptors describe projects/[PROJECT_ID]/notificationChannelDescriptors/email
詳情請參閱
gcloud beta monitoring channel-descriptors list
和describe
參考資料。describe
指令與 API 中的notificationChannelDescriptors.get
方法對應。建立通知管道
您可以使用 Google Cloud CLI,從 JSON 或 YAML 檔案建立 Google Cloud 專案的通知管道,也可以透過程式建立。
如要建立通知管道,您必須提供描述元內的欄位值。這些值,例如
type
,在所有通知管道描述元內,大多很常見;請參閱notificationChannelDescriptors
。每個描述元也包含標籤組合,這個組合會因描述元而不盡相同。如要查看特定描述元的標籤組合,請使用「列出通知管道類型」一文中所述的
gcloud beta monitoring channel-descriptors describe
指令,擷取描述元。例如,擷取email
管道描述元會顯示單一標籤:labels: - description: An address to send email. key: email_address
pubsub
管道描述元也包含單一標籤,該標籤會識別 Pub/Sub 主題。不過,管道可以包含多個標籤。舉例來說,slack
管道描述元有兩個標籤:labels: - description: A permanent authentication token provided by Slack. This field is obfuscated by returning only a few characters of the key when fetched. key: auth_token - description: The Slack channel to which to post notifications. key: channel_name
擷取
webhook_basicauth
管道描述元會顯示數個標籤:labels: - description: The password. The field is obfuscated when the channel is fetched. key: password - description: The public URL to which to publish the webhook. key: url - description: The username. key: username
無論您是透過程式,或是從指令列建立新管道,規格中的
type
值必須符合對應通知管道描述元中的type
欄位。任何必要的標籤鍵也都必須與管道描述元中的標籤鍵相符。某些標籤與用來向供應商驗證的憑證對應。建立管道時,必須從供應商處取得這些標籤的值。取得憑證可能涉及使用供應商網站上的 API 鍵產生頁面,或完成供應商的 OAuth 登入流程。有關如何取得此類憑證的方式,將視特定的供應商而有所差異。
例如,下列顯示採用 JSON 的新
pubsub
通知管道規格:{ "type": "pubsub", "displayName": "Notifications", "description": "Pub/Sub channel for notifications", "labels": { "topic": "projects/[PROJECT_ID]/topics/notificationTopic" }, }
type
值 (pubsub
) 和單一標籤鍵 (topic
) 符合對應管道描述元中的type
和labels.key
欄位。管道預設為啟用。如要建立非活動管道,您可以加入
enabled
欄位,並將值設為false
。下列範例說明如何建立通知管道。
gcloud
如要在 Google Cloud 專案中建立通知管道,請使用
gcloud beta monitoring channels create
指令。如要從檔案載入管道,請使用--channel-content-from-file
標記指定檔案。以下範例會從
pubsub-channel.json
檔案建立新的 Pub/Sub 管道:gcloud beta monitoring channels create --channel-content-from-file="pubsub-channel.json"
如果成功,這個指令會傳回新管道的名稱,例如:
Created notification channel [projects/[PROJECT_ID]/notificationChannels/1355376463305411567].
詳情請參閱
gcloud beta monitoring channels create
參考資料。C#
如要向 Monitoring 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。
Node.js
如要向 Monitoring 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證機制」。
Go
如要向 Monitoring 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證機制」。
Java
如要向 Monitoring 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。
PHP
如要向 Monitoring 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證機制」。
Python
如要向 Monitoring 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。
範例:建立 Slack 通知管道
如要為 Slack 應用程式設定通知管道,請按照下列步驟操作:
設定 Slack 應用程式:
- 如果您還沒有 Slack 應用程式,請按照 Slack 參考文件建立應用程式,並將其安裝到工作區。
- 使用
chat:write
和chat:write.public
的 OAuth 範圍設定 Slack 應用程式。 - 複製應用程式的機器人使用者 OAuth 權杖。
建立定義通知管道設定的檔案。在
auth_token
鍵的值為 Slack 應用程式機器人使用者 OAuth 權杖的情況下,加入標籤。例如:{ "description": "A Slack notification channel", "displayName": "Slack", "type": "slack", "enabled": true, "labels": { "auth_token": "OAUTH_TOKEN_VALUE", "channel_name": "SLACK_CHANNEL_NAME" } }
執行下列指令建立通知管道:
gcloud beta monitoring channels create --channel-content-from-file="FILE_NAME"
列出專案中的通知管道
如要擷取 Google Cloud 專案中所有的通知管道,請使用
notificationChannels.list
方法。這個方法也支援filter
和orderBy
選項,可限制及排序結果;詳情請參閱「排序與篩選」。如果您要尋找特定管道且知道管道名稱,則可以使用
notificationChannels.get
方法只擷取這個管道。管道的名稱格式為projects/[PROJECT_ID]/notificationChannels/[CHANNEL_ID]
,例如:projects/[PROJECT_ID]/notificationChannels/1355376463305411567
當您擷取管道時,像驗證憑證與 API 金鑰這一類敏感值可能會基於安全理由而模糊處理。 如果您要透過複製現有管道的方式來建立新管道,任何模糊處理的值都需要修正。
gcloud
如要列出 Google Cloud 專案中的所有通知管道,請使用
gcloud beta monitoring channels list
指令:gcloud beta monitoring channels list
如果成功,
list
指令會提供指定專案中所有管道的清單。舉例來說,上述指令可能會傳回包含下列項目的清單:--- description: E-mail channel created by gcloud as a test displayName: test e-mail channel enabled: false labels: email_address: user@example.com name: projects/[PROJECT_ID]/notificationChannels/1355376463305411567 type: email --- description: Pub/Sub channel for notifications displayName: Notifications enabled: true labels: topic: projects/[PROJECT_ID]/topics/notificationTopic name: projects/[PROJECT_ID]/notificationChannels/1355376463305411567 type: pubsub
如要列出單一管道,請改用
gcloud beta monitoring channels describe
,並指定管道名稱。例如,這個指令會傳回上述清單中顯示的 Pub/Sub 管道:gcloud beta monitoring channels describe projects/[PROJECT_ID]/notificationChannels/1355376463305411567
詳情請參閱
gcloud beta monitoring channels list
和describe
參考資料。describe
指令與 API 中的notificationChannels.get
方法對應。C#
如要向 Monitoring 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證機制」。
Node.js
如要向 Monitoring 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。
Go
如要向 Monitoring 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。
Java
如要向 Monitoring 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。
PHP
如要向 Monitoring 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證機制」。
Python
如要向 Monitoring 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。
從專案中刪除通知管道
如要刪除 Google Cloud 專案中的通知管道,請使用
notificationChannels.delete
方法,並提供要刪除的通知管道名稱。管道的名稱是NotificationChannel
例項中name
欄位的值,而非displayName
。管道的名稱格式為projects/[PROJECT_ID]/notificationChannels/[CHANNEL_ID]
,例如:projects/[PROJECT_ID]/notificationChannels/1355376463305411567
根據預設,如果您嘗試刪除警告政策參照的管道,該管道將不會遭到刪除。如要強制從警告政策中移除參照項目並刪除管道,請將
force
選項設為true
。這個選項會自動從所有參照政策中移除管道。gcloud
如要刪除通知管道,請使用
gcloud beta monitoring channels delete
,並指定要刪除的管道名稱。舉例來說,下列指令會刪除在其他範例中建立的email
管道:gcloud beta monitoring channels delete projects/[PROJECT_ID]/notificationChannels/1355376463305411567
詳情請參閱
gcloud beta monitoring channels delete
參考資料。C#
如要向 Monitoring 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證機制」。
Go
如要向 Monitoring 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。
Java
如要向 Monitoring 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證機制」。
PHP
如要向 Monitoring 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。
Node.js
如要向 Monitoring 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。
Python
如要向 Monitoring 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。
修改通知管道
如要修改通知管道,請使用
notificationChannels.patch
方法 (在 REST API 中)。其他 API 實作和 Google Cloud CLI 會將這個方法稱為update
,而不是patch
。更新作業可以完全取代現有管道,也可以修改欄位子集。例如,您可以啟用及停用管道。停用管道後,系統就不會將通知傳送至該管道。如果變更是暫時性的,通常停用管道比從參照該管道的快訊政策中移除管道更方便。
gcloud
如要啟用已停用的通知管道,請使用
gcloud beta monitoring channels update
指令,並提供--enabled
標記。下列指令會啟用先前範例中建立的email
通知管道 (處於停用狀態):gcloud beta monitoring channels update projects/[PROJECT_ID]/notificationChannels/1355376463305411567 --enabled
如要停用政策,請使用相同指令並提供
--no-enabled
標記。詳情請參閱gcloud beta monitoring channels update
參考資料。update
指令與 REST API 中的notificationChannels.patch
方法對應。C#
如要向 Monitoring 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。
Go
如要向 Monitoring 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。
Node.js
如要向 Monitoring 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。
Java
如要向 Monitoring 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證機制」。
PHP
如要向 Monitoring 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。
Python
如要向 Monitoring 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。
查看通知管道記錄
您可以使用記錄檔探索工具查看通知管道錯誤:
-
前往 Google Cloud 控制台的「Logs Explorer」頁面:
如果您是使用搜尋列尋找這個頁面,請選取子標題為「Logging」的結果。
輸入並執行查詢。如要查看通知管道錯誤的專屬查詢,請參閱「Cloud Monitoring 查詢」。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-06-20 (世界標準時間)。
-