gizmo
gizmo copied to clipboard
Allow to specify the MessageAttributes when publishing an event
In order to be able to use the attribute based filtering, would be nice if the Publisher
allowed to specify the MessageAttributes field in sns.PublishInput
so basically have something like:
// PublishRaw will emit the byte array to the SNS topic.
// The key will be used as the SNS message subject.
func (p *publisher) PublishRaw(_ context.Context, key string, m []byte, messageAttributes map[string]*MessageAttributeValue) error {
msg := &sns.PublishInput{
TopicArn: &p.topic,
Subject: &key,
Message: aws.String(base64.StdEncoding.EncodeToString(m)),
MessageAttributes: messageAttributes,
}
_, err := p.sns.Publish(msg)
return err
}
ref. https://aws.amazon.com/blogs/compute/simplify-pubsub-messaging-with-amazon-sns-message-filtering/
Howdy @alessandrozucca!
Sorry for the delay, I've been thinking about this one since you posted.
While it does seem handy, I do worry about this leading to a leaky abstraction as we've been trying to find the lowest common denominator between SNS, Kafka & Google PubSub.
I do see that PubSub also has attributes but I haven't found something similar in the Kafka world so I'm a bit hesitant to alter our main interface.
Perhaps we can make a new Publisher interface (similar to the MultiPublisher
) and maybe it just wont be implemented for Kafka?
Hi @jprobinson
Sorry for the delay in the response and happy new year 🎉
I understand your reason for not wanting to change the main interface.
Creating a new Publisher that allows to specify the message attributes makes sense.
In the meanwhile, to get this working, I have already added in a local package an interface (with a third argument map[string]*sns.MessageAttributeValue
) and copied your implementation of the publisher
allowing the MessageAttibute to be specified.
I get the reason for not adding it, but I would love to see this added in somehow. I have taken a similar approach to @alessandrozucca and implemented this in a local package.
@jprobinson what if attributes will be passed via context, so there is no need to change the interface and context will be optionally handled?
Something like:
if v, ok := ctx.Value("MessageAttributes").(map[string]*sns.MessageAttributeValue); ok {
msg.MessageAttributes = v
}
Think this issue can be closed - I have just noticed that this has been implemented 😄
@Trane9991 is possible to do similar thing for google pub/sub? I want to set attribute for my event which will be sent to google pub/sub
if v, ok := ctx.Value("MessageAttributes").(map[string]*sns.MessageAttributeValue); ok {
msg.MessageAttributes = v
}