### Run Laravel Prometheus Install Command Source: https://github.com/spatie/laravel-prometheus/blob/main/docs/installation-setup.md Executes the `prometheus:install` Artisan command provided by the package. This command publishes the configuration file and registers the service provider. ```bash php artisan prometheus:install ``` -------------------------------- ### Install spatie/laravel-prometheus via Composer Source: https://github.com/spatie/laravel-prometheus/blob/main/docs/installation-setup.md Installs the spatie/laravel-prometheus package using Composer. This is the primary method for adding the package to your Laravel project. ```bash composer require spatie/laravel-prometheus ``` -------------------------------- ### Default Prometheus Service Provider Source: https://github.com/spatie/laravel-prometheus/blob/main/docs/installation-setup.md The default content for the `PrometheusServiceProvider` which registers custom collectors. It includes an example of registering a gauge metric and a commented-out section for registering Horizon collectors. ```php namespace App\Providers; use Illuminate\Support\ServiceProvider; use Spatie\Prometheus\Collectors\Horizon\CurrentMasterSupervisorCollector; use Spatie\Prometheus\Collectors\Horizon\CurrentProcessesPerQueueCollector; use Spatie\Prometheus\Collectors\Horizon\CurrentWorkloadCollector; use Spatie\Prometheus\Collectors\Horizon\FailedJobsPerHourCollector; use Spatie\Prometheus\Collectors\Horizon\HorizonStatusCollector; use Spatie\Prometheus\Collectors\Horizon\JobsPerMinuteCollector; use Spatie\Prometheus\Collectors\Horizon\RecentJobsCollector; use Spatie\Prometheus\Facades\Prometheus; class PrometheusServiceProvider extends ServiceProvider { public function register() { /* * Here you can register all the exporters that you * want to export to prometheus */ Prometheus::addGauge('my_gauge', function () { return 123.45; }); /* * Uncomment this line if you want to export * all Horizon metrics to prometheus */ // $this->registerHorizonCollectors(); } public function registerHorizonCollectors(): self { Prometheus::registerCollectorClasses([ CurrentMasterSupervisorCollector::class, CurrentProcessesPerQueueCollector::class, CurrentWorkloadCollector::class, FailedJobsPerHourCollector::class, HorizonStatusCollector::class, JobsPerMinuteCollector::class, RecentJobsCollector::class, ]); return $this; } } ``` -------------------------------- ### Configure Metrics Endpoint URL Source: https://github.com/spatie/laravel-prometheus/blob/main/docs/installation-setup.md Modifies the default URL for the metrics endpoint by updating the `urls.default` key in the `config/prometheus.php` file. ```php // in config/prometheus.php 'urls' => [ 'default' => 'alternative-url', ], ``` -------------------------------- ### Default Laravel Prometheus Configuration Source: https://github.com/spatie/laravel-prometheus/blob/main/docs/installation-setup.md The default configuration array for the spatie/laravel-prometheus package, published to `config/prometheus.php`. It defines enabled status, metrics URLs, allowed IPs, default namespace, middleware, and custom actions. ```php return [ 'enabled' => true, /* * The urls that will return metrics. */ 'urls' => [ 'default' => 'prometheus', ], /* * Only these IP's will be allowed to visit the above urls. * When set to `null` all IP's are allowed. */ 'allowed_ips' => [ // '1.2.3.4', ], /* * This is the default namespace that will be * used by all metrics */ 'default_namespace' => 'app', /* * The middleware that will be applied to the urls above */ 'middleware' => [ Spatie\Prometheus\Http\Middleware\AllowIps::class, ], /* * You can override these classes to customize low-level behaviour of the package. * In most cases, you can just use the defaults. */ 'actions' => [ 'render_collectors' => Spatie\Prometheus\Actions\RenderCollectorsAction::class, ], ]; ``` -------------------------------- ### Export Laravel Horizon Metrics Source: https://github.com/spatie/laravel-prometheus/blob/main/docs/installation-setup.md Enables the export of Laravel Horizon metrics by uncommenting the `registerHorizonCollectors()` method call within the `PrometheusServiceProvider`. ```php $this->registerHorizonCollectors(); ``` -------------------------------- ### Install Redis Extension on Ubuntu Source: https://github.com/spatie/laravel-prometheus/blob/main/README.md Provides the command to install the PHP Redis extension on Ubuntu systems, which is a prerequisite for running certain tests within the laravel-prometheus package. ```bash sudo apt-get install php-redis ``` -------------------------------- ### Secure Metrics Endpoint with Allowed IPs Source: https://github.com/spatie/laravel-prometheus/blob/main/docs/installation-setup.md Restricts access to the metrics endpoint to specific IP addresses by configuring the `allowed_ips` array in `config/prometheus.php`. Only requests from the listed IPs will be permitted. ```php // in config/prometheus.php 'allowed_ips' => [ '1.2.3.4', ], ``` -------------------------------- ### Install Redis Extension on MacOS Source: https://github.com/spatie/laravel-prometheus/blob/main/README.md Provides the command to install the PHP Redis extension on MacOS systems using PECL, a prerequisite for running specific tests in the laravel-prometheus package. ```bash pecl install redis ``` -------------------------------- ### Configure Prometheus Cache Source: https://github.com/spatie/laravel-prometheus/blob/main/docs/advance-usage/using-counter-metric.md Provides an example of configuring the cache driver for metrics in the `config/prometheus.php` file. This example shows setting the cache to `null` for an in-memory implementation without using Laravel's cache. This is suitable for simple setups or testing. ```php 'cache' => null, ``` -------------------------------- ### Run Laravel Prometheus Tests Source: https://github.com/spatie/laravel-prometheus/blob/main/README.md This command executes the test suite for the spatie/laravel-prometheus package using Composer. Ensure all dependencies are installed before running this command. ```bash composer test ``` -------------------------------- ### Create a Counter Metric Source: https://github.com/spatie/laravel-prometheus/blob/main/docs/advance-usage/using-counter-metric.md Demonstrates how to create a basic counter metric using the `addCounter` method. This metric will start at zero and can be incremented later. No external dependencies beyond the package itself are required. ```php $counter = Prometheus::addCounter('my_counter'); ``` -------------------------------- ### Example Horizon Master Supervisor Collector Source: https://github.com/spatie/laravel-prometheus/blob/main/docs/advance-usage/creating-collectors.md An example PHP collector class that registers a gauge metric to track the number of Horizon master supervisors. It uses Laravel's service container to resolve the `MasterSupervisorRepository`. ```php namespace Spatie\Prometheus\Collectors\Horizon; use Laravel\Horizon\Contracts\MasterSupervisorRepository; use Spatie\Prometheus\Collectors\Collector; use Spatie\Prometheus\Facades\Prometheus; class CurrentMasterSupervisorCollector implements Collector { public function register(): void { Prometheus::addGauge('Number of master supervisors') ->name('horizon_master_supervisors') ->helpText('The number of master supervisors') ->value(fn () => app(MasterSupervisorRepository::class)->all()); } } ``` -------------------------------- ### Set Initial Value for Counter Source: https://github.com/spatie/laravel-prometheus/blob/main/docs/advance-usage/using-counter-metric.md Shows how to initialize a counter metric with a specific starting value. This is useful when the count should begin from a number other than zero. It requires the `setInitialValue` method after creating the counter. ```php $counter = Prometheus::addCounter('my counter')->setInitialValue(100); ``` -------------------------------- ### Add Gauge Metric with Dynamic Value - PHP Source: https://github.com/spatie/laravel-prometheus/blob/main/docs/basic-usage/creating-gauges.md This example shows how to create a gauge metric that dynamically fetches its value from the database. It registers a gauge named 'User count' and sets its value to the total number of users in the application using `User::count()`. ```php Prometheus::addGauge('User count') ->value(fn() => User::count()); ``` -------------------------------- ### Add Gauge Metric with Labels - PHP Source: https://github.com/spatie/laravel-prometheus/blob/main/docs/basic-usage/creating-gauges.md This example shows how to add labels to a gauge metric to distinguish different dimensions of data. It registers a gauge named 'User count' with a 'status' label and provides values for 'active' and 'inactive' user statuses. ```php Prometheus::addGauge('User count') ->label('status') ->value(function() { return [ [User::where('status', 'active')->count(), ['active']], [User::where('status', 'inactive')->count(), ['inactive']], ]; }); ``` -------------------------------- ### Add Gauge Metric with Help Text - PHP Source: https://github.com/spatie/laravel-prometheus/blob/main/docs/basic-usage/creating-gauges.md This code illustrates how to add descriptive help text to a gauge metric. It registers a gauge named 'User count', provides a help text 'This is the number of users in our app', and sets its value using `User::count()`. ```php Prometheus::addGauge('User count') ->helpText('This is the number of users in our app') ->value(fn() => User::count()); ``` -------------------------------- ### Configure fly.toml for Prometheus Metrics Source: https://github.com/spatie/laravel-prometheus/blob/main/docs/setting-up-prometheus-and-grafana/using-fly-metrics.md This TOML configuration snippet tells Fly.io where to find your Prometheus metrics. Ensure the `port` matches your application's internal port and `path` is set to `/prometheus` for the spatie/laravel-prometheus package. ```toml [metrics] port = 8080 path = "/prometheus" ``` -------------------------------- ### Add Gauge Metric with Named Arguments - PHP Source: https://github.com/spatie/laravel-prometheus/blob/main/docs/basic-usage/creating-gauges.md This code provides an alternative syntax for defining a gauge metric using named arguments. It sets the name, help text, namespace, labels, and value for the 'User count' gauge in a single method call. ```php Prometheus::addGauge( name: 'User count', helpText: 'This is the number of users in our app', namespace: 'My custom namespace', labels: ['status'], value: function() { return [ [User::where('status', 'active')->count(), ['active']], [User::where('status', 'inactive')->count(), ['inactive']], ]; } ); ``` -------------------------------- ### Register User Count Metric in PHP Source: https://github.com/spatie/laravel-prometheus/blob/main/docs/introduction.md This snippet demonstrates how to register a gauge metric named 'User count' using the Prometheus facade. The value of the metric is dynamically set by counting the total number of users in the database. This requires the Prometheus facade to be available in the PHP environment. ```php Prometheus::addGauge('User count') ->value(fn() => User::count()); ``` -------------------------------- ### Expose Metric to Alternative Endpoint in Laravel Prometheus Source: https://github.com/spatie/laravel-prometheus/blob/main/docs/advance-usage/using-multiple-endpoints.md Shows how to assign a specific metric to an alternative endpoint defined in the configuration. This uses the `urlName` method to direct the metric to a non-default URL. ```php use Spatie\Prometheus\Facades\Prometheus; Prometheus::addGauge('User count') ->urlName('alternative') ->value(fn() => User::count(); ``` -------------------------------- ### Add Gauge Metric - PHP Source: https://github.com/spatie/laravel-prometheus/blob/main/docs/_index.md Demonstrates how to add a gauge metric to expose the current number of users. This typically resides within a service provider and uses the Spatie Prometheus facade. ```php // typically in a service provider use \Spatie\Prometheus\Facades\Prometheus; Prometheus::addGauge('Number of users') ->value(fn() => User::count()); ``` -------------------------------- ### Configure Multiple Endpoints in Laravel Prometheus Source: https://github.com/spatie/laravel-prometheus/blob/main/docs/advance-usage/using-multiple-endpoints.md Defines alternative endpoints for Prometheus metrics in the Laravel configuration file. This allows for exporting metrics at different URLs, typically for varied scraping frequencies. ```php // in config/prometheus.php 'urls' => [ 'default' => 'prometheus', 'alternative' => 'alternative-endpoint', ], ``` -------------------------------- ### Registering Collector Classes in Service Provider Source: https://github.com/spatie/laravel-prometheus/blob/main/docs/advance-usage/creating-collectors.md Demonstrates how to register custom collector classes within a Laravel service provider using the `Prometheus` facade. This makes the defined metrics available for collection. ```php // in a service provider use Spatie\Prometheus\Facades\Prometheus; use Spatie\Prometheus\Collectors\Horizon\CurrentMasterSupervisorCollector; Prometheus::registerCollectorClasses([ CurrentMasterSupervisorCollector::class, ]); ``` -------------------------------- ### Configure Prometheus Cache with Laravel Driver Source: https://github.com/spatie/laravel-prometheus/blob/main/docs/advance-usage/using-counter-metric.md Demonstrates configuring Prometheus to use a specific Laravel cache driver, such as Redis, for storing metrics. This involves setting the `cache` key in `config/prometheus.php` to a valid driver name found in `config/cache.php`. This ensures metrics are persisted across requests. ```php 'cache' => 'redis', ``` -------------------------------- ### Add Gauge Metric with Value - PHP Source: https://github.com/spatie/laravel-prometheus/blob/main/docs/basic-usage/creating-gauges.md This snippet demonstrates the basic usage of adding a gauge metric with a static value. It registers a gauge named 'My gauge' and sets its value to 123.45. The metric will be exposed on the /prometheus endpoint. ```php Prometheus::addGauge('My gauge') ->value(fn() => 123.45); ``` -------------------------------- ### Add Gauge Metric with Custom Namespace - PHP Source: https://github.com/spatie/laravel-prometheus/blob/main/docs/basic-usage/creating-gauges.md This snippet demonstrates how to set a custom namespace for a gauge metric. It registers a gauge named 'User count', assigns it to the 'My custom namespace', and sets its value. The metric will be prefixed with the custom namespace, e.g., 'my_custom_namespace_user_count'. ```php Prometheus::addGauge('User count') ->namespace('My custom namespace') ->value(fn() => User::count()); ``` -------------------------------- ### Collector Interface Definition Source: https://github.com/spatie/laravel-prometheus/blob/main/docs/advance-usage/creating-collectors.md Defines the contract for collector classes. Any class implementing this interface must provide a `register` method for metric registration. ```php namespace Spatie\Prometheus\Collectors; interface Collector { public function register(): void; } ``` -------------------------------- ### Configure Prometheus Scrape Job for Laravel Application Source: https://github.com/spatie/laravel-prometheus/blob/main/docs/setting-up-prometheus-and-grafana/self-hosted.md This YAML configuration snippet defines a Prometheus job to scrape metrics from a Laravel application. It specifies the scrape interval, the path for metrics, and the target application endpoint. Ensure your Laravel application is configured to expose metrics at the specified path. ```yaml scrape_configs: - job_name: laravel scrape_interval: 10s metrics_path: /prometheus static_configs: - targets: ['your-laravel-app.com'] ``` -------------------------------- ### Register User Count Metric in Laravel Source: https://github.com/spatie/laravel-prometheus/blob/main/README.md This snippet demonstrates how to register a gauge metric for the user count in a Laravel application using the spatie/laravel-prometheus package. It utilizes a closure to dynamically fetch the user count. ```php Prometheus::addGauge('User count') ->value(fn() => User::count()); ``` -------------------------------- ### Configure Grafana Agent for Laravel Metrics Source: https://github.com/spatie/laravel-prometheus/blob/main/docs/setting-up-prometheus-and-grafana/using-grafana-com.md This YAML configuration defines a job to scrape metrics from a Laravel application's /prometheus endpoint and send them to a remote write URL, typically for a hosted Prometheus instance on Grafana.com. ```yaml global: scrape_interval: 10s configs: - name: hosted-prometheus scrape_configs: - job_name: laravel scrape_interval: 10s metrics_path: /prometheus static_configs: - targets: ['your-app.com'] remote_write: - url: basic_auth: username: password: ``` -------------------------------- ### Register Horizon Collectors in Laravel Source: https://github.com/spatie/laravel-prometheus/blob/main/docs/basic-usage/using-horizon-exporters.md This code snippet registers the Horizon collectors to export metrics. Ensure the PrometheusServiceProvider is registered in your application. ```php $this->registerHorizonCollectors(); ``` -------------------------------- ### Increment Counter by One Source: https://github.com/spatie/laravel-prometheus/blob/main/docs/advance-usage/using-counter-metric.md Illustrates the basic increment operation for a counter metric using the `inc()` method. This increases the counter's value by a single unit. Ensure the counter has been previously created. ```php $counter->inc(); ``` -------------------------------- ### Increment Counter by Specific Value Source: https://github.com/spatie/laravel-prometheus/blob/main/docs/advance-usage/using-counter-metric.md Explains how to increment a counter metric by a value greater than one. The desired increment amount is passed as an argument to the `inc()` method. This allows for more flexible counting scenarios. ```php $counter->inc(2); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.