-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Messenger] improve logs #32036
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Messenger] improve logs #32036
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,37 +54,30 @@ public function handle(Envelope $envelope, StackInterface $stack): Envelope | |
|
||
$sender = null; | ||
|
||
try { | ||
if ($envelope->all(ReceivedStamp::class)) { | ||
// it's a received message, do not send it back | ||
$this->logger->info('Received message "{class}"', $context); | ||
} else { | ||
/** @var RedeliveryStamp|null $redeliveryStamp */ | ||
$redeliveryStamp = $envelope->last(RedeliveryStamp::class); | ||
|
||
// dispatch event unless this is a redelivery | ||
$shouldDispatchEvent = null === $redeliveryStamp; | ||
foreach ($this->getSenders($envelope, $redeliveryStamp) as $alias => $sender) { | ||
if (null !== $this->eventDispatcher && $shouldDispatchEvent) { | ||
$event = new SendMessageToTransportsEvent($envelope); | ||
$this->eventDispatcher->dispatch($event); | ||
$envelope = $event->getEnvelope(); | ||
$shouldDispatchEvent = false; | ||
} | ||
|
||
$this->logger->info('Sending message "{class}" with "{sender}"', $context + ['sender' => \get_class($sender)]); | ||
$envelope = $sender->send($envelope->with(new SentStamp(\get_class($sender), \is_string($alias) ? $alias : null))); | ||
if ($envelope->all(ReceivedStamp::class)) { | ||
// it's a received message, do not send it back | ||
$this->logger->info('Received message {class}', $context); | ||
} else { | ||
/** @var RedeliveryStamp|null $redeliveryStamp */ | ||
$redeliveryStamp = $envelope->last(RedeliveryStamp::class); | ||
|
||
// dispatch event unless this is a redelivery | ||
$shouldDispatchEvent = null === $redeliveryStamp; | ||
foreach ($this->getSenders($envelope, $redeliveryStamp) as $alias => $sender) { | ||
if (null !== $this->eventDispatcher && $shouldDispatchEvent) { | ||
$event = new SendMessageToTransportsEvent($envelope); | ||
$this->eventDispatcher->dispatch($event); | ||
$envelope = $event->getEnvelope(); | ||
$shouldDispatchEvent = false; | ||
} | ||
} | ||
|
||
if (null === $sender) { | ||
return $stack->next()->handle($envelope, $stack); | ||
$this->logger->info('Sending message {class} with {sender}', $context + ['sender' => \get_class($sender)]); | ||
$envelope = $sender->send($envelope->with(new SentStamp(\get_class($sender), \is_string($alias) ? $alias : null))); | ||
} | ||
} catch (\Throwable $e) { | ||
$context['exception'] = $e; | ||
$this->logger->warning('An exception occurred while handling message "{class}": '.$e->getMessage(), $context); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just removing the try catch. It's usually not a good idea to both log and re-throw because it created duplicate logs. |
||
} | ||
|
||
throw $e; | ||
if (null === $sender) { | ||
return $stack->next()->handle($envelope, $stack); | ||
} | ||
|
||
// message should only be sent and not be handled by the next middleware | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -140,14 +140,16 @@ private function handleMessage(Envelope $envelope, ReceiverInterface $receiver, | |
|
||
$this->dispatchEvent(new WorkerMessageFailedEvent($envelope, $transportName, $throwable, $shouldRetry)); | ||
|
||
$retryCount = RedeliveryStamp::getRetryCountFromEnvelope($envelope); | ||
if ($shouldRetry) { | ||
$retryCount = $this->getRetryCount($envelope) + 1; | ||
++$retryCount; | ||
$delay = $retryStrategy->getWaitingTime($envelope); | ||
if (null !== $this->logger) { | ||
$this->logger->error('Retrying {class} - retry #{retryCount}.', $context + ['retryCount' => $retryCount, 'error' => $throwable]); | ||
$this->logger->error('Error thrown while handling message {class}. Dispatching for retry #{retryCount} using {delay} ms delay. Error: "{error}"', $context + ['retryCount' => $retryCount, 'delay' => $delay, 'error' => $throwable->getMessage(), 'exception' => $throwable]); | ||
} | ||
|
||
// add the delay and retry stamp info + remove ReceivedStamp | ||
$retryEnvelope = $envelope->with(new DelayStamp($retryStrategy->getWaitingTime($envelope))) | ||
$retryEnvelope = $envelope->with(new DelayStamp($delay)) | ||
->with(new RedeliveryStamp($retryCount, $this->getSenderClassOrAlias($envelope))) | ||
->withoutAll(ReceivedStamp::class); | ||
|
||
|
@@ -157,7 +159,7 @@ private function handleMessage(Envelope $envelope, ReceiverInterface $receiver, | |
$receiver->ack($envelope); | ||
} else { | ||
if (null !== $this->logger) { | ||
$this->logger->critical('Rejecting {class} (removing from transport).', $context + ['error' => $throwable]); | ||
$this->logger->critical('Error thrown while handling message {class}. Removing from transport after {retryCount} retries. Error: "{error}"', $context + ['retryCount' => $retryCount, 'error' => $throwable->getMessage(), 'exception' => $throwable]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. including the exception message makes sure you can see it without having to run in |
||
} | ||
|
||
$receiver->reject($envelope); | ||
|
@@ -207,14 +209,6 @@ private function shouldRetry(\Throwable $e, Envelope $envelope, RetryStrategyInt | |
return $retryStrategy->isRetryable($envelope); | ||
} | ||
|
||
private function getRetryCount(Envelope $envelope): int | ||
{ | ||
/** @var RedeliveryStamp|null $retryMessageStamp */ | ||
$retryMessageStamp = $envelope->last(RedeliveryStamp::class); | ||
|
||
return $retryMessageStamp ? $retryMessageStamp->getRetryCount() : 0; | ||
} | ||
|
||
private function getSenderClassOrAlias(Envelope $envelope): string | ||
{ | ||
/** @var SentStamp|null $sentStamp */ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed quotes for consistency. They are not useful in logs because in cli they are colored due to the context and the class is a very fixed string anyway without whitespace.