1

I a writing a Python Cloud Function and I would like to retrieve the "delivery_attempt" attribute.

Based on the documentation, the Cloud Function gets only 2 parameters: event (of type PubsubMessage) and context.

def hello_pubsub(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 `@type` field maps to
                         `type.googleapis.com/google.pubsub.v1.PubsubMessage`.
                        The `data` field maps to the PubsubMessage data
                        in a base64-encoded string. The `attributes` field maps
                        to the PubsubMessage attributes if any is present.
         context (google.cloud.functions.Context): Metadata of triggering event
                        including `event_id` which maps to the PubsubMessage
                        messageId, `timestamp` which maps to the PubsubMessage
                        publishTime, `event_type` which maps to
                        `google.pubsub.topic.publish`, and `resource` which is
                        a dictionary that describes the service API endpoint
                        pubsub.googleapis.com, the triggering topic's name, and
                        the triggering event type
                        `type.googleapis.com/google.pubsub.v1.PubsubMessage`.
    Returns:
        None. The output is written to Cloud Logging.
    """

How can I retrieve the delivery_attempt on a message?

poiuytrez
  • 21,330
  • 35
  • 113
  • 172

1 Answers1

1

When trigging a Cloud Function via Pub/Sub, one does not have access to the delivery attempt field. Instead, the way to get access is to set up an HTTP-based Cloud Function and use the trigger URL as the push endpoint in a subscription you create separately.

Then, you can access the delivery attempt as follows:

def hello_world(request):
    """Responds to any HTTP request.
    Args:
        request (flask.Request): HTTP request object.
    Returns:
        The response text or any set of values that can be turned into a
        Response object using
        `make_response <http://flask.pocoo.org/docs/1.0/api/#flask.Flask.make_response>`.
    """
    request_json = request.get_json()
    print(request_json["deliveryAttempt"])
Arnaud P
  • 12,022
  • 7
  • 56
  • 67
Kamal Aboul-Hosn
  • 15,111
  • 1
  • 34
  • 46
  • Is there any intention to add this capability to PubSub triggered functions? It's not really convenient to convert functions to http triggered to be able to retrieve this field. – Pavelr Oct 25 '21 at 14:21