PHP code example of alexpon92 / php2elk-metrics

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

    

alexpon92 / php2elk-metrics example snippets


Php2ElkMetrics\Laravel\Providers\Php2ElkMetricsProvider::class

    /**
     * Unique metric name
     * @return string
     */
    abstract public static function getName(): string;

    /**
     * Method to convert metric contents to array
     *
     * @return array
     */
    abstract public function arraySerialize(): array;

namespace App\Metrics;

use Php2ElkMetrics\Metrics\BaseMetric;
use DateTime;

class SomeVisitDurationMetric extends BaseMetric
{
    /**
     * @var string
     */
    private $userName;

    /**
     * @var float
     */
    private $duration;

    /**
     * SomeVisitDurationMetric constructor.
     *
     * @param string        $userName
     * @param float         $duration
     * @param DateTime|null $time
     */
    public function __construct(string $userName, float $duration, ?DateTime $time)
    {
        $this->userName = $userName;
        $this->duration = $duration;
        $this->time     = $time; // time is protected field from BaseMetric class
    }


    public static function getName(): string
    {
        return 'some_duration_metric';
    }

    public function arraySerialize(): array
    {
        return [
            'user_name' => $this->userName,
            'duration'  => $this->duration
        ];
    }
}

    $this->app->singleton(
        Registry::class,
        static function ($app) {
            /** @var Container $app */
            $metricRegistry = new Registry();

            $defaultIndex = config('php2elk-metrics.default_index');
            $defaultConnection = config('php2elk-metrics.connections.default');

            $metricRegistry->add(
                new MetricsConfig(
                    SomeVisitDurationMetric::getName(),
                    $defaultIndex,
                    $defaultConnection
                )
            );

            return $metricRegistry;
        }
    );

$producer = app(\Php2ElkMetrics\MetricsProducer\MetricsProducer::class);
$metric = new SomeVisitDurationMetric('John', 12.02);
$producer->produceMetric($metric);
2ElkMetrics\Laravel\Listeners\MetricsEventListener

$metric = new SomeVisitDurationMetric('John', 12.02);
event(new \Php2ElkMetrics\Events\MetricEvent($metric, new \DateTime()));

$producer = app(\Php2ElkMetrics\MetricsProducer\MetricsProducer::class);
$metricsCollection = new \Php2ElkMetrics\Metrics\MetricsCollection();
$metricsCollection
    ->add(new SomeVisitDurationMetric('John', 12.02))
    ->add(new SomeVisitDurationMetric('Alex', 30.02));
$producer->bulkProduce($metricsCollection);
2ElkMetrics\Metrics\DefaultMetrics\Postgres\TableDeadRowsMetric
bash
php artisan vendor:publish --provider="Php2ElkMetrics\Laravel\Providers\Php2ElkMetricsProvider" --tag=config
bash
php artisan vendor:publish --provider="Php2ElkMetrics\Laravel\Providers\Php2ElkMetricsProvider" --tag=service-provider
bash
php artisan vendor:publish --provider="Php2ElkMetrics\Laravel\Providers\Php2ElkMetricsProvider" --tag=service-provider
bash
php artisan php2elk:setup-index {--connection_name=} {--index_name=} {--default_metrics}
bash
php artisan php2elk:php2elk:php2elk:collect-failed-queues
bash
php artisan php2elk:check-mappings