PHP code example of monitaroo / monitaroo-symfony

1. Go to this page and download the library: Download monitaroo/monitaroo-symfony library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

monitaroo / monitaroo-symfony example snippets


public function registerBundles()
{
    $bundles = [
        // ...
        new Monitaroo\Symfony\MonitarooBundle(),
    ];
}

use Monitaroo\Client;

class MyController
{
    private $monitaroo;

    public function __construct(Client $monitaroo)
    {
        $this->monitaroo = $monitaroo;
    }

    public function index()
    {
        // Logging
        $this->monitaroo->info('User logged in', ['user_id' => 123]);
        $this->monitaroo->error('Payment failed', ['order_id' => 456]);

        // Metrics
        $this->monitaroo->increment('orders.completed');
        $this->monitaroo->gauge('queue.size', 42);
        $this->monitaroo->timing('api.response_time', 145.5);

        // Timer helper
        $stop = $this->monitaroo->startTimer('db.query');
        $users = $this->repository->findAll();
        $stop(); // Records the timing
    }
}

use Psr\Log\LoggerInterface;

class MyService
{
    public function __construct(private LoggerInterface $logger)
    {
    }

    public function doSomething()
    {
        $this->logger->info('Something happened');
    }
}

$monitaroo->trace('Detailed trace');
$monitaroo->debug('Debug info');
$monitaroo->info('General info');
$monitaroo->warn('Warning');
$monitaroo->error('Error occurred');
$monitaroo->fatal('Fatal error');

// Context becomes searchable attributes
$monitaroo->info('Order placed', [
    'order_id' => 123,
    'amount' => 99.99,
]);

// Tags are indexed for fast filtering
$monitaroo->info('Order placed', [
    'order_id' => 123,
    'tags' => [
        'type' => 'order',
        'country' => 'FR',
    ],
]);

try {
    // ...
} catch (\Exception $e) {
    $monitaroo->error('Operation failed', [
        'exception' => $e,
    ]);
}

$monitaroo->increment('api.requests');
$monitaroo->increment('items.sold', 5);
$monitaroo->increment('api.requests', 1, ['endpoint' => '/users']);

$monitaroo->gauge('queue.size', $queueSize);
$monitaroo->gauge('memory.mb', memory_get_usage(true) / 1024 / 1024);

// Manual
$start = microtime(true);
$result = $this->doSomething();
$monitaroo->timing('operation.duration', (microtime(true) - $start) * 1000);

// Using helper
$stop = $monitaroo->startTimer('db.query', ['table' => 'users']);
$users = $repository->findAll();
$stop();

$monitaroo->histogram('order.amount', $order->getTotal());

$monitaroo->flush();

while ($processing) {
    // ... process items
    $monitaroo->flush();
}