Skip to content
Search

Last updated

Symfony Messenger monitoring: connect the worker to the request

Let's say our checkout answers the customer at once and sends the receipt later: the controller dispatches a message, and a worker handles it. A customer says the receipt never came. We know the request, and we have to find what the worker did with it.

These steps need a Symfony application, a queue and a collector, so they run only on your machine, not in the playground.

Monitor a Messenger handler with a log line

The first idea is always a log line at the end of the handler.

<?php

declare(strict_types=1);

namespace App\MessageHandler;

use App\Message\SendReceipt;
use Psr\Log\LoggerInterface;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;

#[AsMessageHandler]
final readonly class SendReceiptHandler
{
    public function __construct(private LoggerInterface $logger)
    {
    }

    public function __invoke(SendReceipt $message): void
    {
        usleep(300_000);

        $this->logger->info('receipt sent', ['order' => $message->orderId]);
    }
}

It works: every receipt leaves a line in the log of the worker, when the handler gets to its last line.

Why a log line in the handler is not enough

  • A message that was never handled writes nothing. The log can't tell "not handled yet" from "lost".
  • The line doesn't say how long the handler took, or how long the message waited in the queue.
  • The line is in the log of the worker, and the request is in the log of the web server. Nothing connects the two, so we search by order id and hope it is in both.
  • Every new handler needs its own line, and somebody forgets it.

We want Messenger itself to tell us, for every message: who sent it, when it was handled, and how that went.

Trace Symfony Messenger with OpenTelemetry

flow-php/telemetry is an independent implementation of OpenTelemetry, written in PHP, and it is not a wrapper around the official SDK. The bundle needs PHP 8.3 or newer and Symfony 6.4, 7.4 or 8.0. The only PHP extension it needs is curl.

composer require flow-php/symfony-telemetry-bundle flow-php/telemetry-otlp-bridge

The guide about Symfony monitoring with OpenTelemetry shows the installation step by step. For Messenger we turn on one more line:

# config/packages/flow_telemetry.yaml
flow_telemetry:
  resource:
    custom:
      service.name: shop

  exporters:
    collector:
      otlp:
        transport:
          type: curl
          endpoint: 'http://localhost:4318'
          timeout_ms: 200
          connect_timeout_ms: 50

  tracer_provider:
    processor:
      type: batching
      exporter: collector

  meter_provider:
    processor:
      type: batching
      exporter: collector

  instrumentation:
    http_kernel:
      enabled: true
    messenger:
      enabled: true

The controller and the handler stay as they are. The bundle adds itself to every message bus.

What the request sends

We post one checkout. The collector gets the trace of the request, here with only the names and ids:

Trace ID  07ced0cc1a3584be11025543741da7a1

POST /checkout                                Kind: Server    ID: ec125274fdb858b4
  App\Controller\CheckoutController::__invoke Kind: Internal  ID: eef20d1f4829042b
    send async                                Kind: Producer  ID: 4a6bd57021797cb1
      flow.messenger.message.class = App\Message\SendReceipt
      messaging.destination.name   = async
      messaging.message.id         = 1

send async is new. It says that this request put a SendReceipt message on the async transport, and it has the id of the message.

What the worker sends

Now we start the worker:

php bin/console messenger:consume async
Trace ID  a423844581b2eb396beed59f2692a045

process async                                 Kind: Consumer  ID: da4ea95eb2275fdb
  flow.messenger.message.class = App\Message\SendReceipt
  messaging.message.id         = 1
  Link -> Trace ID: 07ced0cc1a3584be11025543741da7a1  Span ID: 4a6bd57021797cb1

The worker has its own trace, and the last line is the answer to our customer: a link to the trace and the span that sent the message. A monitoring tool shows the link on both sides. From the request we jump to the work of the worker, and from a failed handler back to the request that asked for it.

It is a link and not one long trace on purpose. A message can wait for hours, and a batch of a thousand messages has a thousand different senders. A trace that waits that long is hard to read, and a link has none of these problems.

The bundle carries the ids for us: it puts them into a stamp on the envelope when the message is sent, and the worker reads the stamp. It works with every transport that keeps stamps, and we never touch a header.

When the handler throws, the process span has the status ERROR and the exception, so "the receipt never came" has a place to look.

Count the messages

With meter_provider in the configuration the bundle also counts:

Metric What it holds
messaging.client.sent.messages Messages sent, for each transport
messaging.client.consumed.messages Messages handled, for each transport
messaging.process.duration How long the handlers take, as a histogram

A monitoring tool draws sent against consumed. When the first line grows faster than the second one, the queue fills up, and we see it before a customer does.

What to set for Messenger tracing

All of these are options of messenger, and none is needed to start:

Option Default What it does
context_propagation true Carries the trace ids in the message
trace true The send and process spans
metrics true The three metrics above
span_naming transport Names spans by the transport, or by message_name or message_fqcn

Where to go from here

To keep only the failed and the slow messages, read about tail sampling in the OpenTelemetry Collector for PHP. The same bundle shows the SQL queries of a handler: see find slow Doctrine queries in Symfony. All the options are in the bundle documentation. The log lines of a handler carry the trace id of the worker: see add a trace id to Symfony logs.

Found a typo or an outdated section? Edit this guide on GitHub

scroll back to top