Skip to content

odandb/monitoring-metrics-bundle

Repository files navigation

Monitoring Metrics Bundle

Introduction

This bundle provides some metric in the response header and additional assertions for the functional tests.

It collects the following metrics :

  • Number of Doctrine Query
  • Memory Usage

Install

Step 1: Download the Bundle

Add this repositories section on your composer.json project.

"repositories": [
    {
        "type": "vcs",
        "url": "git@dev.odandb.com:odandb/monitoring-metrics-bundle.git"
    }
],

And run

composer require odandb/monitoring-metrics-bundle --dev

Caution : This bundle must be installed only in dev or/and test environment.

Step 2: Enable the Bundle

// config/bundle.php

return [
    // ...
    Odandb\MonitoringMetricsBundle\MonitoringMetricsBundle::class => ['dev' => true, 'test' => true],
];

Usage

Add the MonitoringAssertionsTrait trait to your test-class and you can start asserting response.

use Odandb\MonitoringMetricsBundle\Test\MonitoringAssertionsTrait;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class FooTest extends WebTestCase
{
    use MonitoringAssertionsTrait;
    
    protected $client;
    
    protected function setUp() : void
    {
        $this->client = self::createClient();
    }
    
    public function testBar()
    {
        $this->client->request('GET', '/foo/bar');

        self::assertResponseIsSuccessful();
        
        // available assertions/methods
        self::assertQueryCountMatches(5); // query doctrine should be exactly 5.
        self::assertQueryCountLessThan(5); // query doctrine should be less than 5
        self::assertMemoryUsageLessThan(10.4); // the memory used should be less than 10.4mb
    }
}

Metric

Doctrine

The DoctrineMetric collects information via the profiling logger and must be enabled via the debug value of the application. Alternatively, you can also enable it on the dbal.profiling or dbal.connections.default.profiling (multiple connections) configuration for the environment whose information you want to collect. The configuration on the connection may be more interesting to avoid activating all the debug features on the environment and avoid slowdowns.

Memory

The MemoryMetric collects information via memory_get_peak_usage from native php and convert to Mb.

Create your own metric

To create your own metric, you will need to extend the AbstractMetric class.

Each metric needs to implement the metric method, which retrieves the information and adds it to the headers of the response.