PHP code example of ns3777k / prometheus-bundle

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

    

ns3777k / prometheus-bundle example snippets




return [
    // ...
    Ns3777k\PrometheusBundle\Ns3777kPrometheusBundle::class => ['all' => true],
];



declare(strict_types=1);

namespace App\Weather;

use Ns3777k\PrometheusBundle\Metrics\CollectorRegistryInterface;

class WeatherClient
{
    private $registry;

    public function __construct(CollectorRegistryInterface $registry)
    {
        $this->registry = $registry;
    }

    public function getWeatherForRegion(string $region)
    {
        $histogram = $this->registry->getOrRegisterHistogram(
            'weather_request_duration_seconds',
            'Weather request duration with response information',
            ['region']
        );

        $start = microtime(true);
        // do request
        $duration = microtime(true) - $start;

        $histogram->observe($duration, [$region]);
    }

}

request_duration_seconds_count{code=~"(2|3).*"}

request_duration_seconds_count{code=~"(4|5).*"}