Skip to content

Commit

Permalink
Add the ability to set or redefine attributes for messages in Rabbit. (
Browse files Browse the repository at this point in the history
…#1724)

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: Попов Михаил <popov.mv@betcity.ru>
  • Loading branch information
pmikle and Попов Михаил authored Jul 22, 2022
1 parent 197f534 commit 3734f19
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions src/Monolog/Handler/AmqpHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -60,14 +83,18 @@ protected function write(array $record): void
$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(
Expand Down

0 comments on commit 3734f19

Please sign in to comment.