From 3734f190f8f643aaed3bae99a722bae5212ccb2a Mon Sep 17 00:00:00 2001 From: Mikhail Popov Date: Fri, 22 Jul 2022 14:08:34 +0300 Subject: [PATCH] Add the ability to set or redefine attributes for messages in Rabbit. (#1724) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit One of content_type, content_encoding, message_id, user_id, app_id, delivery_mode, priority, timestamp, expiration, type or reply_to, headers. Co-authored-by: Попов Михаил --- src/Monolog/Handler/AmqpHandler.php | 35 +++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/src/Monolog/Handler/AmqpHandler.php b/src/Monolog/Handler/AmqpHandler.php index e30d784d..c0b4504e 100644 --- a/src/Monolog/Handler/AmqpHandler.php +++ b/src/Monolog/Handler/AmqpHandler.php @@ -27,6 +27,29 @@ class AmqpHandler extends AbstractProcessingHandler * @var AMQPExchange|AMQPChannel $exchange */ protected $exchange; + /** @var array */ + private $extraAttributes = []; + + /** + * @return array + */ + public function getExtraAttributes(): array + { + return $this->extraAttributes; + } + + /** + * @param array $extraAttributes One of content_type, content_encoding, + * message_id, user_id, app_id, delivery_mode, + * priority, timestamp, expiration, type + * or reply_to, headers. + * @return AmqpHandler + */ + public function setExtraAttributes(array $extraAttributes): self + { + $this->extraAttributes = $extraAttributes; + return $this; + } /** * @var string @@ -60,14 +83,18 @@ class AmqpHandler extends AbstractProcessingHandler $routingKey = $this->getRoutingKey($record); if ($this->exchange instanceof AMQPExchange) { + $attributes = [ + 'delivery_mode' => 2, + 'content_type' => 'application/json', + ]; + if ($this->extraAttributes) { + $attributes = array_merge($attributes, $this->extraAttributes); + } $this->exchange->publish( $data, $routingKey, 0, - [ - 'delivery_mode' => 2, - 'content_type' => 'application/json', - ] + $attributes ); } else { $this->exchange->basic_publish(