Access Layer Metrics

New in version 5.23

You can access performance metrics directly in the request. This can be useful for logging purposes or when returning this information via the HTTP Server-Timings header to clients for integration with Real-User-Monitoring (RUM) tools.

The method to call is \Tideways\Profiler::getLayerMetrics and it returns an array of \Tideways\Profiler\LayerMetric objects with the following class structure:

<?php

namespace Tideways\Profiler;

class LayerMetric
{
    public string $name;
    public int $wallTimeMicroseconds;
}

Example: Server-Timing Integration with Symfony

<?php

use Symfony\Component\HttpKernel\Event\ResponseEvent;

#[AsEventSubscriber(event: 'kernel.response')]
class TidewaysServerTimingListener
{
    public function __invoke(ResponseEvent $event)
    {
        if (!method_exists('Tideways\Profiler', 'getLayerMetrics')) {
            return;
        }

        $response = $event->getResponse();
        $layers = \Tideways\Profiler::getLayerMetrics(),

        // filter and show only "sql" and "http" query metrics.
        $layers = array_filter(
            $layers,
            static fn (\Tideways\Profiler\LayerMetric $metric) => in_arary($metric->name, ['sql', 'http']),
        );

        $serverTiming = implode(
            ', ',
            array_map(
                static fn (\Tideways\Profiler\LayerMetric $metric) => \sprintf("tideways.layer.%s;dur=%g", $metric->name, $metric->wallTimeMicroseconds / 1000),
                $layers,
            ),
        );
        $response->headers->set('Server-Timing', $serverTiming);
    }
}

Known Layers

The following layers are returned from the API:

  • apcu

  • autoloading

  • compiling

  • disk

  • dns

  • elasticsearch

  • email

  • gc (Garbage Collection)

  • http

  • kafka

  • beanstalk

  • mongodb

  • redis

  • shell

  • sleep

  • rdbms

  • sqlite

  • memcache

  • runq

  • amqp

Still need help? Email [email protected]