Reputation: 25
I'm new to the GCP environment.
Scenario: I have Created a Cloud Function which triggers Based on Message Published to the Topic.
Condition: Every Message Published Compulsory to have. Message body and 2 Attributes init.
Cloud Function Should check whether the message and Attribute are there (Non-empty). If any of the above are empty Cloud Function must fail Raise an Email Alert.
**Sample Code**
import base64
import sys
def hello_pubsub(event, context):
pubsub_message = base64.b64decode(event['data']).decode('utf-8')
print(pubsub_message)
pubsub_attributes = event['attributes']
transaction_id = event['attributes']['transaction_id']
print(transaction_id)
refined_paths = event['attributes']['refined_paths']
print(refined_paths)
pubsub_timestamp = context.timestamp
#Check whether variable are set or not.
pubsub_message_status = bool(pubsub_message)
transaction_id_status = bool(transaction_id)
refined_paths_status = bool(refined_paths)
if(pubsub_message_status == False or transaction_id_status == False or refined_paths_status == False):
raise RuntimeError("Message Content and Attribute are Empty")
I tried this way Cloud Function is Deployed Preroroperly but NameError is Raised (Adding Photos of CF logs
Upvotes: 1
Views: 2202
Reputation: 1420
You can use Cloud Monitoring to set up email alerting based on a Pub/Sub message output:
To create a notification channel by using the Cloud Console, do the following:
- In the Cloud Console, use the project picker to select your Google Cloud project, and then select Monitoring, or click the following button:
Go to Monitoring
- In the Monitoring navigation pane, click notifications Alerting.
Click Edit notification channels.
To add a new notification channel, locate the channel type, click Add new, and then follow the channel-specific instructions contained in the following table:
refer to the Pub/Sub table
Upvotes: 0
Reputation: 75715
Yes, you need to perform the business check in your code. When you find something wrong, write a log entry.
Then, create log based metrics on this specific log entry and an alert on this metric
If the alerting isn't a concern, you can also add a filter on PubSub subscription (only at creation time) to only receive the valid messages (with the 2 attributes that you want, body content can't be checked)
Upvotes: 1