PHP code example of renoki-co / laravel-exporter-contracts
1. Go to this page and download the library: Download renoki-co/laravel-exporter-contracts 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/ */
renoki-co / laravel-exporter-contracts example snippets
use RenokiCo\LaravelExporter\GaugeMetric;
class DatabaseUsers extends GaugeMetric
{
/**
* Get the metric help.
*
* @return string
*/
protected function help(): string
{
return 'Get the total amount of users.';
}
/**
* Perform the update call on the collector.
* Optional, as some metrics can be modified somewhere else.
*
* @return void
*/
public function update(): void
{
$this->set(User::count());
}
}
use RenokiCo\LaravelExporter\Exporter;
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
Exporter::register(DatabaseUsers::class);
}
// ...
}
class DatabaseRecords extends GaugeMetric
{
/**
* Define the default labels with their values.
*
* @return array
*/
protected function defaultLabels(): array
{
return [
'static_label' => 'static-value',
];
}
/**
* Get the metric allowed labels.
*
* @return array
*/
protected function allowedLabels(): array
{
return [
'model',
'static_label',
];
}
/**
* Perform the update call on the collector.
* Optional, as some metrics can be modified somewhere else.
*
* @return void
*/
public function update(): void
{
$models = [
User::class,
Post::class,
Team::class,
];
foreach ($models as $model) {
$this->set(
value: $model::count(),
labels: ['model' => $model,
]);
}
}
}
class CustomMetrics extends Metric
{
/**
* The group this metric gets shown into.
*
* @var string|null
*/
public static $showsOnGroup = 'metrics-reloaded';
// ...
}
Route::get('/exporter/group/{group?}', ...);
use RenokiCo\LaravelExporter\Exporter;
Exporter::exportResponse(function () {
return <<<'PROM'
# TYPE openswoole_max_conn gauge
openswoole_max_conn 256
# TYPE openswoole_coroutine_num gauge
openswoole_coroutine_num 1
PROM;
);
use RenokiCo\LaravelExporter\Exporter;
Exporter::exportResponse(..., 'metrics-reloaded');
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.