PHP code example of yakovlef / telegraf-metrics-bundle

1. Go to this page and download the library: Download yakovlef/telegraf-metrics-bundle 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/ */

    

yakovlef / telegraf-metrics-bundle example snippets


Yakovlef\TelegrafMetricsBundle\TelegrafMetricsBundle::class => ['all' => true],

use Yakovlef\TelegrafMetricsBundle\Collector\MetricsCollectorInterface;

class UserController
{
    public function __construct(
        private MetricsCollectorInterface $metricsCollector
    ) {}

    public function register(): Response
    {
        // Your business logic here...

        // Send metrics
        $this->metricsCollector->collect('user_registration', [
            'count' => 1
        ], [
            'source' => 'web', //source for example web, mobile, api
            'country' => 'US' //country of the user
        ], );

        return new JsonResponse(['status' => 'success']);
    }
}

// Performance metrics
$this->metricsCollector->collect('api_response', [
    'response_time' => 145.2,
    'memory_usage' => 1024
], [
    'endpoint' => '/api/users',
    'method' => 'GET',
    'status' => '200'
]);

// Business metrics
$this->metricsCollector->collect('order_created', [
    'amount' => 99.99,
    'items_count' => 3
], [
    'payment_method' => 'credit_card',
    'currency' => 'USD'
]);

// Error tracking
$this->metricsCollector->collect('application_error', [
    'count' => 1
], [
    'type' => 'database_connection',
    'severity' => 'critical'
]);