symfony-messenger icon indicating copy to clipboard operation
symfony-messenger copied to clipboard

Question about the DetailType parameter

Open dcorrea777 opened this issue 1 year ago • 3 comments

In the send method of the EventBridgeTransport class I noticed that we don't have control over the DetailType parameter, and this parameter is very important in defining the EventBridge rules.

I noticed it's not on version 1.0 yet, I'd like to know what your plans are.

dcorrea777 avatar Jul 11 '23 14:07 dcorrea777

Could you clarify what you mean? I don't understand.

mnapoli avatar Jul 15 '23 03:07 mnapoli

Sorry for not being clear. @mnapoli .

I installed this package to test the following scenario:

My application publishes a message to my custom EventBus on AWS using the EventBridge service.

Using the AWS SDK to publish a message on the Bus, it is possible to do as follows:

// SDK AWS 
// https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-eventbridge-2015-10-07.html#putevents
$result = $client->putEvents([
    'EndpointId' => '<string>',
    'Entries' => [ // REQUIRED
        [
            'Detail' => '<string>',
            'DetailType' => '<string>',
            'EventBusName' => '<string>',
            'Resources' => ['<string>', ...],
            'Source' => '<string>',
            'Time' => <integer || string || DateTime>,
            'TraceHeader' => '<string>',
        ],
        // ...
    ],
]);

Note that in the AWS example, it is possible to pass the DetailType parameter and the Source. In your EventBridgeTransport object, I cannot control the DetailType parameter, and it is essential for a consumer to be able to filter a message type.

Your class

    // https://github.com/brefphp/symfony-messenger/blob/master/src/Service/EventBridge/EventBridgeTransport.php
    public function send(Envelope $envelope): Envelope
    {
        $encodedMessage = $this->serializer->encode($envelope);
        $arguments = [
            'Entries' => [
                [
                    'Detail' => json_encode($encodedMessage, JSON_THROW_ON_ERROR),
                    // Ideally here we could put the class name of the message, but how to retrieve it?
                    'DetailType' => 'Symfony Messenger message',
                    'Source' => $this->source,
                ],
            ],
        ];

        if ($this->eventBusName) {
            $arguments['Entries'][0]['EventBusName'] = $this->eventBusName;
        }

        try {
            $result = $this->eventBridge->putEvents($arguments);
            $failedCount = $result->getFailedEntryCount();
        } catch (Throwable $e) {
            throw new TransportException($e->getMessage(), 0, $e);
        }

        if ($failedCount > 0) {
            foreach ($result->getEntries() as $entry) {
                $reason = $entry->getErrorMessage() ?? 'no reason provided';
                throw new TransportException("$failedCount message(s) could not be published to EventBridge: $reason.");
            }
        }

        return $envelope;
    }

dcorrea777 avatar Jul 16 '23 16:07 dcorrea777

Ahh ok, thanks for explaining.

There are no specific plans for now, it's open to ideas. I'm happy to merge pull requests or be hired to work on this topic (I don't have an immediate need for this feature personally).

mnapoli avatar Jul 17 '23 05:07 mnapoli