Google Pubsub - how to add/update custom attributes before NACK()

895 views Asked by At

Google pubsub allows to add custom attributes to messages before publishing them, e.g.:

{
  data: {
    something: "value"
  },
  attributes: {
    attribute1: "value"
  }
};

When receiving a message in a subscription, you must ack() or nack() the message to allow the broker knowing if the message was acknowledged or if it must be removed from the inventory and scheduled to be redelivered.

I would like to update the custom attributes in the message before nack() it, adding a property called ErrorCode - so, after X retries, the message should be automatically published to the DLQ topic with the ErrorCode in its attributes.

However, when I update the message attributes and nack() it, the next time the subscriber receives the same message the attributes don't get updated:

message.attributes = {ErrorCode: "123"}    
message.nack();

In this example, the value of attributes in the message keeps having only the property attribute1 in the next time the subscriber receives the message.

Does someone know how the custom attributes can be updated for the current message? If it is not possible (it seems that it is the case), what would be the thoughts about a solution for this?

1

There are 1 answers

0
Kamal Aboul-Hosn On BEST ANSWER

Attributes in a message are immutable once the message has been published. If you are changing the attributes, you are only changing them in the local copy. If you want to change the attributes in a message when it gets sent to a dead letter topic, you'd instead have to do the process manually, publishing to this other topic yourself instead of depending on Pub/Sub automatic dead letter capabilities.