#Symfony monitoring with OpenTelemetry: requests, commands and SQL queries
Let's say our shop runs on Symfony. On our machine the web profiler shows every query and every millisecond of a request. In production somebody says "the price page is slow since Monday", and we have nothing to look at.
These steps need a Symfony application and a collector, so they run only on your machine, not in the playground.
#The Symfony profiler in production
The profiler is the first thing we think of, and its own configuration says where it lives:
# config/packages/web_profiler.yaml
when@dev:
web_profiler:
toolbar: true
framework:
profiler:
collect_serializer_data: true
when@dev is there for a reason. The profiler stores everything about every request, and it shows the whole application to anybody who finds its address.
It also knows only the last requests of one server.
#What we need in production
- Something that is always on and costs almost nothing.
- One record for every request and every console command: how long it took, and what happened inside.
- The record leaves the server, so we can read it later, from all servers in one place.
That record is a trace, and the standard for it is OpenTelemetry. Every monitoring tool reads it.
#Add OpenTelemetry to a Symfony application
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 bundle has no Flex recipe, so we add it to the list ourselves:
// config/bundles.php
return [
// ...
Flow\Bridge\Symfony\TelemetryBundle\FlowTelemetryBundle::class => ['all' => true],
];
After the installation nothing is recorded yet. We turn on each part we want, so the first configuration has three parts: where the spans go, how they get there, and what is traced.
# config/packages/flow_telemetry.yaml
flow_telemetry:
resource:
custom:
service.name: shop
exporters:
terminal:
console: ~
tracer_provider:
processor:
type: passthrough
exporter: terminal
instrumentation:
console:
enabled: true
dbal:
enabled: true
#Trace a Symfony console command
The console exporter prints to the terminal, so we start with a command. The shop has one that prints the three most expensive categories.
php bin/console app:price-report
Between the lines of the command we now see its spans. These are two of them, with the long lines cut:
| SPAN: SELECT product
| Trace: 805ffb8ac2a9bdabe5dec5ff854453b7 Span: b4a5ce6e23cd8f33 Parent: 9914660346568e80
| Kind: CLIENT Status: UNSET Duration: 244.26ms
| db.query.text = SELECT category_id, AVG(price) AS average FROM product GROUP BY category_id ORDER BY average DESC LIMIT 3
category 50: 2524.00
category 49: 2523.00
category 48: 2522.00
| SPAN: app:price-report
| Trace: 805ffb8ac2a9bdabe5dec5ff854453b7 Span: 9914660346568e80
| Kind: INTERNAL Status: UNSET Duration: 248.65ms
| flow.symfony.command.name = app:price-report
| process.exit.code = 0
We didn't change the command. The command took 248ms, and 244ms of it is one SQL query, with its text.
The query is a child of the command: its parent is the span id of app:price-report.
#Trace Symfony requests and Doctrine queries
A request has no terminal, so the spans have to leave the server. They go over OTLP, the protocol of OpenTelemetry, to a collector on the same machine. The guide about the OpenTelemetry Collector for PHP shows how to start one in Docker, and why it is the right place to send to.
We replace the exporter and turn on http_kernel:
# 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
console:
enabled: true
dbal:
enabled: true
Now we open the price page, and the collector prints what it got. Again with the long lines cut:
ScopeTraces #0 flow.symfony.dbal@4.6.0
doctrine.dbal.connection 3c8c4614e380df92919fd4954975696d 03052d8b27b00295 db.system.name=sqlite
SELECT product 3c8c4614e380df92919fd4954975696d 3b023f33295ebdef db.operation.name=SELECT db.collection.name=product db.query.text=SELECT category_id, AVG(price) AS average FROM product GROUP BY category_id ORDER BY average DESC
ScopeTraces #0 flow.symfony.http_kernel@v7.4.25
App\Controller\ProductController::prices 3c8c4614e380df92919fd4954975696d 19297a7024039a87 http.route=prices
GET /prices 3c8c4614e380df92919fd4954975696d b69eab7a3a4971eb http.request.method=GET url.path=/prices http.route=/prices http.response.status_code=200
One request is one trace with four spans: the request, the controller, the connection to the database and the query. The controller is the same as before, and it works with the ORM too: Doctrine sends every query through the same connection.
Three things in the configuration keep the request fast:
batchingkeeps the spans in memory and sends them once, after the response went out to the visitor.- The two timeouts are in milliseconds. A collector that doesn't answer costs at most that, and never an error page.
dbalrecords the SQL with?in place of the values. The data of our customers doesn't leave the database.
#Add your own span and metric
The bundle knows Symfony, and only we know the shop. Telemetry is a service, so we ask for it in the controller.
<?php
declare(strict_types=1);
namespace App\Controller;
use Doctrine\DBAL\Connection;
use Flow\Telemetry\Telemetry;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
final class ProductController extends AbstractController
{
#[Route('/report', name: 'report', methods: ['GET'])]
public function report(Connection $connection, Telemetry $telemetry): Response
{
$telemetry->meter('shop')->createCounter('reports.built', 'reports')->add(1, ['report' => 'price']);
return $telemetry->tracer('shop')->trace('build-price-report', fn (): Response => $this->json(
$connection->fetchAllAssociative(
'SELECT category_id, AVG(price) AS average FROM product GROUP BY category_id ORDER BY average DESC',
),
));
}
}
The collector shows two new lines for this request:
ScopeTraces #0 shop
build-price-report cfe93d89156c7eed0e579cc6961dde63 8dfdcce05d73ef93
ScopeMetrics #0 shop
reports.built{report=price} 1
build-price-report has the same trace id as the request around it and the query inside it. We didn't connect anything, the tracer knows which span is open.
The guides about tracing in PHP and PHP metrics show what else a span and a metric can carry.
#What else the bundle can trace
Each line is one more enabled: true under instrumentation:
| Key | What we get |
|---|---|
messenger |
A span for every message that is sent and every one handled |
twig |
A span for every rendered template |
http_client |
A span for every request of the Symfony HTTP client |
psr18_client |
The same for PSR-18 clients |
cache |
Spans for cache writes, and counters for hits and misses |
security |
The id of the user on the span of the request |
All the options are in the bundle documentation.
#Where to go from here
The collector decides what is kept and where it goes: see the OpenTelemetry Collector for PHP. To find the query that makes a page slow, read find slow Doctrine queries in Symfony in production. For the queue and the logs, read Symfony Messenger monitoring and add a trace id to Symfony logs.