def main(event, context): """Background Cloud Function to be triggered by Pub/Sub. Args: event (dict): The dictionary with data specific to this type of event. The `data` field contains the PubsubMessage message. The `attributes` field will contain custom attributes if there are any. context (google.cloud.functions.Context): The Cloud Functions event metadata. The `event_id` field contains the Pub/Sub message ID. The `timestamp` field contains the publish time. """ print("""This Function was triggered by messageId {} published at {} """.format(context.event_id, context.timestamp)) import base64 import json if 'data' in event: data = json.loads(base64.b64decode(event['data']).decode('utf-8')) if 'incident' in data: from datadog_api_client.v1 import ApiClient, Configuration from datadog_api_client.v1.api.events_api import EventsApi from datadog_api_client.v1.model.event_alert_type import EventAlertType from datadog_api_client.v1.model.event_create_request import EventCreateRequest cluster = data["incident"]["resource_name"] body = EventCreateRequest( title=data["incident"]["condition_name"], text=data["incident"]["summary"], tags=["service:bigtable", "platform:delphi", "project:api", f"cluster:{cluster}", "environment:production"], source_type_name="google cloud bigtable", alert_type=EventAlertType("error") ) configuration = Configuration() with ApiClient(configuration) as api_client: api_instance = EventsApi(api_client) response = api_instance.create_event(body=body) print(response) else: raise Exception('expected incident key not found in data') else: raise Exception('data is missing')