1. Go to this page and download the library: Download dbstudios/prometheus-client 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/ */
dbstudios / prometheus-client example snippets
use DaybreakStudios\PrometheusClient\CollectorRegistry;
$registry = new CollectorRegistry();
use DaybreakStudios\PrometheusClient\Adapter\Apcu\ApcuAdapter;
$adapter = new ApcuAdapter();
use DaybreakStudios\PrometheusClient\Collector\Counter;
use DaybreakStudios\PrometheusClient\Collector\Gauge;
use DaybreakStudios\PrometheusClient\Collector\Histogram;
$counter = new Counter($adapter, 'test_counter', 'Please ignore');
$registry->register($counter);
$gauge = new Gauge($adapter, 'test_gauge', 'Please ignore');
$registry->register($gauge);
$histogram = new Histogram($adapter, 'test_histogram', 'Please ignore', [
1,
5,
15,
50,
100
]);
$registry->register($histogram);
$counter = $registry->getCounter('test_counter');
$counter->increment();
$histogram = $registry->getHistogram('test_gauge');
// throws DaybreakStudios\PrometheusClient\Exception\CollectorRegistryException due to type mismatch
use DaybreakStudios\PrometheusClient\Collector\Counter;
$counter = new Counter($adapter, 'api_calls_total', 'Number of API calls made', [
'path',
'method',
]);
$counter->increment([
'method' => 'GET',
'path' => '/users/me',
]);
use DaybreakStudios\PrometheusClient\Export\Render\TextRenderer;
$renderer = new TextRenderer();
header('Content-Type: ' . $renderer->getMimeType());
echo $renderer->render($registry->collect());
use DaybreakStudios\PrometheusClient\Collector\Counter;
/**
* @var \DaybreakStudios\PrometheusClient\Adapter\AdapterInterface $adapter
*/
$counter = new Counter($adapter, 'requests_served', 'The number of requests processed by the application');
$counter->increment();
$counter->increment([], 3);
use DaybreakStudios\PrometheusClient\Collector\Gauge;
/**
* @var \DaybreakStudios\PrometheusClient\Adapter\AdapterInterface $adapter
*/
$gauge = new Gauge($adapter, 'cpu_usage_last5', 'CPU usage average over the last 5 minutes');
$gauge->increment();
$gauge->decrement();
$gauge->set(1.7);
use DaybreakStudios\PrometheusClient\Collector\Histogram;
/**
* @var \DaybreakStudios\PrometheusClient\Adapter\AdapterInterface $adapter
*/
$histogram = new Histogram(
$adapter,
'api_call_execution_time_milliseconds',
'Call time to external API',
[50, 1000, 5000]
);
$histogram->observe(17);
$histogram->observe(120);
$histogram->observe(2700);
$histogram->observe(7010);
/**
* @var \DaybreakStudios\PrometheusClient\Collector\Histogram $histogram
*/
$timer = $histogram->startTimer();
usleep(10000); // ~10 ms
$timer->observe(); // Tracks time since Histogram::startTime() was invoked and adds it to the histogram
$histogram->time(function() {
usleep(100000); // ~100 ms
}); // Invokes the callable passed to Histogram::time() and adds the execution time as a value of the histogram
use DaybreakStudios\PrometheusClient\Collector\Histogram;
use DaybreakStudios\PrometheusClient\Collector\HistogramTimer;
/**
* @var \DaybreakStudios\PrometheusClient\Adapter\AdapterInterface $adapter
*/
$histogram = new Histogram(
$adapter,
'api_call_execution_time_seconds',
'Call time to external API',
[100, 1000, 5000],
[],
HistogramTimer::PRECISION_SECONDS,
);
$histogram->time(function() {
usleep(1100000); // ~1100 ms or 1.1 s
}); // Adds 1 to the histogram (e.g. `$histogram->observe(1)`)
use DaybreakStudios\PrometheusClient\Adapter\Redis\RedisAdapter;
use DaybreakStudios\PrometheusClient\Adapter\Redis\RedisClientConfiguration;
$config = new RedisClientConfiguration('localhost');
// You can also supply other information, such as port or password, using the setters
// available on the configuration object, e.g.:
// - $config->setPort(1234)
// - $config->setPassword('MyTotallySecurePassword')
$adapter = new RedisAdapter($config);
use DaybreakStudios\PrometheusClient\Adapter\Filesystem\FilesystemAdapter;
$adapter = new FilesystemAdapter('/var/www/html/prometheus');
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.