Google Cloud Pub/Sub Send Message Example

Overview

The following example demonstrates how to send a message to a Google Cloud Pub/Sub topic using a GCP Pub/Sub webhook.

The example invokes the Google Cloud Pub/Sub API projects.topics.publish method.

Configuration

Create a new GCP Pub/Sub webhook or import the template used in this example. To import the XML template file, open the Alerts > Outgoing Webhooks page, select Import in the split-button located below the table and complete the upload form.

To create a new notification, open the Alerts > Outgoing Webhooks page and click Create.

Parameters

Enter a name and specify the following parameters:

Name Value
Project ID <PROJECT_ID>
Topic <TOPIC>
Service Account <SERVICE_ACCOUNT>
Private Key Alias <PRIVATE_KEY_ALIAS>
Message <MESSAGE_TEXT>

Modify the Project ID by replacing the <PROJECT_ID> value with your project ID, for example:

charged-thought-189815

Modify the Topic by replacing the <TOPIC> value with your topic name, for example:

my-topic

Modify the Service Account by replacing the <SERVICE_ACCOUNT> value with your service account, for example:

publisher

Modify the Private Key Alias by selecting the alias for imported account private key, for example:

gcp_publisher

Modify the Message** by replacing the <MESSAGE_TEXT> value with your text, for example:

{
   "status": "${status}",
   "entity": "${entity}",
   "rule": "${rule}",
   "tags": "${tags}",
   "message": ""
}

The Message text contains placeholders that are substituted with actual values when the webhook is triggered. The placeholders specified in the message are visible as editable parameters in the rule editor.

Rule

Create a new rule or import the rule template used in this example. To import the XML template file, open the Alerts > Rules page, select Import in the split-button located below the table and complete the upload form.

To create a new rule, open the Alerts > Rules page and click Create.

Specify the key settings on the Overview tab.

Name Value
Status Enabled
Metric test_m
Condition value > 1

Open the Webhooks tab.

Set Enabled to Yes and choose the previously created webhook from the Endpoint drop-down.

Enable Open, Repeat and Cancel triggers.

Specify the message text into the Message parameter for all triggers.

Test

Test the integration by submitting a sample series command on the Data > Data Entry page.

series e:test_e m:test_m=2

The value causes the condition to evaluate to true, which in turn triggers the notification. To verify that an alert is raised, open Alerts > Open Alerts page and check that an alert for the test_m metric is present in the Alerts table.

Check the Google Cloud Pub/Sub topic statistics to ensure the new messages arrive.